Spring Cache注解+Redis(二)
之前有写过一篇Spring Cache注解+Redis
今天对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查看效果如下图:

中间遇到的问题: 插入的hash类型的key前面会有一堆的\xac\xed\x00\x05t\x00\tb 这种东西
处理方法:redisTemplate中添加<property name="keySerializer" ref="stringRedisSerializer"/>等