springcache

在springcache之前

在没有使springcache之前我们使用缓存的方式是这样的:

 Map> keyListHashMap = Maps.newHashMap();
        DateTime dt = new DateTime().withHourOfDay(2).plusDays(1);
        DateTime now = DateTime.now();
        int toTwo = (int) (dt.getMillis() - now.getMillis()) / 1000;
        List resList = Lists.newArrayList();
        for (Integer cinemaId : cinemaIds) {
            StoreKey storeKey = new StoreKey(CacheConstants.GDATA_STRING, String.format(CacheConstants.GATEWAY_SPECIALS_BY_CINEMAID, cinemaId));
            keyListHashMap.put(storeKey, resList);
            returnTocMap.put(cinemaId, transToTSpecialHallToApp(resList));
        }
        redisStoreClient.multiSet(keyListHashMap, toTwo);

这段代码我刚开始看的时候也觉得没问题,还觉得写的挺好的(因为是我写的偷笑),等我看到了springcache之后我觉得这段代码就不是那么的优雅了,总体来说就是代码的处理逻辑和缓存耦合在一起,降低了代码的可读性,不利于后期人员的维护;还有就是缓存切换的话成本很大,需要改动的地方比较多,风险成本比较高。

在springcache之后
springcache是什么?

首先,要明白springcache是一种解决缓存的方案,而不是一种具体的缓存方式(类似的Redis,memcache,ehcache等等)。

springcache的相关配置

pom配置:
这里需要注意spring应该是3.1+


            net.sf.ehcache
            ehcache
            2.8.3
        

        
            org.springframework
            spring-context-support
            4.1.1.RELEASE
        

springcache相关xml配置

 
    
    
    
        
    
    
    
        
    

还需要配置具体的缓存:



    
    
    
    

springcache的相关代码实现
package com.yuxi.cache;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * Created by yuxi on 2017/9/28.
 */
@Service("cacheService")
public class CacheService {

    @Cacheable(value = "dbCache", key = "'info_' + #id")
    public Info getInfo(Integer id) {
        System.out.println("from cache");
        return getDb(id);
    }

    private Info getDb(Integer id) {
        System.out.println("from db" + id);
        return new Info(id);
    }


    @CachePut(value = "dbCache", key = "'info_' + #id")
    public Info getInfo2(Integer id) {
        System.out.println("from cache put");
        return getDb(id);
    }


    @CacheEvict(value = "dbCache", key = "'info_' + #id")
    public boolean delete(Integer id) {
        System.out.println("delete");
        return true;
    }
}

相关测试类

package com.yuxi.main;

import com.yuxi.cache.CacheService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by yuxi on 2017/8/5.
 */
public class KnightMain {
    public static void main(String[] args) {
        // spring 应用上下文
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("knight.xml");
   
        CacheService cacheService = (CacheService) applicationContext.getBean("cacheService");
        cacheService.getInfo(1);
        cacheService.getInfo(1);
        cacheService.getInfo(1);
        cacheService.getInfo(1);
        cacheService.getInfo(1);

        cacheService.getInfo2(1);
        cacheService.getInfo2(1);
        cacheService.getInfo2(1);
        cacheService.delete(1);
    }
}

相关的测试结果为:

from cache
from db1
from cache put
from db1
from cache put
from db1
from cache put
from db1
delete
springcache的相关注解说明:

三图胜过万言,图片来自郭老师的wiki,不要问我郭老师是谁?他是一个传说。

springcache_第1张图片
郭老师的wiki
springcache_第2张图片
郭老师的wiki
springcache_第3张图片
郭老师的wiki

相关代码可以参考:springcache

你可能感兴趣的:(springcache)