首页 > 代码库 > spring-cache
spring-cache
spring-cache支持将现有缓存服务器配置为基于注解的缓存,本人所用过的就是作数据层缓存;
首先,说一个踩过的坑:
不同环境的切换,将会导致每个环境的spring-cache的结果不同,从而数据不一致,其实这个表面看起来大家
都明白,但是出现坑的原因是在于预上线环境的存在,如果预上线和线上的缓存不是同一套,那么在预上线作修改就
会导致预上线改数据库了并更新缓存,但是线上的数据库与缓存数据就不一致了。
其实,说下配置方式。
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #4e9192 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #3933ff } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco } span.s1 { color: #009193 } span.s2 { color: #000000 } span.s3 { color: #932192 } span.s4 { color: #3933ff } span.s5 { color: #4e9192 } span.s6 { color: #4f76cb }</style>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=http://www.mamicode.com/"400" />
<property name="maxIdle" value=http://www.mamicode.com/"100" />
<property name="maxWaitMillis" value=http://www.mamicode.com/"1000" />
<property name="testOnBorrow" value=http://www.mamicode.com/"false" />
<property name="testWhileIdle" value="false"/>
</bean>
</property>
<property name="expires" value="604800" /><!--7 days -->
</bean>
JedisCacheManager
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #931a68 } span.s1 { color: #931a68 } span.s2 { color: #0326cc } span.s3 { color: #000000 }</style>
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();
}
路由类
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #777777 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #3933ff } span.s1 { color: #931a68 } span.s2 { color: #0326cc } span.s3 { color: #000000 } span.s4 { color: #7e504f }</style>
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类
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #931a68 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #0326cc } span.s1 { color: #931a68 } span.s2 { color: #0326cc } span.s3 { color: #000000 } span.s4 { color: #7e504f }</style>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;
}
再次,使用方法。
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #777777 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #3933ff } span.s1 { color: #931a68 } span.s2 { color: #000000 } span.s3 { color: #0326cc } span.s4 { color: #777777 } span.s5 { color: #3933ff } span.s6 { color: #7e504f }</style>@Repository
public class UserInfoDao {
@Resource
UserInfoMapper userInfoMapper;
@Cacheable(value = http://www.mamicode.com/"user_info", key = "#id")
public UserInfo getUserInfoBySid(long id) {
return userInfoMapper.getUserInfoBySid(id);
}
@CacheEvict(value = "http://www.mamicode.com/user_info", key = "#userInfo.id")
public void updateUserInfo(UserInfo userInfo) {
userInfoMapper.update(userInfo);
}
public void insertUserInfo(UserInfo userInfo) {
userInfoMapper.insert(userInfo);
}
}
最后,本周会补上实践代码到github。
spring-cache