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整合的更多相关文章

  1. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

  2. Spring整合Ehcache管理缓存(转)

    目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...

  3. ehcache的基本使用及Spring整合

    1.ehcache:百度百科这样解释的,EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.总的来说,他的出现就是减少对数据 ...

  4. Spring整合EHCache框架

    在Spring中使用缓存可以有效地避免不断地获取相同数据,重复地访问数据库,导致程序性能恶化. 在Spring中已经定义了缓存的CacheManager和Cache接口,只需要实例化便可使用. Spr ...

  5. 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出

    1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法   Shiro框架内部整合好缓存管理器, ...

  6. Spring整合EhCache详解

    一.EhCache介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开 源Java分布 ...

  7. 以Spring整合EhCache为例从根本上了解Spring缓存这件事(转)

    前两节"Spring缓存抽象"和"基于注解驱动的缓存"是为了更加清晰的了解Spring缓存机制,整合任何一个缓存实现或者叫缓存供应商都应该了解并清楚前两节,如果 ...

  8. Spring整合Shiro做权限控制模块详细案例分析

    1.引入Shiro的Maven依赖 <!-- Spring 整合Shiro需要的依赖 --> <dependency> <groupId>org.apache.sh ...

  9. 分析下为什么spring 整合mybatis后为啥用不上session缓存

    因为一直用spring整合了mybatis,所以很少用到mybatis的session缓存. 习惯是本地缓存自己用map写或者引入第三方的本地缓存框架ehcache,Guava 所以提出来纠结下 实验 ...

随机推荐

  1. 微信小程序开发技巧及填坑记录

    以下是自己在开发过程中遇到的坑和小技巧,记录以下: 1.出现了 page[pages/XXX/XXX] not found.May be caused by :1. Forgot to add pag ...

  2. Android DownloadProvider学习 (二)

    DownloadManager.Request用来请求一个下载,DownloadManager.Query用来查询下载信息,这两个类的具体功能会在后面穿插介绍.DownloadManager的源码可见 ...

  3. Sqlserver2005附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法

    Sqlserver2005附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法 最近几天从网上找了几个asp.net的登录案例想要研究研究代码,结果在用 Sql Server2005附 ...

  4. 获取url的html值

    //取当前页面的地址 例如http:127.0.0.1:80/aaa/index.html 返回http:127.0.0.1:80/aaa/function getUrlAddr(){ var str ...

  5. LintCode Two Strings Are Anagrams

    1. 把string变为char数组 2. 排序Arrays.sort() public class Solution { /** * @param s: The first string * @pa ...

  6. 初识Java-分数录入系统

    package classTest; import java.util.Scanner; public class scoreArrangement { /**  * 选择界面(main)  */ p ...

  7. Angularjs directive

    .directive('mydir',function(){ return{ multiElement: true/false, priority: number, //default: 0 term ...

  8. asdddddddddddddddd

    <a href="www.baidu.com">sad</a>

  9. 《C#编程宝典:十年典藏版》阅读笔记(1)

    1.运行时错误,使用Checked块语句进行异常检查与抛出异常. 2.值类型使用线程堆栈保存数据,数据大小大概为1M左右,引用类型使用托管堆保存数据,可以无限分配空间,因为有一个GC垃圾回收机制存在, ...

  10. Codeforces Round #175 (Div. 2)

    A. Slightly Decreasing Permutations 后\(k\)个倒序放前面,前\(n-k\)个顺序放后面. B. Find Marble 模拟. C. Building Perm ...