Start redis-server/etc/redis/redis.conf First
Package Com.test;import Java.util.arraylist;import java.util.iterator;import java.util.list;import java.util.Set; Import Redis.clients.jedis.jedis;import Redis.clients.jedis.jedispool;import redis.clients.jedis.JedisPoolConfig; Import Redis.clients.jedis.jedisshardinfo;import Redis.clients.jedis.shardedjedis;import Redis.clients.jedis.ShardedJedisPool; /** * * @author Luozhonghua * */public class Redisclient {private Jedis jedis;//non-slicing amount Client connection private Jedispool jedispool;// Non-tiled connection pool private Shardedjedis shardedjedis;//Slice amount Client Connection private Shardedjedispool shardedjedispool;//tile Connection Pool Public Redisclient () {initialpool (); Initialshardedpool (); Shardedjedis = Shardedjedispool.getresource (); Jedis = Jedispool.getresource ();} /** * Initialize non-tile pool */private void Initialpool () {//Pool basic configuration jedispoolconfig config = new Jedispoolconfig (); Config.setmaxactive (20 ); Config.setmaxidle (5); config.setmaxwait (1000l); Config.settestonborrow (false); SYSTEM.OUT.PRINTLN ("Begin ..."); jedispool = new Jedispool (config, "127.0.0.1", 6379); System.out.println ("End ...");} /** * Initialize tile pool */private void Initialshardedpool () {//Pool basic configuration jedispoolconfig config = new Jedispoolconfig (); config.setmaxact Ive: Config.setmaxidle (5); config.setmaxwait (1000l); Config.settestonborrow (false);//slave link list< jedisshardinfo> shards = new arraylist<jedisshardinfo> () Shards.add (New Jedisshardinfo ("127.0.0.1", 6379, " Master "));//Construction Pool Shardedjedispool = new Shardedjedispool (config, shards);} public void Show () {keyoperate (); Stringoperate (); Listoperate (); Setoperate (); Sortedsetoperate (); Hashoperate (); Jedispool.returnresource (Jedis); Shardedjedispool.returnresource (Shardedjedis);} private void Keyoperate () {System.out.println ("======================key=========================="); Clears the data System.out.println ("Clears all data in the library:" +jedis.flushdb ()); Determine if key is present System.out.println ("Determine if the key999 key exists:" +shardedjedis.exists ("key999")); System.out.println ("New key001,value001 key value pair:" +shardedjedis.set ("key001", "value001 ")); System.out.println ("Judging whether key001 exists:" +shardedjedis.exists ("key001")); All key System.out.println in the output system ("New key002,value002 key value pair:" +shardedjedis.set ("key002", "value002")); SYSTEM.OUT.PRINTLN ("All keys in the system are as follows:"); set<string> keys = Jedis.keys ("*"); Iterator<string> It=keys.iterator (); while (It.hasnext ()) {String key = It.next (); SYSTEM.OUT.PRINTLN (key); }//delete a key, and if key does not exist, the command is ignored. System.out.println ("Delete key002 in the system:" +jedis.del ("key002")); System.out.println ("Judging whether key002 exists:" +shardedjedis.exists ("key002")); Set the expiration time for key001 System.out.println ("Setting key001 expires at 5 seconds:" +jedis.expire ("key001", 5)); try{Thread.Sleep (2000); } catch (Interruptedexception e) {}//View the remaining lifetime of a key, in seconds. Permanent or non-existent return-1 System.out.prin TLN ("View remaining lifetime of key001:" +jedis.ttl ("key001")); Removes the time-to-live of a key System.out.println ("Remove Key001 Time to Live: "+jedis.persist (" key001 ")); System.out.println ("View remaining lifetime of key001:" +jedis.ttl ("key001")); View the type of value stored by key System.out.println ("View the type of value stored by key:" +jedis.type ("key001")); /* * Some other methods: 1, modify the key name: Jedis.rename ("Key6", "Key0"); * 2. Move the current DB key to the given DB: Jedis.move ("foo", 1) */}private void Stringoperate () {System . OUT.PRINTLN ("======================string_1=========================="); Clears the data System.out.println ("Clears all data in the library:" +jedis.flushdb ()); System.out.println ("============= increase ============="); Jedis.set ("key001", "value001"); Jedis.set ("key002", "value002"); Jedis.set ("key003", "value003"); System.out.println ("3 new key values have been added to the following:"); System.out.println (Jedis.get ("key001")); System.out.println (Jedis.get ("key002")); System.out.println (Jedis.get ("key003")); System.out.println ("============= delete ============="); System.out.println ("Delete key003 key-value pairs:" +jedis.del ("key003")); System.out.println ("Gets the value of the key003 key:" +jedis.get ("key003")); System.out.println ("============= change ============="); 1, direct coverage of the original data System.out.println ("directly covered key001 original data:" +jedis.set ("key001", "value001-update")); System.out.println ("Get the new value for key001:" +jedis.get ("key001")); 2, directly overwrite the original data System.out.println ("appended to the original value of key002:" +jedis.append ("key002", "+appendstring")); System.out.println ("Get key002 corresponding to the new value" +jedis.get ("key002")); System.out.println ("============= increase, delete, check (multiple) ============="); /** * Mset,mget simultaneously added, modified, query multiple key value pairs * equivalent to: * Jedis.set ("name", "SSSs"); * Jedis.set ("Jarorwar", "xxxx"); */System.out.println ("One-time new key201,key202,key203,key204 and its corresponding values:" +jedis.mset ("key201", "value201", "key202", "value202", "key203", "value203", "key204", "value204"); System.out.println ("One-time get key201,key202,key203,key204 corresponding value:" + Jedis.mget ("key201", "key202", "key203", "key204")); SYSTEM.OUT.PRINTLN ("Disposable Delete key201,key202:" +jedis.del (New string[]{"key201", "key202"}); System.out.println ("One-time get key201,key202,key203,key204 corresponding values:" + jedis.mget ("key201", "key202", "key203", "key204 ")); System.out.println (); Jedis features Shardedjedis can also be used directly, the following test some of the previously useless methods System.out.println ("======================string_2============== ============"); Clears the data System.out.println ("Clears all data in the library:" +jedis.flushdb ()); System.out.println ("============= new key value pair to prevent overwriting the original value ============="); System.out.println ("The original key301 does not exist, added key301:" +shardedjedis.setnx ("key301", "value301"); System.out.println ("The original key302 does not exist, added key302:" +shardedjedis.setnx ("key302", "value302"); System.out.println ("When key302 exists, try adding key302:" +shardedjedis.setnx ("key302", "value302_new"); System.out.println ("Get key301 corresponding value:" +shardedjedis.get ("Key301 ")); System.out.println ("Get key302 corresponding value:" +shardedjedis.get ("key302")); System.out.println ("============= exceeds the validity period key value pair is deleted ============="); Set the validity period of the key and store the data System.out.println ("New key303, and specify an expiration time of 2 seconds" +shardedjedis.setex ("key303", 2, "Key303-2second"); System.out.println ("Get key303 corresponding value:" +shardedjedis.get ("key303")); try{Thread.Sleep (3000); } catch (Interruptedexception e) {} System.out.println ("3 seconds later, get key303 corresponding value:" +shardedjedis.get ("Key3 03 ")); System.out.println ("============= get the original value, update to new value step complete ============="); System.out.println ("key302 Original value:" +shardedjedis.getset ("key302", "Value302-after-getset")); System.out.println ("key302 New value:" +shardedjedis.get ("key302")); System.out.println ("============= get substring ============="); System.out.println ("Gets the substring in the key302 corresponding value:" +shardedjedis.getrange ("key302", 5, 7)); } private void Listoperate () {}private void setoperate (){}private void Sortedsetoperate () {}private void Hashoperate () {}public static void Main (String[]args) {new Redisclient () . Show (); }}
Small Test Redis demo under Linux