Spring中使用Ehcache的方法和注意事项

 1 如何调用方法数据增加缓存
 2 @Cacheable(value="MY_CACHE", key="'cache_business_' + #business_id")
 3 public ShopCacheData loadShopData(long business_id) throws SQLException{
 4         ...
 5         return scd;
 6 }
 7 
 8 如何通过调用清空缓存
 9 @CacheEvict(value="MY_CACHE",  key="'cache_business_' + #business_id")        //allEntries=true
10 public void cleanBusinessFromCache(long business_id) throws SQLException {
11        return;
12 }
13 
14 上面使用Cacheable注释的方式,如果在同一个Class中调用则缓存无效。
15 
16 如:
17 
18 public class ShopManager{
19 
20 @Cacheable(value="MY_CACHE", key="'cache_business_' + #business_id")
21 public ShopCacheData loadShopData(long business_id) throws SQLException{
22         ...
23         return scd;
24 }
25 
26 如何通过调用清空缓存
27 @CacheEvict(value="MY_CACHE",  key="'cache_business_' + #business_id")        //allEntries=true
28 public void cleanBusinessFromCache(long business_id) throws SQLException {
29        return;
30 }
31 
32 
33 public void readShop(){
34 
35     ShopCacheData shop = loadShopData(1000);    //此处调用每次都会执行方法体,而不会使用相映的缓存
36 
37     ....
38 
39 }
40 
41 }
42 
43 
44 
45 解决办法:必须在方法中手工写入对Ehcache操作的代码才能起到作用
46 
47 public class ShopManager{
48 
49 @Cacheable(value="MY_CACHE", key="'cache_business_' + #business_id")
50 public ShopCacheData loadShopData(long business_id) throws SQLException{
51         ShopCacheData scd = null;
52         ...
53         Element el = null;
54         CacheManager manager = CacheManager.create();
55         // 通过manager可以生成指定名称的Cache对象
56         Cache cache = manager.getCache("MY_CACHE");
57         if(cache.isKeyInCache("cache_business_"+business_id)){
58             el = cache.get("cache_business_"+business_id);
59             return (ShopCacheData)el.getObjectValue();
60         }
61         //在缓存中没有找到对应的key则从数据库读取相关信息,并在得到后将结果放入缓存
62         scd = loadShopDatafromDb(business_id);
63         if(scd!=null){
64                 el = new Element("cache_business_"+business_id, scd);
65                 cache.put(el);
66         }
67         return scd;
68 }
69 
70 @CacheEvict(value="MY_CACHE",  key="'cache_business_' + #business_id")        //allEntries=true
71 public void cleanBusinessFromCache(long business_id) throws SQLException {
72         CacheManager manager = CacheManager.create();     //通过manager可以生成指定名称的Cache对象
73         Cache cache = manager.getCache("MY_CACHE");
74         if(cache.isKeyInCache("cache_business_"+business_id)){   //将指定key的缓存对象从缓存中清除
75             cache.remove("cache_business_"+business_id);
76         }
77        return;
78 }
79 
80 
81 public void readShop(){
82 
83     ShopCacheData shop = loadShopData(1000);   //此时调用缓存将有效
84 
85     ....
86 
87 }
88 
89 }

如何调用方法数据增加缓存
@Cacheable(value="MY_CACHE", key="'cache_business_' + #business_id")
public ShopCacheData loadShopData(long business_id) throws SQLException{
        ...
        return scd;
}

如何通过调用清空缓存
@CacheEvict(value="MY_CACHE",  key="'cache_business_' + #business_id")        //allEntries=true
public void cleanBusinessFromCache(long business_id) throws SQLException {
       return;
}

上面使用Cacheable注释的方式,只能在Controller中调用使缓存有效,如果在同一个Class中调用则缓存无效。

如:

public class ShopManager{

@Cacheable(value="MY_CACHE", key="'cache_business_' + #business_id")
public ShopCacheData loadShopData(long business_id) throws SQLException{
        ...
        return scd;
}

如何通过调用清空缓存
@CacheEvict(value="MY_CACHE",  key="'cache_business_' + #business_id")        //allEntries=true
public void cleanBusinessFromCache(long business_id) throws SQLException {
       return;
}

 

public void readShop(){

    ShopCacheData shop = loadShopData(1000);    //此处调用每次都会执行方法体,而不会使用相映的缓存

    ....

}

}

 

解决办法:必须在方法中手工写入对Ehcache操作的代码才能起到作用

public class ShopManager{

@Cacheable(value="MY_CACHE", key="'cache_business_' + #business_id")
public ShopCacheData loadShopData(long business_id) throws SQLException{
        ShopCacheData scd = null;
        ...
        Element el = null;
        CacheManager manager = CacheManager.create();
        // 通过manager可以生成指定名称的Cache对象
        Cache cache = manager.getCache("MY_CACHE");
        if(cache.isKeyInCache("cache_business_"+business_id)){
            el = cache.get("cache_business_"+business_id);
            return (ShopCacheData)el.getObjectValue();
        }

        //在缓存中没有找到对应的key则从数据库读取相关信息,并在得到后将结果放入缓存
        scd = loadShopDatafromDb(business_id);
        if(scd!=null){
                el = new Element("cache_business_"+business_id, scd);
                cache.put(el);
        }
        return scd;
}

@CacheEvict(value="MY_CACHE",  key="'cache_business_' + #business_id")        //allEntries=true
public void cleanBusinessFromCache(long business_id) throws SQLException {
        CacheManager manager = CacheManager.create();     //通过manager可以生成指定名称的Cache对象
        Cache cache = manager.getCache("MY_CACHE");
        if(cache.isKeyInCache("cache_business_"+business_id)){   //将指定key的缓存对象从缓存中清除
            cache.remove("cache_business_"+business_id);
        }

       return;
}

 

public void readShop(){

    ShopCacheData shop = loadShopData(1000);   //此时调用缓存将有效

    ....

}

}

原文地址:https://www.cnblogs.com/chancy/p/8610802.html