Installing Redis
Http://www.runoob.com/redis/redis-install.html
<!--Redis Rack Pack--
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactid >jedis</artifactId>
<version>2.9.0</version>
</dependency>
Redisutil Tool Class:
Package com.utils;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;
Import Java.util.concurrent.TimeUnit;
Import javax.annotation.PostConstruct;
Import Org.springframework.data.redis.core.RedisTemplate;
Import Org.springframework.util.CollectionUtils; /** * * @author * Spring and Redis-based Redistemplate tool class * For all hashes is a method starting with H * For all sets are the method that begins with S No general method * For all lists are the methods that begin with L */public class Redisutil {private static redistemplate<string, object>
Redistemplate;
Private Redisutil utils;
@SuppressWarnings ("static-access") @PostConstruct public void, init () {utils = this;
Utils.redistemplate = this.redistemplate; } @SuppressWarnings ("static-access") public void Setredistemplate (redistemplate<string, object> Redistempla
TE) {this.redistemplate = redistemplate; }//=============================common============================/** *Specify cache Expiration Time * @param key * @param time (seconds) * @return */public static Boolean expire (String Key,long time) {try {if (time>0) {Redistemplate.expire (key, Time, TIMEUNIT.S
Econds);
} return true;
} catch (Exception e) {e.printstacktrace ();
return false; }}/** * Gets expiration time based on key * @param key cannot be null * @return time (seconds) returned 0 is permanent valid */pub
Lic Static long Getexpire (String key) {return redistemplate.getexpire (key,timeunit.seconds); }/** * Determines if key exists * @param key key * @return True exists false does not exist */public static Boolean
Haskey (String key) {try {return Redistemplate.haskey (key);
} catch (Exception e) {e.printstacktrace ();
return false; }}/** * Delete cache * @param keyYou can pass a value or multiple */@SuppressWarnings ("unchecked") public static void Del (String ... key) {if (key!=n
ull&&key.length>0) {if (key.length==1) {redistemplate.delete (key[0]);
}else{Redistemplate.delete (Collectionutils.arraytolist (key));
}}}//============================string=============================/** * Normal cache Fetch * @param key * @return value */public static Object get (String key) {return Key==null?null:
Redistemplate.opsforvalue (). get (key); }/** * Normal cache put * @param key * @param value * @return True Success false failed */Publ IC Static Boolean set (String key,object value) {try {redistemplate.opsforvalue (). Set (Key, Valu
e);
return true;
} catch (Exception e) {e.printstacktrace (); Return FalsE }}/** * Normal cache put and set time * @param key key * @param value * @param time (seconds) to be greater than 0 Fruit time less than or equal to 0 will be set indefinitely * @return True Success false failed */public static Boolean set (String key,object Value,long ti Me) {try {if (time>0) {Redistemplate.opsforvalue (). Set (key, value, time, time
Unit.seconds);
}else{set (key, value);
} return true;
} catch (Exception e) {e.printstacktrace ();
return false; }}/** * increment * @param key * @param by to add a few (greater than 0) * @return */Public STA
Tic Long incr (String key, long Delta) {if (delta<0) {throw new RuntimeException ("increment factor must be greater than 0");
} return Redistemplate.opsforvalue (). Increment (key, delta); }/** * decrements * @param key * @param by to be reduced by a few (less than0) * @return */public static long DECR (String key, long Delta) {if (delta<0) {
throw new RuntimeException ("decrement factor must be greater than 0");
} return Redistemplate.opsforvalue (). Increment (key,-delta); }//================================map=================================/** * hashget * @param Key key cannot be NULL * @param item item cannot be NULL * @return value */public static Object Hget (String key,string
Item) {return Redistemplate.opsforhash (). Get (key, item); /** * Get all key values for HashKey * @param key key * @return corresponding multiple key values */public static map<obj
Ect,object> hmget (String key) {return Redistemplate.opsforhash (). Entries (key);
}/** * HashSet * @param key * @param map corresponds to multiple key values * @return True Success false failed */ public static Boolean Hmset (String key, map<string,object> Map) {Try {Redistemplate.opsforhash (). Putall (key, map);
return true;
} catch (Exception e) {e.printstacktrace ();
return false;
}}/** * HashSet and set time * @param key * @param map corresponds to multiple key values * @param time (seconds)
* @return True Success false failed */public static Boolean Hmset (String key, map<string,object> Map, long time) {
try {redistemplate.opsforhash (). Putall (key, map);
if (time>0) {expire (key, time);
} return true;
} catch (Exception e) {e.printstacktrace ();
return false;
}}/** * Put data into a hash table, if not present will create * @param key * @param Item Item * @param value value * @return True Success false failed */public static Boolean Hset (String key,string item,object value) {TR y {RedIstemplate.opsforhash (). Put (key, item, value);
return true;
} catch (Exception e) {e.printstacktrace ();
return false;
}}/** * Put data into a hash table, if not present will create * @param key * @param Item Item * @param value value * @param time (seconds) Note: If the existing hash table has time, this will replace the original time * @return True Success false failed */public static Boolean Hset (String key,string item,object value,long time) {try {Redistemplate.opsforhash (). Put (Key, I
TEM, value);
if (time>0) {expire (key, time);
} return true;
} catch (Exception e) {e.printstacktrace ();
return false; }}/** * Delete the value in the hash table * @param key cannot be null * @param item item can make multiple cannot be null */PU Blic static void Hdel (String key, Object ... item) {Redistemplate.opsforhash (). Delete (key,iTEM); }/** * Determine if there is a value for the key in the hash table * @param key cannot be null * @param item item cannot be NULL * @return true exists f Alse does not exist */public static Boolean Hhaskey (string key, string item) {return Redistemplate.opsforhash ()
. Haskey (key, item);
}/** * Hash increment if it does not exist, it will create one and return the new value * @param key * @param Item Item * @param by to add a few (more than 0) * @return */public static double hincr (string key, String item,double by) {return Redistem
Plate.opsforhash (). Increment (Key, item, by); }/** * Hash decrement * @param key * @param Item Item * @param by to reduce (less than 0) * @return * /public static double HDECR (string key, String item,double by) {return Redistemplate.opsforhash (). incre
ment (key, item,-by); //============================set=============================/** * Gets all the values in the set according to key * @param
Key Key * @return */public static set<object> Sget (String key) {try {return Redistemplate.opsforset ()
. Members (key);
} catch (Exception e) {e.printstacktrace ();
return null; }}/** * Query from a set based on value, existence of * @param key key * @param value value * @return true exists FAL SE does not exist */public static Boolean Shaskey (String key,object value) {try {return Redistem
Plate.opsforset (). IsMember (key, value);
} catch (Exception e) {e.printstacktrace ();
return false;
}}/** * Put data into the set cache * @param key * @param values can be multiple * @return number of successes */ public static long SSet (String key, object...values) {try {return Redistemplate.opsforset ()
. Add (key, values);
} catch (Exception e) {e.printstacktrace ();
return 0;}}/** * Put set data into cache * @param key * @param time (seconds) * @param values can be multiple
* @return Number of successes */public static long Ssetandtime (String key,long time,object...values) {try {
Long count = Redistemplate.opsforset (). Add (key, values);
if (time>0) Expire (key, time);
return count;
} catch (Exception e) {e.printstacktrace ();
return 0; }}/** * Gets the length of the set cache * @param key * @return * */public static long Sgetsetsize (String key)
{try {return Redistemplate.opsforset (). Size (key);
} catch (Exception e) {e.printstacktrace ();
return 0;
}}/** * Remove the value of the * @param key * @param values can be multiple * @return removed number * *
public static long Setremove (String key, Object ... values) { try {Long count = Redistemplate.opsforset (). Remove (key, values);
return count;
} catch (Exception e) {e.printstacktrace ();
return 0;
}}//===============================list=================================/** * Gets the contents of the list cache * @param key * @param start starts * @param end end 0 to 1 means all values * @return */public static L Ist<object> LGet (String key,long start, long end) {try {return redistemplate.opsforlist (). R
Ange (key, start, end);
} catch (Exception e) {e.printstacktrace ();
return null; }}/** * Gets the length of the list cache * @param key * @return * */public static long Lgetlistsi
Ze (String key) {try {return redistemplate.opsforlist (). Size (key);
} catch (Exception e) {e.printstacktrace (); return 0; }}/** * Gets the value in list by index * @param key key * @param index index index>=0, 0 table header, 1 second element, and so on;
index<0, 1, footer, 2 the penultimate element, and so on * @return */public static Object Lgetindex (String Key,long index) {
try {return redistemplate.opsforlist (). Index (key, index);
} catch (Exception e) {e.printstacktrace ();
return null; }}/** * put list in cache * @param key * @param value * @param time (seconds) * @retu RN */public static Boolean LSet (String key, Object value) {try {REDISTEMPLATE.OPSFO
RList (). Rightpush (key, value);
return true;
} catch (Exception e) {e.printstacktrace ();
return false; }}/** * put list in cache * @param key * @param value * @param time (seconds) * @retu
RN */ public static Boolean LSet (String key, Object value, long time) {try {redistemplate.opsforlist
(). Rightpush (key, value);
if (Time > 0) Expire (key, time);
return true;
} catch (Exception e) {e.printstacktrace ();
return false; }}/** * put list in cache * @param key * @param value * @param time (seconds) * @retu RN */public static Boolean LSet (String key, list<object> value) {try {Rediste
Mplate.opsforlist (). Rightpushall (key, value);
return true;
} catch (Exception e) {e.printstacktrace ();
return false; }}/** * put list in cache * @param key * @param value * @param time (seconds) * @retu
RN */public static Boolean LSet (String key, list<object> value, long time) {try { Redistemplate.opsforlist (). Rightpushall (key, value);
if (Time > 0) Expire (key, time);
return true;
} catch (Exception e) {e.printstacktrace ();
return false; }}/** * Modifies a data in list by index * @param key key * @param index Index * @param value *
@return */public static Boolean Lupdateindex (String key, long Index,object value) {try {
Redistemplate.opsforlist (). Set (key, index, value);
return true;
} catch (Exception e) {e.printstacktrace ();
return false; }}/** * Remove n values as value * @param key * @param count remove number of * @param value * Number of @return removed */public static long Lremove (String key,long cou