spring整合redis-----ShardedJedisPool實現

redis是一個很是優秀的緩存框架,良好的api,強悍的性能,是如今很是很是火的緩存框架。下面來介紹一下spring中是如何整合redis的java

分析:redis

  • 須要引入依賴
  • 須要配置鏈接池,就是一個xml文件,而後參數寫在properties中
  • 須要寫一個工具類,主要方法有,從這個鏈接池中獲得redis資源,銷燬redis資源。
  • 須要再寫一個service,這個就是具體的獲得redis實例以後,保存緩存save方法,取得緩存get方法。具體是按照本身業務邏輯來實現。好比說key如何生成了,反正最後都是用的一個redis的api。這裏就簡單演示一下。

pom.xmlspring

<!-- redis -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.8.1</version>
      <type>jar</type>
    </dependency>
View Code

 

redis.xmlapi

<?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>
                <bean class="redis.clients.jedis.JedisShardInfo">
                    <constructor-arg name="host" value="${redis.host}"/>
                    <constructor-arg name="port" value="${redis.port}"/>
                    <constructor-arg name="timeout" value="${redis.timeout}"/>
                </bean>
            </list>
        </constructor-arg>
    </bean>

</beans>
View Code

redis.properties
redis.host=127.0.0.1 redis.port=6379 redis.timeout=3000
View Code

 

RedisPool.java
package com.mmall.service; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; import javax.annotation.Resource; /** * Created by 敲代碼的卡卡羅特 * on 2018/4/1 14:21. */ @Service("redisPool") @Slf4j public class RedisPool { @Resource(name = "shardedJedisPool") private ShardedJedisPool shardedJedisPool; public ShardedJedis instance() { return shardedJedisPool.getResource(); } public void safeClose(ShardedJedis shardedJedis) { try { if (shardedJedis != null) { shardedJedis.close(); } } catch (Exception e) { log.error("return redis resource exception", e); } } }
View Code

 

CacheService.java緩存

 
 
package com.mmall.service; import com.google.common.base.Joiner; import com.mmall.beans.CacheKeyConstants; import com.mmall.util.JsonMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import redis.clients.jedis.ShardedJedis; import javax.annotation.Resource; /** * Created by 敲代碼的卡卡羅特 * on 2018/4/1 14:22. */ @Service @Slf4j public class SysCacheService { @Resource(name = "redisPool") private RedisPool redisPool; //保存value
    public void saveCache(String toSavedValue, int timeoutSeconds, CacheKeyConstants prefix) { saveCache(toSavedValue, timeoutSeconds, prefix, null); } //保存value
    public void saveCache(String toSavedValue, int timeoutSeconds, 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); } } //如何生成key
    private String generateCacheKey(CacheKeyConstants prefix, String... keys) { String key = prefix.name(); if (keys != null && keys.length > 0) { key += "_" + Joiner.on("_").join(keys); } return key; } //根據key獲得value
    public String getFromCache(CacheKeyConstants prefix, String... keys) { ShardedJedis shardedJedis = null; String cacheKey = generateCacheKey(prefix, keys); try { shardedJedis = redisPool.instance(); String value = shardedJedis.get(cacheKey); return value; } 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
 
 

 

而後在application.xml中引入redis的配置文件就okapp

<import resource="redis.xml" />
相關文章
相關標籤/搜索