Ehcache和Spring整合
Ehcache是使用Java编写的缓存框架,比较常用的是,整合在Hibernate和MyBatis这种关系型数据库持久框架。
不过现在用NoSQL也比较盛行,要应用Ehcache,整合起来就没法按照那两个持久框架的方式。
比较通用的方式就是用Spring框架中的Cache功能,配合Cache相关注解来使用,这样不管数据库是关系型的还是NoSQL都是通用。
Ehcache配置
不管和哪个框架整合,ehcache的配置方式都是固定,只需配置ehcache.xml文件,一般默认放在classpath根目录下
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="D:/onda" /> <defaultCache maxElementsInMemory="3000" eternal="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="true"
diskPersistent="false" diskExpiryThreadIntervalSeconds="100"
memoryStoreEvictionPolicy="LRU" />
<cache name="one" maxElementsInMemory="3000" eternal="false"
overflowToDisk="true" timeToIdleSeconds="3600" timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LFU" />
</ehcache>
参考文档
http://www.ehcache.org/ehcache.xml
与Spring整合应用Spring Cache注解
Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.2.xsd"> <cache:annotation-driven cache-manager="cacheManager" /> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehcache" /> <bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml" p:shared="true" /> </beans>
注:<cache:annotation-driven cache-manager="cacheManager" />加这个标签需要在xmlns引入cache和xsi:schemaLocation指定spring-cache-4.2.xsd文件
Java代码
@Service
public class UserService { @Autowired
private UserDao userDao; @Cacheable(value="one")
public List<User> getUsers() { return userDao.getUsers();
}
}
Java示例代码中,在方法上加@Cacheable注解,说明这个方法是可以被缓存的
当程序调用此方法时,会先到缓存里查找是否存在,如果存在则返回缓存里的数据,如果不存在则执行这个方法获取数据,并把数据库放入缓存中。
@Cacheable注解里,有好几个参数,比如value是指定缓存的名称
Spring配置问题
用Cache注解方式,可能会碰到Spring配置问题。
可能在项目中应用了SpringMVC框架,因为配置文件没指定好,导致缓存操作失效
应用SpringMVC,在web.xml配置如下信息
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-core.xml</param-value>
</context-param>
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
ContextLoaderListener和DispatcherServlet这两个类分别是两个不同的ApplicationContext
如果以下信息是在spring-mvc.xml这个文件里配置
<context:component-scan base-package="com.zhang" />
<mvc:annotation-driven />
<cache:annotation-driven cache-manager="cacheManager" />配置在spring-core.xml,就算在代码里加了@Cache注解,也是无效的
正确方式,是把context:component-scan和cache:annotation-driven配置在同一个xml文件里
具体的原因,可以搜索ContextLoaderListener和DispatcherServlet这两个类加载bean的不同
Ehcache和Spring整合的更多相关文章
- Spring整合Ehcache管理缓存
前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...
- Spring整合Ehcache管理缓存(转)
目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...
- ehcache的基本使用及Spring整合
1.ehcache:百度百科这样解释的,EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.总的来说,他的出现就是减少对数据 ...
- 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整合Shiro做权限控制模块详细案例分析
1.引入Shiro的Maven依赖 <!-- Spring 整合Shiro需要的依赖 --> <dependency> <groupId>org.apache.sh ...
- 分析下为什么spring 整合mybatis后为啥用不上session缓存
因为一直用spring整合了mybatis,所以很少用到mybatis的session缓存. 习惯是本地缓存自己用map写或者引入第三方的本地缓存框架ehcache,Guava 所以提出来纠结下 实验 ...
随机推荐
- java中与数据库的连接
package unitl01; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet; ...
- POJ 2976
http://poj.org/problem?id=2976 01分数规划问题,可以舍掉k组 01分数规划用于解决的经典问题是最优比率生成树 解法见http://www.cnblogs.com/lot ...
- 数据存储之Cookie和Web Storage。
Cookie Cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密).接下来就谈谈cookie的一些利弊,coo ...
- Add and Search Word
Trie 树的一个应用 Design a data structure that supports the following two operations: void addWord(word) b ...
- 从AutoCAD和.NET开始
引自并参考Kean's blog:http://through-the-interface.typepad.com/through_the_interface/2006/07/getting_star ...
- VS常用快捷键
智能提示:ctrl + J方法参数提示:ctrl + shift +空格智能标记(如:提示using.实现接口.抽象类等):ctrl + .执行测试:ctrl + R,T(当前上下文),ctrl + ...
- web安全之sql注入报错型注入
前提: echo mysql_error(),输出错误信息. 熟悉的函数: floor()向下取整 concat()返回的字符串参数连接的结果 count()函数返回匹配指定条件的行数 rand()函 ...
- css3新增的属性选择器
使用css选择器,可以实现一个样式对应多个html文档的元素,在{}前面的部分就是"选择器",指明了样式的作用对象. 在CSS中追加了三个属性选择器:[att*=val].[att ...
- javascript中bind函数的作用
javascript的bind的作用 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- WCF初体验(C#操作Exchange)
最近再做一个Exchange的客户端维护工具,遇到了很多问题. 由于刚接触C#和Exchange,所以还需要继续学习.在此记录一下,只是一个新手的记录. 环境: 服务器:Exchang ...