如何调用方法数据增加缓存
@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. 机器学习算法总结(九)——降维(SVD, PCA)

    降维是机器学习中很重要的一种思想.在机器学习中经常会碰到一些高维的数据集,而在高维数据情形下会出现数据样本稀疏,距离计算等困难,这类问题是所有机器学习方法共同面临的严重问题,称之为“ 维度灾难 ”.另 ...

  2. BZOJ1095:[ZJOI2007]Hide 捉迷藏(动态点分治)

    Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩 捉迷藏游戏.他们的家很大且构造很奇特,由N个屋子和N-1条 ...

  3. docker 6 docker运行的底层原理

    docker是一个client-server结构的系统,docker守护进程运行在主机上,然后通过socket连接从客户端访问,守护进程从客户端接收命令并管理运行在主机上的容器,是一个运行时的环境,就 ...

  4. Spring Security(十二):5. Java Configuration

    General support for Java Configuration was added to Spring Framework in Spring 3.1. Since Spring Sec ...

  5. 数组复制的五种方式(遍历循环一一赋值、System.arraycopy、地址赋值、克隆clone()、Arrays.copyof())

    package com.Summer_0424.cn; import java.util.Arrays; import java.util.concurrent.CopyOnWriteArrayLis ...

  6. 两个数字比较大小的方法 (分别应用if-else和条件运算符实现)

    package com.Summer_0424.cn; /** * @author Summer * 两个数字比较大小的方法 * 分别应用if-else和条件运算符实现 */ public class ...

  7. 2018Action Recognition from Skeleton Data via Analogical Generalization over Qualitative Representations

    论文标题: 来源/作者机构情况: Northwestern University Thirty-Second AAAI Conference on Artificial Intelligence, 2 ...

  8. Python排序算法——选择排序

    有趣的事,Python永远不会缺席! 如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/10787340.html 一.选择排序(Sele ...

  9. WPF PasswordBox不支持绑定解决方法

    原文:WPF PasswordBox不支持绑定解决方法 PasswordBox的Password属性因为安全原因不支持直接绑定,可以使用依赖属性实现.直接插入代码 public class Passw ...

  10. 在IDEA中构建Tomcat项目流程

    在IDEA中构建Web项目流程 打开你的IDEA,跟着我走! 第一步:新建项目 第二步:找到Artifacts 点击绿色的+号,如图所示,点一下 这一步很关键,目的是设置输出格式为war包,如果你的项 ...