spring-cache
Posted 鹰搏长空08
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-cache相关的知识,希望对你有一定的参考价值。
spring-cache支持将现有缓存服务器配置为基于注解的缓存,本人所用过的就是作数据层缓存;
首先,说一个踩过的坑:
不同环境的切换,将会导致每个环境的spring-cache的结果不同,从而数据不一致,其实这个表面看起来大家
都明白,但是出现坑的原因是在于预上线环境的存在,如果预上线和线上的缓存不是同一套,那么在预上线作修改就
会导致预上线改数据库了并更新缓存,但是线上的数据库与缓存数据就不一致了。
其实,说下配置方式。
spring-cache.xml
<cache:annotation-driven cache-manager="jedisCacheManager" />
<bean id="jedisCacheManager" class="wdm.yong.spring.jedis.JedisCacheManager">
<property name="cacheStoreJedisHashRouter">
<bean class="wdm.yong.cache.spring.jedis.CacheStoreJedisHashRouter" />
</property>
<property name="serializer">
<bean class="wdm.yong.cache.spring.jedis.JsonSerializer" />
</property>
<property name="config">
<bean class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
<property name="maxTotal" value="400" />
<property name="maxIdle" value="100" />
<property name="maxWaitMillis" value="1000" />
<property name="testOnBorrow" value="false" />
<property name="testWhileIdle" value="false"/>
</bean>
</property>
<property name="expires" value="604800" /><!--7 days -->
</bean>
JedisCacheManager
public class JedisCacheManager extends AbstractCacheManager {
private static final Logger logger = LoggerFactory.getLogger(JedisCacheManager.class);
private List<Cache> cacheList;
private List<JedisPool> jedisPoolList;
private Map<String, String> namedClients;
private int expires;
private Serializer serializer;
private CacheStoreJedisHashRouter cacheStoreJedisHashRouter;
private GenericObjectPoolConfig config = new GenericObjectPoolConfig();
}
路由类
public class CacheStoreJedisHashRouter implements CacheStoreRouter<JedisPool> {
private static final Logger logger = LoggerFactory.getLogger(CacheStoreJedisHashRouter.class);
@Override
public JedisPool pickUp(List<JedisPool> cacheStores, String cacheName, Object key) {
int hashCode = new StringBuilder().append(cacheName).append(String.valueOf(key)).toString().hashCode();
logger.debug("cacheName={}, key={}, hashCode={}", new Object[]{cacheName, key, hashCode});
return cacheStores.get(Math.abs(hashCode) % cacheStores.size());
}
}
JedisCache类
public class JedisCache implements Cache {
private static final Logger logger = LoggerFactory.getLogger(JedisCache.class);
private String name;
private List<JedisPool> jedisPoolList;
private CacheStoreRouter<JedisPool> cacheStoreRouter;
private Serializer serializer;
private int expires;
public JedisCache(String name, List<JedisPool> jedisList, CacheStoreRouter<JedisPool> cacheStoreRouter, Serializer serializer, int expires) {
this.name = name;
this.jedisPoolList = jedisList;
this.cacheStoreRouter = cacheStoreRouter;
this.serializer = serializer;
this.expires = expires;
}
// 实现get、put、evict、clear;
}
再次,使用方法。
@Repository
public class UserInfoDao {
@Resource
UserInfoMapper userInfoMapper;
@Cacheable(value = "user_info", key = "#id")
public UserInfo getUserInfoBySid(long id) {
return userInfoMapper.getUserInfoBySid(id);
}
@CacheEvict(value = "user_info", key = "#userInfo.id")
public void updateUserInfo(UserInfo userInfo) {
userInfoMapper.update(userInfo);
}
public void insertUserInfo(UserInfo userInfo) {
userInfoMapper.insert(userInfo);
}
}
最后,本周会补上实践代码到github。
以上是关于spring-cache的主要内容,如果未能解决你的问题,请参考以下文章