1, the system integrates the Redis service. Redis configuration in the properties file:
redis.host=127.0.0.1
redis.port=6379
redis.default.db=0
redis.timeout=100000
redis.maxactive=
redis.maxidle=100
redis.maxwait=1000
redis.testonborrow=true
Spring consolidates the configuration of Redis (it is definitely omitted here to import the properties file):
<!--Jedis Pool configuration--<bean id= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig"
; <property name= "Maxidle" value= "${redis.maxidle}"/> <property name= "Testonborrow" value= "${redis.t Estonborrow} "/> </bean> <!--spring Data Redis---<bean id=" Jedisconnect Ionfactory "class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "> <property
Name= "Usepool" value= "true" ></property> <property name= "HostName" value= "${redis.host}"/> <property name= "Port" value= "${redis.port}"/> <property name= "Timeout" value= "${redis.
Timeout} "/> <property name=" database "value=" ${redis.default.db} "></property> <constructor-arg index= "0" ref= "jedispoolconfig"/> </bean> <bean id= "Redistemplate" cl ass= "org. Springframework.data.redis.core.StringRedisTemplate "p:connectionfactory-ref=" Jedisconnectionfactory "P:key serializer-ref= "Stringredisserializer" > </bean>
2, custom rediscache implement self-org.springframework.cache.Cache interface
Define member variable name, define member variable Redisclint (this member variable is an object that directly operates the Redis class)
Override the Getnativecache,get,put,evict,clear method (override these methods in order to use Redis to store the cache, while using your own rules to name keys also convenient for log viewing)
Public Object Getnativecache () {return jedisclient;
} public void put (object key, Object value) {//logger.info ("Deposit---put------------key:" +key); Modify the value of key, bind string _key=this.name+ "_" +string.valueof (key) with name,//key must be a string type//logger.
Info ("deposit cache, corresponding to Redis key:" +_key);
Depositing data into Redis if (null!=value) {Serializable val= (Serializable) Value;//value must be of type Serializable
Jedisclient.set (_key.getbytes (), Serializeutil.serialize (Val)); }} public Valuewrapper get (Object key) {//logger.info ("Get Cache---valuewrapper------------key:"
+key); Modify the value of key, bind string _key=this.name+ "_" +string.valueof (key) with name,//key must be of type string//logger.info ("
cache, corresponding to Redis key: "+_key);
Valuewrapper result = null;
Get data from Redis byte[] V=jedisclient.get (_key.getbytes ()); Serializable Val=serializeutil.unserialize (v);
Object thevalue = val;
if (thevalue! = null) {result = new Simplevaluewrapper (thevalue);
} return result;
} public void evict (Object key) {//logger.info ("Delete cache---evict------------key:" +key); Modify the value of key, bind string _key=this.name+ "_" +string.valueof (key) with name,//key must be of type string//logger.info ("delete
In addition to the cache, corresponding to Redis key: "+_key);
Remove Jedisclient.keydel (_key.getbytes ()) from Redis; } public void Clear () {///clear cache, you need to blur the collection of key values in Redis based on the name property of the cache, and remove all String pattern
=this.name+ "*";//Prefix match//logger.info ("clear Cache---clear------------key pattern:" +pattern);
Set<byte[]> ks= Jedisclient.keys (Pattern.getbytes ());
For (byte[] bys:ks) {//logger.info ("clear Cache-redis Delete key:" +new String (bys)); This.jedisclient.kEydel (bys); }
}
3, custom CacheManager inheritance Org.springframework.cache.support.AbstractCacheManagerCreate member variable private Collection
public void Setcaches (collection<? extends rediscache> caches) {
this.caches = caches;
}
Override Loadcaches to load a custom cache object caches
protected collection<? Extends Rediscache> loadcaches () {
return caches;
}
The 4,spring configuration file is configured as follows:
<!--Database Cache manager
--<cache:annotation-driven cache-manager= "CacheManager"/>
<!--cache classes and cache definitions-- >
<bean id= "CacheManager" class= "Custom CacheManager Package" >
<property name= "caches" >
<set >
<!--Test Cache class--
<!--the
P:name property is probably the same as the name in <property name= "name" ref= "Rediscache" >< /property> is the
Name property injected into the Rediscache class-
-
<bean class= "Custom Rediscache package" P:name= " Rediscache in the name ">
<property name=" jedisclient "ref=" jedisclient "></property>
</bean >
</set>
</property>
5, use annotations on methods that require caching
@Cacheable (value= "name in Rediscache", key= "custom key rule ensures that each data is unique", condition (optional attribute) = "") indicates that the return value of the method needs to be cached (in the key attribute you can use the # parameter to ensure that the unique , while the condition attribute can be judged by # parameters and Conditions)
@CacheEvict (name in value= Rediscache, allentries=true) The annotation is to indicate that the method needs to clear the cache, and allentries=true to clear all caches corresponding to that value