Pom.xml Introducing Redis Open Cache
<!--Cache--<Dependency><Groupid>org.springframework.boot</groupid> < artifactid>spring-boot-starter-cache</ artifactid> </dependency> Span class= "hljs-comment" ><!--Redis--<dependency> Span class= "Hljs-tag" ><groupid>org.springframework.boot </groupid> <artifactId> Spring-boot-starter-data-redis</artifactId> </DEPENDENCY>
Application.properties configuration file
# Redis Database index (default = 0) Spring. Redis. database=0# Redis Server address Spring. Redis. host=localhost# Redis Server Connection Port Spring. Redis. port=6379# Redis Server connection password (default is empty) spring. Redis. password=# Connection Pool Maximum number of connections (use negative values to indicate no limit) spring. Redis. Pool. max-active=8# Connection pool maximum blocking wait time (with negative values for No limit) Spring.redis. Poolmax-wait=-1# Maximum idle connection in the connection pool spring. Redis. Pool.max-idle=8# The minimum idle connection in the connection pool spring. Redis. Pool. min-idle=0# Connection time-out (ms) Spring. Redis. timeout=0
Redisservice
Import org.springframework.beans.factory.annotation.Autowired;Import org.springframework.data.redis.core.*;Import Org.springframework.stereotype.Service;Import java.io.Serializable;Import java.util.List;Import Java.util.Set;Import Java.util.concurrent.TimeUnit;/** * Created by Administrator on 2017/12/1. */@ServicePublicClassRedisservice {@AutowiredPrivate Redistemplate redistemplate;/** * Write Cache *@param key *@param value *@return */PublicBooleanSetFinal String key, Object value) {Boolean result =Falsetry {valueoperations<serializable, object> operations = Redistemplate.opsforvalue (); Operations.set (key, value) ; result =True }catch (Exception e) {e.printstacktrace ();}return result; }/** * Write Cache Set Aging time *@param key *@param value *@return */PublicBooleanSetFinal String key, Object value, Long expiretime) {Boolean result =Falsetry {valueoperations<serializable, object> operations = Redistemplate.opsforvalue (); Operations.set (key, value) ; Redistemplate.expire (Key, Expiretime, timeunit.seconds); result =True }catch (Exception e) {e.printstacktrace ();}return result; }/** * Bulk Delete the corresponding value *@param keys * *PublicvoidRemoveFinal String ... keys) {for (String Key:keys) {remove (key);}}/** * Bulk Delete key *@param pattern */PublicvoidRemovepattern (Final String pattern) {set<serializable> keys = Redistemplate.keys (pattern);if (Keys.size () >0) Redistemplate.delete (keys); }/** * Delete the corresponding value *@param key */PublicvoidRemoveFinal String Key) {if (exists (key)) {Redistemplate.delete (key);}}/** * Determine if there is a corresponding value in the cache *@param key *@return */PublicBooleanExistsFinal String Key) {return Redistemplate.haskey (key); }/** * Read Cache *@param key *@return */Public ObjectGetFinal String key) {Object result =Null Valueoperations<serializable, object> operations = Redistemplate.opsforvalue (); result = Operations.get (key);return result; }/** * Hash Add *@param key *@param HashKey *@param value */PublicvoidHmset (String key, Object HashKey, Object value) {hashoperations<string, object, object> hash = redistemplate.opsfor Hash (); Hash.put (Key,hashkey,value); }/** * Hash Fetch data *@param key *@param HashKey *@return */Public ObjectHmget (String key, Object HashKey) {hashoperations<string, object, object> hash = Redistemplate.opsforhash ();Return Hash.get (Key,hashkey); }/** * List Add *@param k *@param v */PublicvoidLpush (String K,object v) {listoperations<string, object> list = Redistemplate.opsforlist (); List.rightpush (k,v);}/** * List Get *@param k *@param l *@param L1 *@return */Public list<object>Lrange (String K,Long L,Long L1) {listoperations<string, object> list = Redistemplate.opsforlist ();Return List.range (K,L,L1); }/** * Collection Add *@param key *@param value */PublicvoidAdd (String key,object value) {setoperations<string, object> set = Redistemplate.opsforset (); Set.add (Key,value);}/** * Collection Get *@param key *@return */Public set<object>Setmembers (String key) {setoperations<string, object> set = Redistemplate.opsforset ();return Set.members (key); }/** * Ordered Collection Add *@param key *@param value *@param scoure * /public void Zadd (String key,object value,double scoure) {zsetoperations<string, object> Zset = Redistemplate.opsforzset (); Zset.add (key,value,scoure); } /** * @param key * @param scoure * @param scoure1 * @return * */Public Set<object> ; Rangebyscore (String key,double scoure,double scoure1) {zsetoperations<string, object> zset = Redistemplate.opsforzset (); return Zset.rangebyscore (Key, Scoure, Scoure1);}}
Controller used for testing
ImportCom. Example. Service. Redisservice; Import org. springframework. Beans. Factory. annotation. Autowired; Import org. springframework. Web. bind. annotation. Requestmapping; Import org. springframework.web.bind;import org.springframework.web.bind.annotation "/test", method = Requestmethod.set ( "1", "value22222") ;}
Springboot+redis Configuration and use