Keyword: Redis jedis multithreading
Using Jedis as a client connection Redis,jedis encapsulates a number of operations on Reids, which makes it easy to access Redis
1 <Dependency>2 <groupId>Redis.clients</groupId>3 <Artifactid>Jedis</Artifactid>4 <version>2.8.1</version>5 </Dependency>
A Redis instance is installed in the native virtual machine and the IP address is 192.168.1.110, the base object for connecting Reids in Port 6379,jedis is Jedis, creating a simple Jedis object
1 New Jedis ("192.168.1.110", 6379);
Then you can use the basic additions and deletions to the operation, with a string type of key value pairs as an example
1 Public Staticstring getString (String key) {2String result =Jedis.get (key);3 returnresult;4 }5 6 Public Static voidputstring (String key, String valuestr) {7Rowkey =key;8 Jedis.set (ROWKEY,VALUESTR);9 }Ten One Public Static Longdelstring (String key) { ARowkey =key; - returnJedis.del (Rowkey);//Success 1, fail 0 -}
It is worth noting that the Jedis object is thread insecure, so in the case of concurrency to use Jedispool, Jedispool only supports 8 connections by default, so the maximum number of connections to Jedispool is first modified when declaring Jedispool
1 New jedispoolconfig (); 2 // To modify the maximum number of connections 3 Config.setmaxtotal (a); 4 // declaring a pool of threads 5 New Jedispool (config, "192.168.1.102", 6379); 6 7 // get Jedis Object 8 Jedis Tjedis = Pool.getresource ();
Using Jedis to connect to Redis