---
title: "Spring Cache注解+Redis（二）"
author: "王亚龙"
date: "2017-08-25T15:41:58Z"
tags: []
categories: ["编程语言"]
slug: "spring-cache-redis"
draft: false
lastmod: "2017-08-25T15:37:02Z"
id: 2391122
url: "https://memoryforwyl.iteye.com/blog/2391122"
---

之前有写过一篇[Spring Cache注解+Redis](/blog/2331407)

今天对Cache+Redis配置的优化。

 

首页还是Jar的依赖，请看之前的文章，这里不做赘述。

然后后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"
       xmlns:cache="http://www.springframework.org/schema/cache"
       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
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"
       default-autowire="byName">

    <context:property-placeholder
            location="classpath*:config/redis.properties" ignore-unresolvable="true"/>

    <!-- 启用缓存注解功能 -->
    <cache:annotation-driven cache-manager="cacheManager"/>

    <!--Redis Cache 配置-->
    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg name="redisOperations" ref="redisTemplate"/>
    </bean>

    <!-- redis 相关配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxWaitMillis" value="${redis.maxWait}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
    </bean>

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="poolConfig" ref="poolConfig"/>
        <property name="port" value="${redis.port}"/>
        <property name="hostName" value="${redis.host}"/>
        <property name="timeout" value="${redis.timeout}"/>
        <property name="password" value="${redis.pwd}"/>
        <property name="database" value="${redis.database}"/>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <!--<property name="defaultSerializer" ref="stringRedisSerializer"/>-->
        <property name="keySerializer" ref="stringRedisSerializer"/>
        <!--<property name="valueSerializer" ref="stringRedisSerializer"/>-->
        <property name="hashKeySerializer" ref="stringRedisSerializer"/>
        <!--<property name="hashValueSerializer" ref="stringRedisSerializer"/>-->
    </bean>

    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

</beans>
```

 redis.properties请看上一篇文章；

 

 

缓存注解使用：

在service实现方法上加注解

 

```
    @Override
    @Cacheable(value = "ShardingTableCache", key = "'shardingTableName:' + #appId + ':' + #appUserId")
    public Integer getShardingTableName(String appId, String appUserId) {
        return mapper.getShardingTableName(appId, appUserId);
    }

    @Override
    @CacheEvict(value = "ShardingTableCache", key = "'shardingTableName:' + #appId + ':' + #appUserId")
    public int updateShardingTable(String appId, String appUserId, int shardingTable) {
        return mapper.updateShardingTable(appId, appUserId, shardingTable);
    }
```

 

 

使用redis客户端RedisDesktopManager查看效果如下图：

![](http://dl2.iteye.com/upload/attachment/0126/6909/a67979b6-82a6-312f-acfc-90d49a155f51.png)  
 

中间遇到的问题： 插入的hash类型的key前面会有一堆的\\xac\\xed\\x00\\x05t\\x00\\tb 这种东西

处理方法：redisTemplate中添加&lt;property name="keySerializer" ref="stringRedisSerializer"/&gt;等