I have written a Spring Cache annotation + Redis before
Today's optimization of Cache+Redis configuration.
The home page is still a dependency of Jar, please see the previous article, and I will not repeat it here.
Then post XML configuration:
<?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"/> <!-- Enable cache annotations --> <cache:annotation-driven cache-manager="cacheManager"/> <!--Redis Cache configuration--> <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> <constructor-arg name="redisOperations" ref="redisTemplate"/> </bean> <!-- redis related configuration--> <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>
Please see the previous article for redis.properties;
Cache annotations use:
Annotate the service implementation method
@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); }
Use the redis client RedisDesktopManager to view the effect as shown below:
The problem encountered in the middle: There will be a bunch of \xac\xed\x00\x05t\x00\tb in front of the inserted hash type key
Processing method: add <property name="keySerializer" ref="stringRedisSerializer"/> etc. to redisTemplate