Spring中使用Ehcache的方法和注意事项
如何调用方法数据增加缓存
@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的方法和注意事项的更多相关文章
- 在 JPA、Hibernate 和 Spring 中配置 Ehcache 缓存
jpa, hibernate 和 spring 时配置 ehcache 二级缓存的步骤. 缓存配置 首先在 persistence.xml 配置文件中添加下面内容: <property name ...
- Spring中RestTemplate的使用方法
一.REST 在互联网中,我们会通过请求url来对网络上的资源做增删改查等动作,这里的请求包含两部分:动词,主要包括增.删.改.查:名词,就是网络中的各种资源.传统的非REST风格的请求方式是把动词和 ...
- 面试官:spring中定义bean的方法有哪些?我一口气说出了12种,把面试官整懵了。
前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...
- 【Spring Framework】12种spring中定义bean的方法
前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...
- Spring中集成Ehcache缓存
1.导入依赖包 <dependency> <groupId>org.springframework</groupId> <artifactId>spri ...
- Spring中@Async注解实现“方法”的异步调用
原文:http://www.cnblogs.com/zhengbin/p/6104502.html 简单介绍: Spring为任务调度与异步方法执行提供了注解支持.通过在方法上设置@Async注解,可 ...
- SSM-Spring-12:Spring中NameMatchMethodPointcutAdvisor名称匹配方法切入点顾问
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- advice 是通知advisor 是顾问 顾问(Advisor) 通知Advice是Spring提供的一种切 ...
- spring中的多线程aop方法拦截
日常开发中,常用spring的aop机制来拦截方法,记点日志.执行结果.方法执行时间啥的,很是方便,比如下面这样:(以spring-boot项目为例) 一.先定义一个Aspect import org ...
- spring中得到servletContext对象方法
1.spring得到servletContext,这个和session没有什么关系,上下文可以说是一个session容器,一个上下文可以有多个会话session 在web.xml中有以下配置后.加入s ...
随机推荐
- (2)Python索引和切片
- 【转】联普多WAN口路由器是否可以设置叠加带宽
TP-link联普是全球领先的通讯供应厂商之一,那么你是否知道联普多WAN口路由器可以设置叠加带宽吗?下面是学习啦小编整理的一些关于联普多WAN口路由器是否可以设置叠加带宽的相关资料,供你参考. 联普 ...
- 【转】Mysql解决The total number of locks exceeds the lock table size错误
InnoDB表执行大批量数据的更新,插入,删除操作时会出现这个问题,需要调整InnoDB全局的innodb_buffer_pool_size的值来解决这个问题,并且重启mysql服务. windows ...
- hash_hmac 签名
<?php /** * =========================================================== * Model_Base * Descriptio ...
- Docker下载mysql镜像
1.使用命令查看mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL [OK] mariadb MariaDB [OK] mysql/ ...
- Java虚拟机垃圾收集器
一.判断对象存活的算法 1.引用计数(Reference Counting)算法 给对象添加一个引用计数器,每当有一个地方引用时,计数器加1.当引用失效时,计数器减1.当计数器的值为0的时候说该对象不 ...
- .net ElasticSearch-Sql 扩展类【原创】
官方提供的是java sdk,并支持jdbc方式的查询结果输出;但是却没有.net sdk的支持. 开发 ElasticSearch-Sql 第三方开源项目的.net sdk,未来集成入bsf框架.( ...
- Spring Boot 2 (十):Spring Boot 中的响应式编程和 WebFlux 入门
Spring 5.0 中发布了重量级组件 Webflux,拉起了响应式编程的规模使用序幕. WebFlux 使用的场景是异步非阻塞的,使用 Webflux 作为系统解决方案,在大多数场景下可以提高系统 ...
- iOS开发简记(5):设备唯一标识与全局变量
这里记录两个iOS开发中经常用到的知识点,一个是唯一标识,一个是全局变量. (1)唯一标识 唯一标识一台设备(比如iPhone.iPad等)是一个基本的实现与业务上的需求,因为这个唯一标识在许多场景都 ...
- 腾讯 Omi 5.0 发布 - Web 前端 MVVM 王者归来,mappingjs 强力加持
写在前面 腾讯 Omi 框架正式发布 5.0,依然专注于 View,但是对 MVVM 架构更加友好的集成,彻底分离视图与业务逻辑的架构. 你可以通过 omi-cli 快速体验 MVVM: $ npm ...