I. Preface
Practice is the sole criterion for testing truth. It's shallow on paper. If you want to know a person, just rely on personal data, even if you meet and talk about one or two sentences, it will also produce a lot of contingent results. As the saying goes, adversity can see the truth. Of course, here is not to let you go to adversity, but to have a simple interaction with redis. Although the mechanism is not detailed, there will still be a general impression of what it looks like. If you don't say much, just raise your sleeve!
2. Specific operation
1. Open eclipse and build a new Maven project.
2. Import additional jar packages:
<! - redis Toolkit - >
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
</dependency>
3. Not much code, just two classes
(1)MyRedisTest.java
package com.spring.server.redis; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import redis.clients.jedis.Jedis; public class MyRedisTest { public static void main(String args[]){ final String watchKeys = "watchKeys"; ExecutorService executor = Executors.newFixedThreadPool(20);//Twenty thread pools are concurrent. final Jedis jedis = new Jedis("127.0.0.1",6379); System.out.println(jedis.get("myKey")); jedis.set(watchKeys,"100");//Set the starting number of snaps. jedis.close(); for(int i = 0; i < 1000; i++){//Set up 1000 people to launch a snap-up executor.execute(new MyRunnable("user"+getRandomString(6))); } executor.shutdown(); } private static String getRandomString(int length) {//Length is a random string length String base = "abcdefghijklmnopqrstuvwxyz0123456789"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for(int i = 0; i<length; i++){ //The purpose of this method is to generate a random int value, which is in the range of [0, n], i.e. the random int value between 0 and n, containing 0 but not n. int number = random.nextInt(base.length()); //The charAt() method is used to return characters at the specified index. The index ranges from 0 to length() - 1. sb.append(base.charAt(number)); } return sb.toString(); } }
(2)MyRunnable.java
package com.spring.server.redis; import java.util.List; import redis.clients.jedis.Jedis; import redis.clients.jedis.Transaction; public class MyRunnable implements Runnable { String watchKeys = "watchKeys";//Monitoring keys Jedis jedis = new Jedis("127.0.0.1",6379); String userInfo; public void myRunnable(){ } public MyRunnable(String uinfo){ this.userInfo = uinfo; } @Override public void run() { try{ jedis.watch(watchKeys); String val = jedis.get(watchKeys); int valint = Integer.valueOf(val); if(valint <= 100 && valint >=1){ Transaction tx = jedis.multi();//Open transaction tx.incrBy("watchKeys",-1); List<Object> list = tx.exec();//Submit the transaction and return null if watchKeys is changed at this time if(list == null || list.size()==0){ String failuserifo = "fail"+userInfo; String failinfo="User:" + failuserifo + "Commodity scramble fails, snap-up fails"; System.out.println(failinfo); /* Business Logic of Buying Failure */ jedis.setnx(failuserifo, failinfo); }else{ for(Object succ : list){ String succuserifo ="succ"+succ.toString() +userInfo ; String succinfo="User:" + succuserifo + "Successful Buying, Number of Successful Buying at Present:" + (1-(valint-100)); System.out.println(succinfo); /* Business Logic of Successful Buying */ jedis.setnx(succuserifo, succinfo); } } }else{ String failuserifo = "kcfail" + userInfo; String failinfo1="User:" + failuserifo + "Goods were snapped up and failed"; System.out.println(failinfo1); jedis.setnx(failuserifo, failinfo1); // Thread.sleep(500); return; } }catch(Exception e){ e.printStackTrace(); }finally{ jedis.close(); } } }
4. Click Run
OK! Now that the interaction with redis is over, what do you gain?