码迷,mamicode.com
首页 > 编程语言 > 详细

spring-cache

时间:2017-04-13 09:07:26      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:logger   tor   height   数据不一致   oid   切换   5.0   ogg   out   

    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

标签:logger   tor   height   数据不一致   oid   切换   5.0   ogg   out   

原文地址:http://www.cnblogs.com/hawk-whu/p/6702128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!