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);   //此時調用緩存將有效

    ....

}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM