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 所以提出来纠结下 实验 ...
随机推荐
- Linux内核启动过程start_kernel分析
虽然题目是start_kernel分析,但是由于我在ubuntu环境下配置实验环境遇到了一些问题,我觉得有必要把这些问题及其解决办法写下来. 首先我使用的是Ubuntu14.04 amx64,以下的步 ...
- Ashx的处理实例(逻辑处理/js调用)
做asp.net开发的对.aspx,.ascx和.ashx都不会陌生.关于它们,网上有很多文章介绍.“纸上得来终觉浅,绝知此事要躬行”,下面自己总结一下做个笔记.1..aspxWeb窗体设计页面.We ...
- CSS前5课总结
CSS<精通CSS.DIV网页样式与布局>视频前5课总结: 地对地导弹 第一课: 使用CSS控制页面: 1,行内样式 <p style="color:#0000FF; fo ...
- SqlServer不能将text列类型更改为ntext的问题
可以先将text类型更改为nvarchar,在将nvarchar更改为ntext即可. alter db_note alter column [content] nvarchar; alter db_ ...
- Swift编程语言简介
这篇文章简要介绍了苹果于WWDC 2014发布的编程语言Swift. ...
- 临时解决系统中大量的TIME_WAIT连接
今天,偶然间发现后台服务与数据库之间有大量的TIME_WAIT的连接: [root@localhost logs]# netstat -an | grep TIME_WAIT tcp a.a.a.a: ...
- iOS RSA加密解密及签名验证
1.首先要下载openssl,这个不用说,直接官网下载或者用brew install openssl下载 2.终端生成私钥密钥 2.1生成私钥 openssl genrsa - 2.2生成密钥 ope ...
- 系统弹性概念[TODO]
系统弹性 Shopify构建分布式可扩展应用的最佳实践 [编者的话]在构建大型分布式系统应用时,如何降低不同部分之间的依赖,增强系统的弹性,电商解决方案提供商 Shopify 给出了解决方法. 弹性矩 ...
- LintCode Interleaving String
Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...
- WinForm DataGridView根据选中的复选框删除
注意:在DataGridView添加一列(name:delete),ColumnType属性为:DataGridViewCheckBoxColumn,FlaseValue属性为:Flase,TureV ...