Redis the operation of the String,list,hash,set,zset data type by the tool class. Add Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> Spring-boot-starter-data-redis</artifactid>
</dependency>
Redis Configuration
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.cache.annotation.EnableCaching;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.data.redis.connection.RedisConnectionFactory;
Import org.springframework.data.redis.core.*;
/** * Created by the Administrator on 2017/7/2. * Redis template configuration, cache configuration * * * @Configuration @EnableCaching public class Redisconfig {@Autowired redisconnectionfactory
Redisconnectionfactory; /** * Instantiated Redistemplate Object * * @Bean public redistemplate<string, object> Functiondomainredistempla
Te () {redistemplate<string, object> redistemplate = new redistemplate<string, object> ();
Initdomainredistemplate (Redistemplate, redisconnectionfactory);
return redistemplate; /** * Set data to be stored in Redis serialization mode/private void Initdomainredistemplate (redistemplate<string, object> Redistemplate, Redisconnectionfactory Factory) {/*redistemplate.setkeyserializer (new StringRedisSeri
Alizer ());
Redistemplate.setvalueserializer (New Jdkserializationredisserializer ());
Redistemplate.sethashkeyserializer (New Stringredisserializer ());
Redistemplate.sethashvalueserializer (New Jackson2jsonredisserializer (Object.class));
Redistemplate.setconnectionfactory (Factory); /** * Instantiate the Hashoperations object, you can use the Hash type operation/@Bean public hashoperations<string, String, Obje
Ct> hashoperations (redistemplate<string, object> redistemplate) {return Redistemplate.opsforhash (); /** * Instantiate Valueoperations object, you can use String operation/@Bean public valueoperations<string, Object> ;
Valueoperations (redistemplate<string, object> redistemplate) {return redistemplate.opsforvalue (); /** * Instantiate Listoperations object, you can use the List operation * * @Bean PUBlic listoperations<string, object> listoperations (redistemplate<string, Object> RedisTemplate) {Retu
RN Redistemplate.opsforlist (); /** * Instantiate Setoperations object, you can use Set operation * * @Bean public setoperations<string, object> Setope
Rations (redistemplate<string, object> redistemplate) {return redistemplate.opsforset (); /** * Instantiate Zsetoperations object, you can use Zset operation * * @Bean public zsetoperations<string, object> zse
Toperations (redistemplate<string, object> redistemplate) {return redistemplate.opsforzset (); }
}
Redis Action
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.data.redis.core.*;
Import Org.springframework.stereotype.Service;
Import Javax.annotation.Resource;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;
Import Java.util.concurrent.TimeUnit;
@Service public class Redisservice {@Autowired private redistemplate<string, object> redistemplate;
HashMap Operation @Resource Private hashoperations<string, String, object> hashoperations;
Object Operation @Resource Private valueoperations<string, object> valueoperations;
List Operation @Resource Private listoperations<string, object> listoperations;
Set Operation @Resource Private setoperations<string, object> setoperations;
Zset Operation @Resource Private zsetoperations<string, object> zsetoperations; ---------------------------------------------------------------------//Redistemplate//---------------------------------------------------------------------/** * To determine if key exists * @param key * *
public boolean Haskey (String key) {return Redistemplate.haskey (key);
/** * Delete key * @param key */public void Delete (String key) {redistemplate.delete (key); /** * To determine whether the hashkey of the specified key exists * @param key * @param hashkey * @return/public Boolean
Haskey (string key, String HashKey) {return Redistemplate.opsforhash (). Haskey (key, HashKey); /** * Set timeout time * @param key * @param timeout * @param unit/public void expire (String
Key, final long timeout, final timeunit unit) {Redistemplate.expire (key, timeout, unit); /** * Gets the expiration time * @param key * @return/Public long TTL (String key) {return Redistem
Plate.getexpire (key); /** * Gets the specified pattern's key * @param pattern * @return
* * Public set<string> keys (String pattern) {return Redistemplate.keys (pattern); /** * Delete multiple key * @param keys */public void Delete (set<string> keys) {Redistemplat
E.delete (keys); /** * Set Expiration time * @param key * @param expire/private void Setexpire (String key,long expire
{if (Expire!=-1) {Redistemplate.expire (key, expire, timeunit.seconds); }//---------------------------------------------------------------------//valueoperations-> Redis St
Ring/value operation//---------------------------------------------------------------------/** * Set Key-value value
*/public void AddValue (String key, Object Value,long expire) {Valueoperations.set (key, value);
Setexpire (Key,expire); /** * Set Key-value value, incoming time unit */public void AddValue (String key, Object value,long expire, timeunit times
Unit) { Valueoperations.set (key, value, expire, timeunit); /** * Set Key-value value, no expiration time/public void AddValue (String key, Object value) {valueoperations.
Set (key, value);
/** * Gets the value of key */public Object GetValue (String key) {return valueoperations.get (key); }//---------------------------------------------------------------------//hashoperations-> Redis Redis Hash operation//---------------------------------------------------------------------/** * Add content to Redis * param key Save Key * @param hashkey HashKey * @param data Save Object Data * @param expire expiration Time-1 : Indicates no expiration/public void Addhashvalue (String key,string hashkey, Object data, long expire) {hashoperations.
Put (key, HashKey, data);
Setexpire (Key,expire); /** * Hash Add Data * @param key key * @param map data */public void Addallhashvalue (Strin G KeY, map<string, object> Map, long expire) {Hashoperations.putall (key, MAP);
Setexpire (Key,expire); /** * Delete Hash key * @param key key * @param hashkey HashKey/public long Deletehas
Hvalue (string key, String HashKey) {return Hashoperations.delete (key, HashKey); /** * Get Data/Public Object Gethashvalue (string key, String HashKey) {return hashoperations.
Get (key, HashKey); /** * Bulk FETCH data */public list<object> Gethashallvalue (String key) {return hashoperations
. values (key); /** * Bulk FETCH the specified HashKey data/public list<object> Gethashmultivalue (String key, list<string>
Hashkeys) {return Hashoperations.multiget (key, Hashkeys);
/** * Get Hash quantity */public Long Gethashcount (String key) {return hashoperations.size (key); }
//---------------------------------------------------------------------//zsetoperations-> redis Sort Set operation//----------------------------------------------- ----------------------/** * Set Zset value/public boolean addzsetvalue (String key, Object member, Long SCO
RE) {return Zsetoperations.add (key, member, score); /** * Set Zset value/public boolean addzsetvalue (String key, Object member, double score) {return
Zsetoperations.add (key, member, score); /** * Batch Set Zset value/public long addbatchzsetvalue (String key, Set<zsetoperations.typedtuple<obje
Ct>> tuples) {return Zsetoperations.add (key, tuples); /** * Zset value/public void Inczsetvalue (string key, String member, long Delta) {Zsetoperati
Ons.incrementscore (Key, member, Delta); /** * Get zset quantity */Public long Getzsetscore (string key, String member) {Double score = Zsetope
Rations.score (key, member); if (ScorE==null) {return 0;
}else{return Score.longvalue (); /** * Gets the ranking of member members in the ordered set key.
The ordered set members are sorted by decreasing the score value (from small to large). */Public set<zsetoperations.typedtuple<object>> Getzsetrank (String key, long start, long end) {RE
Turn zsetoperations.rangewithscores (key, start, end); }//---------------------------------------------------------------------//Listoperations-> Redis List () Make//---------------------------------------------------------------------/** * Add list/Public
void Addlistvalue (String key,object list) {Listoperations.leftpush (key,list); /** * Gets the specified key corresponding to the list/public Object Getlistvalue (String key) {return Listoperations.leftpop
(key);
}//---------------------------------------------------------------------//Setoperations-> Redis Set () operation //---------------------------------------------------------------------/** * Add Set Set collection */public void Addsetvalue (String key,object list) {Setop
Erations.add (key,list); /** * Gets the specified key corresponding to set/public Object Getsetvalue (String key) {return setoperations.members ke
y);
/** * Gets and removes the value of the specified key */public Object Popsetvalue (String key) {return Setoperations.pop (key);
}
}