在一個Service裡面的方法
@Transactional(readOnly = true)
@Cacheable(value = "users", key = "#id")
public User findUser(Integer id) {
return this.em.find(User.class, id);
}
有時會發現 緩存是緩存了,但是為啥還有事務的打開和關閉操作呢??
因為, 沒有指定這兩個攔截器的順序!!導致Tx的攔截器先發揮作用了
<tx:annotation-driven order="2" />
<cache:annotation-driven order="1" />
還有一個地方需要注意, 當需要使用keys("*")查詢鍵時,鍵的序列化應使用String序列化器
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="localhost" p:port="6379" p:use-pool="true" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"
p:keySerializer-ref="stringRedisSerializer"
p:hashKeySerializer-ref="stringRedisSerializer"/>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
否則,查詢不到