Redis is a very good caching framework, good API, strong performance, is now very very fire caching framework. Here's how to integrate Redis in spring
Analysis:
- Need to introduce dependency
- You need to configure the connection pool, which is an XML file, and then the parameters are written in the properties
- Need to write a tool class, the main method is to get Redis resources from this connection pool, destroy Redis resources.
- Need to write another service, this is specific to get the Redis instance, save the cache save method, get the cache get method. In particular, according to their own business logic to achieve. For example, how key is generated, anyway, the last is a Redis API. Here's a quick demo.
Pom.xml
<!--Redis-- <dependency> <groupId>redis.clients</groupId> <artifactid >jedis</artifactId> <version>2.8.1</version> <type>jar</type> </dependency>
View Code
Redis.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http ://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <context:property-placeholder location= "classpath:redis.properties"/> <bean id= "Jedispoolconfig"class= "Redis.clients.jedis.JedisPoolConfig"/> <bean id= "Shardedjedispool"class= "Redis.clients.jedis.ShardedJedisPool" scope= "singleton" > <constructor-arg index= "0" ref= "jedispoolconfig" /> <constructor-arg index= "1" > <list> <beanclass= "Redis.clients.jedis.JedisShardInfo" > <constructor-arg name= "host" value= "${redis.host}"/> <constructor-arg name= "Port" value= "${redis.port}"/> <constructor-arg name= "t Imeout "value=" ${redis.timeout} "/> </bean> </list> </constructor-arg> ; </bean></beans>
View Code
Redis.properties
redis.host=127.0.0.1redis.port=6379redis.timeout=3000
View Code
Redispool.java
PackageCom.mmall.service;Importlombok.extern.slf4j.Slf4j;ImportOrg.springframework.stereotype.Service;ImportRedis.clients.jedis.ShardedJedis;ImportRedis.clients.jedis.ShardedJedisPool;ImportJavax.annotation.Resource;/*** Created by knocking the code out of the Kaka * on 2018/4/1 14:21.*/@Service ("Redispool") @Slf4j Public classRedispool {@Resource (name= "Shardedjedispool") PrivateShardedjedispool Shardedjedispool; PublicShardedjedis Instance () {returnShardedjedispool.getresource (); } Public voidsafeclose (Shardedjedis shardedjedis) {Try { if(Shardedjedis! =NULL) {shardedjedis.close (); } } Catch(Exception e) {log.error ("Return Redis Resource Exception", E); } }}
View Code
Cacheservice.java
PackageCom.mmall.service;ImportCom.google.common.base.Joiner;Importcom.mmall.beans.CacheKeyConstants;ImportCom.mmall.util.JsonMapper;Importlombok.extern.slf4j.Slf4j;ImportOrg.springframework.stereotype.Service;ImportRedis.clients.jedis.ShardedJedis;ImportJavax.annotation.Resource;/*** Created by knocking the code out of the Kaka * on 2018/4/1 14:22.*/@Service @slf4j Public classSyscacheservice {@Resource (name= "Redispool") PrivateRedispool Redispool; //Save Value Public voidSavecache (String Tosavedvalue,inttimeoutseconds, cachekeyconstants prefix) {Savecache (tosavedvalue, timeoutseconds, prefix,NULL); } //Save Value Public voidSavecache (String Tosavedvalue,inttimeoutseconds, cachekeyconstants prefix, String ... keys) { if(Tosavedvalue = =NULL) { return; } Shardedjedis Shardedjedis=NULL; Try{String CacheKey=generatecachekey (prefix, keys); Shardedjedis=redispool.instance (); Shardedjedis.setex (CacheKey, Timeoutseconds, Tosavedvalue); } Catch(Exception e) {log.error ("Save cache exception, prefix:{}, keys:{}", Prefix.name (), jsonmapper.obj2string (keys), e); } finally{redispool.safeclose (Shardedjedis); } } //how to generate key Privatestring Generatecachekey (cachekeyconstants prefix, string ... keys) {string key=Prefix.name (); if(Keys! =NULL&& keys.length > 0) {Key+ = "_" + Joiner.on ("_")). Join (keys); } returnkey; } //get value based on key Publicstring Getfromcache (cachekeyconstants prefix, string ... keys) {Shardedjedis Shardedjedis=NULL; String CacheKey=generatecachekey (prefix, keys); Try{Shardedjedis=redispool.instance (); String value=Shardedjedis.get (CacheKey); returnvalue; } Catch(Exception e) {log.error ("Get from cache exception, prefix:{}, keys:{}", Prefix.name (), jsonmapper.obj2string (keys), e); return NULL; } finally{redispool.safeclose (Shardedjedis); } }}
View Code
The Redis configuration file is then introduced in the Application.xml to OK
<import resource= "Redis.xml"/>
Spring Integrated Redis