如何调用方法数据增加缓存
@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注释的方式,如果在同一个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); //此时调用缓存将有效 .... } }

如何调用方法数据增加缓存
@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);   //此时调用缓存将有效

....

}

}

Spring中使用Ehcache的方法和注意事项的更多相关文章

  1. 在 JPA、Hibernate 和 Spring 中配置 Ehcache 缓存

    jpa, hibernate 和 spring 时配置 ehcache 二级缓存的步骤. 缓存配置 首先在 persistence.xml 配置文件中添加下面内容: <property name ...

  2. Spring中RestTemplate的使用方法

    一.REST 在互联网中,我们会通过请求url来对网络上的资源做增删改查等动作,这里的请求包含两部分:动词,主要包括增.删.改.查:名词,就是网络中的各种资源.传统的非REST风格的请求方式是把动词和 ...

  3. 面试官:spring中定义bean的方法有哪些?我一口气说出了12种,把面试官整懵了。

    前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...

  4. 【Spring Framework】12种spring中定义bean的方法

    前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...

  5. Spring中集成Ehcache缓存

    1.导入依赖包 <dependency> <groupId>org.springframework</groupId> <artifactId>spri ...

  6. Spring中@Async注解实现“方法”的异步调用

    原文:http://www.cnblogs.com/zhengbin/p/6104502.html 简单介绍: Spring为任务调度与异步方法执行提供了注解支持.通过在方法上设置@Async注解,可 ...

  7. SSM-Spring-12:Spring中NameMatchMethodPointcutAdvisor名称匹配方法切入点顾问

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- advice 是通知advisor 是顾问 顾问(Advisor) 通知Advice是Spring提供的一种切 ...

  8. spring中的多线程aop方法拦截

    日常开发中,常用spring的aop机制来拦截方法,记点日志.执行结果.方法执行时间啥的,很是方便,比如下面这样:(以spring-boot项目为例) 一.先定义一个Aspect import org ...

  9. spring中得到servletContext对象方法

    1.spring得到servletContext,这个和session没有什么关系,上下文可以说是一个session容器,一个上下文可以有多个会话session 在web.xml中有以下配置后.加入s ...

随机推荐

  1. Anaconda的下载与安装

    1.下载地点: 支持国产:https://mirrors.tuna.tsinghua.edu.cn/ Anaconda官网:https://www.anaconda.com/download/ 2.下 ...

  2. tensorflow 模型保存与加载 和TensorFlow serving + grpc + docker项目部署

    TensorFlow 模型保存与加载 TensorFlow中总共有两种保存和加载模型的方法.第一种是利用 tf.train.Saver() 来保存,第二种就是利用 SavedModel 来保存模型,接 ...

  3. springboot--bean交给容器

    1.把bean交给springboot管理 springboot也是一个spring容器.把一个bean交给springboot的容器有三种方法,前两种是先把bean交给spring容器再把sprin ...

  4. (六) JavaScript 对象

  5. RabbitMQ学习系列

    http://www.cnblogs.com/zhangweizhong/category/855479.html https://www.cnblogs.com/zhangweizhong/p/56 ...

  6. 英文字母对应的Unicode编码

    A~Z :65~90 a~z :97~122 0-9 : 48-57 如果想要知道字符串中的值是否是小写英文字符,不使用工具包的一种方法就是使用Unicode编码值,举例: package main ...

  7. Spring Security(八):2.4.3 Project Modules

    In Spring Security 3.0, the codebase has been sub-divided into separate jars which more clearly sepa ...

  8. node.js如何引用其它js文件

    以Java来说,比如要实现第三方存储,我可能需要导入对应的库,以maven为例,使用腾讯云或者七牛云.阿里云,我需要导入对应的maven依赖.再比如,有些时候我们封装某个类,而那个类不在该包下,我们需 ...

  9. 【记一次pull request的惨痛教训】不可见的分隔符之Zero-with-space

    问题描述: 我在修改 ctf-wiki 目录后进行 mkdocs build 去生成索引目录的时候报错: 然后我尝试定位到第 2 行和第 288 行,这些行我似乎并没有修改过啊. 未果,开始去找师傅解 ...

  10. 开源后的.Net 如何选择使用

     .NET是跨平台的开发栈.它有一个标准库,称为.NET Standard Library,其中包含了大量的APIs.这个标准库由各种.NET运行环境实现:.NET Framework..NET Co ...