spring-data-redis简介

1、简述
Redis是一种nosql数据库,在开发中常用做缓存。Jedis是Redis在Java中的redis- client。spring把专门的数据操作独立封装在spring-data系列中,spring-data-redis自然是针对Redis的独立封装了。

2、使用
2.1 pom.xml

 
<dependency>  
    <groupId>redis.clientsgroupId>  
    <artifactId>jedisartifactId>  
    <version>2.8.2version>  
    <type>jartype>  
    <scope>compilescope>  
dependency>  

  
<dependency>  
    <groupId>org.springframework.datagroupId>  
    <artifactId>spring-data-redisartifactId>  
    <version>1.6.4.RELEASEversion>  
dependency>  

2.2 redis.properties配置

#最大分配的对象数
redis.pool.maxTotal=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWaitMillis=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true
#连接密码
redis.password=
#IP
redis.ip=xxxx
#Port
redis.port=6379

2.3 spring-redis.xml配置


    <context:component-scan base-package="com.xxxx" />
    
    <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>

    
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.pool.maxTotal}" />
        <property name="maxIdle" value="${redis.pool.maxIdle}" />
        <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
    bean>

    
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.ip}" />
        <property name="port" value="${redis.port}" />
        
        <property name="poolConfig" ref="jedisPoolConfig" />
    bean> 

    
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
    bean>

redisTemplate已经基本配置完成。接下来可以创建一个Goods类,必须实现或者间接实现Serializable接口。

Redis存储对象是使用序列化,spring-data-redis已经将序列化的功能内置,不需要我们去管,我们只需要调用api就可以使用。SerialVersionUID字段对序列化扩展有用,为了以后扩展或者缩减字段时不会造成反序列化出错。

2.4 创建redis的增删改查操作

@Component
public class RedisCache {

    public final static String CAHCENAME="cache";//缓存名
    public final static int CAHCETIME=60;//默认缓存时间

    //自动注入RedisTemplate
    @Autowired
    private RedisTemplate redisTemplate;

    public  boolean putCache(String key, T obj) {
        final byte[] bkey = key.getBytes();
        final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
        boolean result = redisTemplate.execute(new RedisCallback() {
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.setNX(bkey, bvalue);
            }
        });
        return result;
    }
    public  boolean setCache(String key, T obj) {
        final byte[] bkey = key.getBytes();
        final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
        boolean result = redisTemplate.execute(new RedisCallback() {
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                connection.set(bkey, bvalue);
                return true;
            }
        });
        return result;
    }

    public  void putCacheWithExpireTime(String key, T obj, final long expireTime) {
        final byte[] bkey = key.getBytes();
        final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
        redisTemplate.execute(new RedisCallback() {
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                connection.setEx(bkey, expireTime, bvalue);
                return true;
            }
        });
    }


    public  boolean putListCache(String key, List objList) {
        final byte[] bkey = key.getBytes();
        final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList);
        boolean result = redisTemplate.execute(new RedisCallback() {
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.setNX(bkey, bvalue);
            }
        });
        return result;
    }

    public  boolean putListCacheWithExpireTime(String key, List objList, final long expireTime) {
        final byte[] bkey = key.getBytes();
        final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList);
        boolean result = redisTemplate.execute(new RedisCallback() {
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                connection.setEx(bkey, expireTime, bvalue);
                return true;
            }
        });
        return result;
    }

    public  T getCache(final String key, Class targetClass) {
        byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() {
            @Override
            public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.get(key.getBytes());
            }
        });
        if (result == null) {
            return null;
        }
        return ProtoStuffSerializerUtil.deserialize(result, targetClass);
    }

    public  List getListCache(final String key, Class targetClass) {
        byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() {
            @Override
            public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.get(key.getBytes());
            }
        });
        if (result == null) {
            return null;
        }
        return ProtoStuffSerializerUtil.deserializeList(result, targetClass);
    }

    /** * 精确删除key * * @param key */
    public void deleteCache(String key) {
        redisTemplate.delete(key);
    }

    /** * 模糊删除key * * @param pattern */
    public void deleteCacheWithPattern(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    /** * 清空所有缓存 */
    public void clearCache() {
        deleteCacheWithPattern(RedisCache.CAHCENAME+"|*");
    }
}

2.5 get和set操作示例

@Service("goodsService")
public class GoodsServiceImpl implements IGoodsService {
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    @Resource
    private IElasticService elasticService;
    @Autowired
    private RedisCache cache;
    @Value("${searchUrl}")
    private String searchUrl;


    @Override
    public List getGoodsList(int offset, int limit) {

        String cacheKey = RedisCache.CAHCENAME + "|getGoodsList|" + offset + "|" + limit;
        //先去缓存中取
        List resultCache = cache.getListCache(cacheKey, Goods.class);
        if (resultCache == null) {
            //缓存中没有再去数据库取,并插入缓存(缓存时间为60秒)
            resultCache = elasticService.getGoodsList(offset, limit);
            cache.putListCacheWithExpireTime(cacheKey, resultCache, RedisCache.CAHCETIME);
            LOG.info("put cache with key:" + cacheKey);
        } else {
            LOG.info("get cache with key:" + cacheKey);
        }
        return resultCache;
    }



    @Override
    public ReportResultSimple searchGoods(String queryData) {
        ReportResultSimple result = new ReportResultSimple();

        try {

            JsonParser jsonParser = new ObjectMapper().readValue(queryData, new TypeReference() {
            });
            LOG.info("query begin, query is:[ " + jsonParser.toString() + " ]");
            String cacheKey = RedisCache.CAHCENAME + "|" + queryData;
            //暂时只对没有search的查询条件做缓存
            if (jsonParser.getQuerys() == null) {
                //先去缓存中取
                result = cache.getCache(cacheKey, ReportResultSimple.class);
            }
            if (result == null || jsonParser.getQuerys() != null) {
                //缓存没有结果,从elasticsearch查询
                //
                if (jsonParser.getQuerys() == null) {
                    cache.putCacheWithExpireTime(cacheKey, result, RedisCache.CAHCETIME);
                }
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            result.setFlag("false");
            result.setMsg(e.getMessage());
        }
        return result;
    }
}

你可能感兴趣的:(Redis)