ehcache的基本使用及Spring整合
1.ehcache:百度百科这样解释的,EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。总的来说,他的出现就是减少对数据库的访问次数。从数据库查询出来的数据放到缓存中,以便下次调用,大大减少了 开销,性能也得到了提高。
特点:
l Java界最出名的缓存框架,主要用于单机缓存,速度很快。
l 进程级别的缓存,多个线程可共享一个缓存数据。
其实现方式如下图所示:

2.简单的解释一下ehcache的核心配置文件
<!-- 硬盘上缓存的临时目录 -->
<diskStore path="java.io.tmpdir"/>
<!--
maxElementsInMemory:内存中最大存放的元素的个数
eternal:是否永生,默认是false
timeToIdleSeconds:发呆闲置的时间,超过该时间,被清除,单位秒
timeToLiveSeconds:存活的事件,超过该时间被清除
maxElementsOnDisk:如果内存满了,溢出到硬盘上的临时目录中的存放的元素的个数
diskExpiryThreadIntervalSeconds:轮询时间,巡视组
memoryStoreEvictionPolicy:内存对象的清理策略,如果满了,怎么办?
策略有三个:LRU、LFU、FIFO
LRU:最少使用被清理,次数
LFU:时间,闲置最长的时间
FIFO:管道策略,先进先出
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
l 如果你的数据只是临时存放到缓存,减轻数据库压力,用默认值就很好
l 如果你的数据要在程序运行过程中一直被查询,则建议将设置为所有的元素被清除,一般用于数据字典。
3.ehcache中的缓存注解
@Cacheable和@CacheEvict来对缓存进行操作
@Cacheable:将方法的返回值放入到缓存中
@CacheEvict:负责清除缓存
4.下面与Spring整合的案列
需求:将菜单数据放入Ehcache中。
4.1.引入坐标
<!-- ehcache的缓存框架 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.10</version>
</dependency>
<!-- spring整合第三方框架的 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
4.2.编写配置文件(ehcache文件)
<!-- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
--><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!-- 磁盘上的缓存的临时目录 ,默认是系统的临时目录,也可以手动指定一个目录-->
<diskStore path="java.io.tmpdir"/>
<!-- 默认的缓存区域的默认策略 -->
<!--
maxElementsInMemory:内存中元素最大存放的个数
eternal:缓存的对象是否永生不死。一般都是false。
timeToIdleSeconds:发呆的时间,多长时间不用,就干掉,秒
timeToLiveSeconds:存活的时间,活够了就干掉,秒
maxElementsOnDisk:硬盘上最大存放的元素的个数,如果内存10000个满了,就往硬盘上存。
memoryStoreEvictionPolicy:内存中存储和销毁元素的策略:默认使用LRU,解决的是缓存元素满了怎么办。
策略有三个:LRU、LFU、FIFO
LRU:最少使用被清理,次数
LFU:时间,闲置最长的时间
FIFO:管道策略,先进先出
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<!-- Spring整合的菜单缓存 -->
<cache name="bos_menu_cache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
4.3.Spring整合encache配置文件(需要引入ehcache文件)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 配置ehcache的对象EhCacheManager -->
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<!-- 注入ehcache核心配置文件的位置 可以不配置,默认找类路径下的ehcache.xml -->
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
<!-- Spring整合Ehache -->
<!-- Spring的平台缓存管理器 -->
<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<!-- 注入ehcache的对象 -->
<property name="cacheManager" ref="ehCacheManager"></property>
</bean>
<!-- spring的缓存注解驱动 -->
<cache:annotation-driven cache-manager ="springCacheManager" />
</beans>
4.4.在需要操作缓存数据的方法上添加注解
package cn.bos.service.impl.system; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import cn.bos.dao.system.MenuRepository;
import cn.bos.domain.system.Menu;
import cn.bos.domain.system.User;
import cn.bos.service.system.MenuService; @Service("menuService")
@Transactional
public class MenuServiceImpl implements MenuService{ @Autowired
private MenuRepository menuRepository; @Override
public List<Menu> menu_list() { return menuRepository.findAll();
} @Override
//清除所有的缓存
@CacheEvict(value="bos_menu_cache",allEntries=true)
public void menu_add(Menu model) { if(model.getParentMenu()!=null&&model.getParentMenu().getId()==null){
model.setParentMenu(null);
} menuRepository.save(model);
} @Override
//@Cacheable(value="bos_menu_cache")方法的结果放到缓存中
// 使用用户的标号作为缓存的key
@Cacheable(value="bos_menu_cache",key="#user.id")
public List<Menu> findMenuByUser(User user) {
// 管理员 所有菜单
if("admin".equalsIgnoreCase(user.getUsername())){
return menuRepository.findByOrderByPriority();
}else{
// 普通用户(显示部分菜单)
return menuRepository.findByUser(user);
}
}
}
注意:实体类中的序列化问题:
序列化的id,用来作为序列化和反序列化的唯一标识,默认的值是根据类的内容计算出来,如果类的内容发生变化,则值会变,例如:反序列化之前,将类改变内容了,那么反序列化将会失败。因此,为了保障反序列化成功,一般将该id的值定死!(随机数)
ehcache的基本使用及Spring整合的更多相关文章
- Spring整合Ehcache管理缓存
前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...
- Spring整合Ehcache管理缓存(转)
目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...
- Ehcache和Spring整合
Ehcache是使用Java编写的缓存框架,比较常用的是,整合在Hibernate和MyBatis这种关系型数据库持久框架. 不过现在用NoSQL也比较盛行,要应用Ehcache,整合起来就没法按照那 ...
- Spring整合EHCache框架
在Spring中使用缓存可以有效地避免不断地获取相同数据,重复地访问数据库,导致程序性能恶化. 在Spring中已经定义了缓存的CacheManager和Cache接口,只需要实例化便可使用. Spr ...
- 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出
1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法 Shiro框架内部整合好缓存管理器, ...
- Spring整合EhCache详解
一.EhCache介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开 源Java分布 ...
- 以Spring整合EhCache为例从根本上了解Spring缓存这件事(转)
前两节"Spring缓存抽象"和"基于注解驱动的缓存"是为了更加清晰的了解Spring缓存机制,整合任何一个缓存实现或者叫缓存供应商都应该了解并清楚前两节,如果 ...
- spring整合ehcache实现缓存
Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它支持注解方式使用缓存,非常方便. spring本身内置了对Cache的支持,之 ...
- Spring整合Shiro做权限控制模块详细案例分析
1.引入Shiro的Maven依赖 <!-- Spring 整合Shiro需要的依赖 --> <dependency> <groupId>org.apache.sh ...
随机推荐
- $(window).scroll在页面没有滚动条时无法触发事件的bug解决方法
JS //给页面绑定滑轮滚动事件 if (document.addEventListener) { //webkit document.addEventListener('mousewheel', s ...
- ulua c#调用lua中模拟的类成员函数
项目使用ulua,我神烦这个东西.lua单纯在lua环境使用还好,一旦要跟外界交互,各种月经不调就来了.要记住贼多的细节,你才能稍微处理好.一个破栈,pop来push去,位置一会在-1,一会在-3,2 ...
- bat自动打包压缩实现
1.引言 本文档的编辑目的是为了实bat脚本自动打包功能,包含包的名字命名,压缩文件内外层文件夹的名字:包含svn版本号等: 2.实现介绍 (1)获取svn号,生成批处理文件 写一个pak.bat文件 ...
- 包装类和基本类型区别?(integer和int取值范围一样大)
1.声明方式不同,int不需要new .Integer需要new 2.性质上根本不同点:int是基本数据类型.Integer是引用数据类型,它有自己的属性,方法 3.存储位置和方式不同:int是存储在 ...
- HTML5之Notification简单使用
var webNotification = { init: function() { if(!this.isSupport()) { console.log('不支持通知'); return; } t ...
- wpf timePicker 时间选择控件
wpf里有日期选择控件,但没有时间选择控件.其他地方也有类似的,但效果并不太好,而且复杂.所以就自己写了个.参考codeproject上的. 分两部分. 第一部分是.cs文件.也就是control控件 ...
- mysql插入数据时检查是否某字段已存在
SELECT\n" + " '',\n" + " '{0}',\n" + " '{1}',\n" + " '{2}'\n ...
- thinkphp使用自带webserver
进入命令行,进入 tp5/public 目录后,输入如下命令:php -S localhost:8888 router.php 然后进行访问
- ABP示例程序-使用AngularJs,ASP.NET MVC,Web API和EntityFramework创建N层的单页面Web应用
本片文章翻译自ABP在CodeProject上的一个简单示例程序,网站上的程序是用ABP之前的版本创建的,模板创建界面及工程文档有所改变,本文基于最新的模板创建.通过这个简单的示例可以对ABP有个更深 ...
- HDU [P1533]
二分图带权最小匹配(朴素) 只要换几个不等号的方向就行,不需要变换权值的正负 #include <iostream> #include <cstdio> #include &l ...