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 ...
随机推荐
- 洛谷P1127-词链
Problem 洛谷P1127-词链 Accept: 256 Submit: 1.3kTime Limit: 1000 mSec Memory Limit : 128MB Problem ...
- 深入学习Redis:Redis内存模型
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 一.Redis内存统计 工欲善其事必先利其器,在说明Redis内存之前首先说明如何统计 ...
- foreach 引用传值&
foreach 引用传值& php 怎么在foreach中循环数组 ,的时候添加元素跟值 foreach($arr as $key => &$vo){ //注意,由于上面遍 ...
- ubantu搭建oj——第一天(6.11)
oj第一份作业: 按照DMOJ的文档将代码搬运到ubantu上 sudo apt install git gcc g++ make python-dev libxml2-dev libxslt1-de ...
- 004_centos安装pip的几种方式及pip源
一. (1) yum -y install epel-release yum install python-pip pip install --upgrade pip (2) python脚本的一键安 ...
- ①---Java开发环境配置
Java 开发环境配置 以下将为大家介绍如何搭建Java开发环境. window系统安装java 下载JDK 首先我们需要下载java开发工具包JDK,下载地址:http://www.oracle.c ...
- flask_sqlalchemy中根据聚合分组后的结果进行排序,根据日期(datetime)按天分组
from sqlalchemy import func, desc # 根据聚合查询总收入,按总收入逆序 s= db.session.query(TpOrders.room_type_id, (fun ...
- Node.js读取某个目录下的所有文件夹名字并将其写入到json文件
针对解决的问题是,有些时候我们需要读取某个文件并将其写入到对应的json文件(xml文件也行,不过目前用json很多,json是主流). 源码如下:index.js var fs = require( ...
- RabbitMQ 惰性队列Lazy Queue
RabbitMQ 队列分为几种类型,按照不同维度来分,可以分为排他性队列.普通队列.延迟队列.惰性队列.发布订阅队列等. 今天我们讨论的主角是惰性队列 Lazy Queue.众所周知,队列可以存储消息 ...
- [WPF]何如在Win7使用Aero2主题
1. 问题 假设我在Windows10的环境新建一个4.6的WPF项目,添加一个ComboBox,并用Blend在这个ComboBox上右键"编辑模板"->"编辑副 ...