Spring Cache 使用的注意點

在一個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"/>	

否則,查詢不到




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值