如何调用方法数据增加缓存
@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. centos7下安装docker(15.3跨主机网络-macvlan)

    除了ovrlay,docker还开发了另一个支持跨主机容器的driver:macvlan macvlan本身是linu kernel模块,其功能是允许在同一物理网卡上配置多了MAC地址,即:多个int ...

  2. Oracle 11gR1 RAC存储迁移方案

    一.需求Oracle 11gR1 RAC存储计划更换,数据库版本为11.1.0.7,无停机维护窗口. 二.环境准备1.主机环境.OS环境2.安装11.1.0.6.0版Clusterware(linux ...

  3. Java8时间的简单时间

    package com.java8.date; import org.junit.Test; import java.text.SimpleDateFormat; import java.time.* ...

  4. Scala主构造器参数是否升级为成员与是否有get/set

    1:主构造器前面添加val/var 关键字则升级为类成员,否则只是构造器中的一个参数而已. 2:private 修饰get/set方法权限,private var/val 成员变量,则有get/set ...

  5. 第5章 Java中的锁

    5.1 Lock接口 并发编程安全性需要使用到锁,synchronized是一种隐式的获得与释放锁的关键字,除此之外还有Lock接口及其实现类,该接口及实现类提供了显示获取和释放锁的方式. 除了上述编 ...

  6. 【angularjs】使用angular搭建项目,实现隔行换色

    描叙:使用ng-class实现每隔3行换色 代码: <!DOCTYPE html> <html ng-app="myApp"> <head> & ...

  7. sqlachemy 查询当日数据,

    Tokens.query.filter(Tokens.user_id == user_id, db.cast(Tokens.create_time, db.DATE) == db.cast(curre ...

  8. hyperledge工具-configtxgen

    参考http://www.blockchainbrother.com/article/1339 configtxgen是Hyperledger Fabric提供的用于通道配置的实用程序,主要生成以下3 ...

  9. 自定义的库加载不进来,因为库中import的PIL和pillow文件没有pip install

    1.自定义的库,加载进来,提示red不能识别这个class或moudle 2.应该展开细节多看下,细节中提示,没有PIL和pillow 3.这个时候在cmd中使用pip安装PIL和pillow pip ...

  10. 首页技术支持常见问题宽带外网IP显示为10、100、172开头,没有公网IP,如何解决?

    1.表现形式: 路由器拨号获得的公网IP变成了一个以100开头的IP(或者是10.172开头),而打开ip138.com查询却又是另外一个IP,将100开头的这个IP到百度去查询下则显示所在区域为美国 ...