以Spring整合EhCache为例从根本上了解Spring缓存这件事(转)
前两节“Spring缓存抽象”和“基于注解驱动的缓存”是为了更加清晰的了解Spring缓存机制,整合任何一个缓存实现或者叫缓存供应商都应该了解并清楚前两节,如果只是为了快速的应用EhCache到Spring项目中,请直接前往第三节“Spring整合EhCache缓存”。
一、 Spring缓存抽象
1. 注意和核心思想
Spring自身并没有实现缓存解决方案,但是对缓存管理功能提供了声明式的支持,能够与多种流行的缓存实现进行集成。
Spring Cache是作用在方法上的(不能理解为只注解在方法上),其核心思想是:当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值存放在缓存中,等到下次利用同样的参数调用该方法时将不再执行该方法,而是直接从缓存中获取结果进行返回。所以在使用Spring Cache的时候我们要保证我们的缓存的方法对于相同的方法参数要有相同的返回结果。
2. Spring对Cache的支持有两种方式
- 基于注解驱动的缓存
- 基于XML配置声明的缓存
下来我们介绍基于注解驱动的缓存
二、 基于注解驱动的缓存
在往bean上添加缓存注解之前,必须要启动Spring对注解驱动缓存的支持
1. 启用Spring对注解驱动缓存的支持
启用Spring对注解驱动缓存的支持,也是有两种方式的:
- Java配置(这个方法可以比较清晰的了解缓存管理器是如何声明的)
- XML配置(这个方法比较方便简单,这里的XML配置不能跟上面的“基于XML配置声明的缓存”搞混,两者不是同一层概念)
下面用一个简单的例子来说明这两种方法的区别,这跟Spring整合EhCache没什么关系,但是可以让大家搞懂缓存管理器,也就是我们下节要说的。
程序清单1: Java配置启用注解驱动的缓存
- package com.snieri.authentication.ehcache;
- import org.springframework.cache.CacheManager;
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- @EnableCaching//启用缓存
- public class CachingConfig {
- /**
- * 声明缓存管理器
- */
- public CacheManager cacheManager() {
- return new ConcurrentMapCacheManager();
- }
- }
package com.snieri.authentication.ehcache; import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Configuration; @Configuration
@EnableCaching//启用缓存
public class CachingConfig {
/**
* 声明缓存管理器
*/
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager();
} }
程序清单2:XML配置启用注解驱动缓存
- <cache:annotation-driven/>
- n id="cacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager"></bean>
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager"></bean>
这两种方式,Java配置方式是通过使用@EnableCaching启用注解驱动的缓存,XML配置方式是通过使用<cache:annotation-driven/>启用注解驱动的缓存。本质上来讲,这两种工作方式是相同的,它们都会创建一个切面(AOP)并出发Spring缓存注解的切点(pointcut)。
在这两个程序清单中,不仅仅启用了注解驱动的缓存,还声明了一个缓存管理器(cache manager)的bean。缓存管理器是Spring抽象的核心,它能够与多个流行的缓存实现进行集成。这里又提到这句话,请大家切记。
上面的例子声明了一个ConcurrentMapCacheManager,这个简单的缓存管理器使用java.util.concurrent.ConcurrentHashMap作为其缓存存储。这个缓存非常简单,对于开发、测试或其他基础的应用来讲,是一个不错的选择。但是它的缓存存储是基于内存的,所以它的声明周期是与应用关联的,对于生产级别的大型企业级应用程序,这个缓存并不理想。
幸好,Spring有多个缓存管理器方案可供选择,请看下节。
2. 缓存管理器(这里的概念搞清楚很重要)
Spring内置很多缓存管理器(从Spring3到Spring4好像有7个吧,记不太清了,有兴趣的可以查查)
SimpleCacheManager
NoOpCacheManager
ConcurrentMapCacheManager(这个缓存管理器就是上面的例子介绍的)
CompositeCacheManager
EhCacheCacheManager(看到这里终于见到标题主人翁了)
RedisCacheManager(来自Spring Date Redis项目)
GemfireCacheManager(来自Spring Date GemFire项目)
可以看到,在为Spring的缓存抽象选择缓存管理器时,我们有很多可选方案。具体选择哪一个要取决于想要使用的底层缓存供应商。每一个方案都可以为应用提供不同风格的缓存,其中有一些会比其他的更加适用于生产环境。尽管所做出的选择会影响到数据如何缓存,但是Spring声明缓存的方式并没有什么差别。也就是说,无论你用什么缓存管理器,Spring声明缓存的方式是不变的,注解驱动缓存(java配置启动、XML配置启动)和XML配置缓存两种方式。
三、 Spring整合EhCache缓存
在往bean上添加缓存注解之前,必须要启动Spring对注解驱动缓存的支持,所以说有两个步骤:
启用Spring对注解驱动缓存的支持
声明某些方法或者整个类使用缓存,这一步包含编写ehcache.xml文件(后面会有详细介绍)
1. 启用Spring对注解驱动缓存的支持
这里依然介绍的是基于注解驱动,上面已经讲了,有两个启动方式:Java配置的方式和XML配置的方式(两种方式本质是一样的)。XML是我们常见的启动方式。
程序清单3:以Java配置的方式设置EhCacheManager
- package com.snieri.authentication.ehcache;
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.cache.ehcache.EhCacheCacheManager;
- import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.io.ClassPathResource;
- import net.sf.ehcache.CacheManager;
- @Configuration
- @EnableCaching//启用缓存
- public class CachingConfig {
- /**
- * @param cacheManager 是 net.sf.ehcache.CacheManager的一个实例
- * 配置EhCacheCacheManager缓存管理器
- */
- @Bean
- public EhCacheCacheManager cacheManager(CacheManager cacheManager) {
- return new EhCacheCacheManager(cacheManager);
- }
- @Bean
- public EhCacheManagerFactoryBean ehcache() {
- EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
- ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("**/**/ehcache.xml"));
- return ehCacheManagerFactoryBean;
- }
- }
package com.snieri.authentication.ehcache; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource; import net.sf.ehcache.CacheManager; @Configuration
@EnableCaching//启用缓存
public class CachingConfig {/**
* @param cacheManager 是 net.sf.ehcache.CacheManager的一个实例
* 配置EhCacheCacheManager缓存管理器
*/
@Bean
public EhCacheCacheManager cacheManager(CacheManager cacheManager) {
return new EhCacheCacheManager(cacheManager);
} @Bean
public EhCacheManagerFactoryBean ehcache() { EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("**/**/ehcache.xml"));
return ehCacheManagerFactoryBean; }
}
程序清单4:以XML配置的方式设置EhCacheManager
- <!-- 启用缓存 -->
- <cache:annotation-driven cache-manager ="cacheManager" />
- <!--声明一个缓存管理器(EhCacheCacheManager) 这里的实现代码是通过传入EhCache的CacheManager实例实现的 -->
- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>
- <!--这里并不是EhCacheManagerFactoryBean的实例,而是EhCache中CacheManager的一个实例 -->
- <!--因为Spring和EhCache都定义了CacheManager类型 -->
- <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
- <property name="configLocation" value="classpath:/ehcache.xml" />
- <property name="shared" value="true"/>
- </bean>
<!-- 启用缓存 -->
<cache:annotation-driven cache-manager ="cacheManager" />
<!--声明一个缓存管理器(EhCacheCacheManager) 这里的实现代码是通过传入EhCache的CacheManager实例实现的 -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>
<!--这里并不是EhCacheManagerFactoryBean的实例,而是EhCache中CacheManager的一个实例 -->
<!--因为Spring和EhCache都定义了CacheManager类型 -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:/ehcache.xml" />
<property name="shared" value="true"/>
</bean>
在程序清单3中,cacheManager()方法创建了一个EhCacheCacheManager实例,这是通过传入EhCache的cacheManager实例实现的。这里比较诡异,让人感到迷惑,而且在导包的时候往往会容易导错,这是因为Spring和EhCache都定义了CacheManager类型。需要明确的是,EhCache的CacheManager要被注入到Spring的EhCacheCacheManager之中。
可以从import的包中也可以查看到这些信息(这也算是马后炮了,但是有助于我们理解)。下面这段比较重要,认真思考。
*我们需要使用EhCache的CacheManager来进行注入,所以必须声明一个CacheManager bean。为了对其进行简化,Spring提供了EhCacheManagerFactoryBean来生成EhCache的CacheManager。方法ehcache()会创建并返回一个EhCacheManagerFactoryBean实例。因为它是一个工厂bean(也就是说,它实现了Spring的FactoryBean接口),所以注册在Spring应用上下文的并不是EhCacheManagerFactoryBean的实例,而是EhCache的CacheManager实例,因此适合注入到EhCacheCacheManager之中。
2. 声明某些方法或整个类哪个使用缓存并且编写ehcache.xml文件
下面两个内容可以在我转载的另一个博文《Spring使用Cache》中查看,比较清晰,结合起来的话应该会对spring缓存有所了解。
1) 为方法或类添加注解以支持缓存
2) ehcache.xml配置
而以下的这两节我会在不久进行整理,注意查看我的博文。
四、 Spring整合Redis缓存
五、 Spring整合使用多个缓存管理器
以Spring整合EhCache为例从根本上了解Spring缓存这件事(转)的更多相关文章
- Spring整合Ehcache管理缓存
前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...
- Spring整合Ehcache管理缓存(转)
目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...
- Spring整合EHCache框架
在Spring中使用缓存可以有效地避免不断地获取相同数据,重复地访问数据库,导致程序性能恶化. 在Spring中已经定义了缓存的CacheManager和Cache接口,只需要实例化便可使用. Spr ...
- Spring整合EhCache详解
一.EhCache介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开 源Java分布 ...
- spring整合ehcache实现缓存
Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它支持注解方式使用缓存,非常方便. spring本身内置了对Cache的支持,之 ...
- spring整合ehcache注解实现查询缓存,并实现实时缓存更新或删除
转载: http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天 ...
- spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除
写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主题有所感触.不多说了,开干! 注:引入jar <!-- 引入ehcach ...
- 2.spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除
转自:http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主 ...
- spring+shiro+ehcache整合
1.导入jar包(pom.xml文件) <!-- ehcache缓存框架 --> <dependency> <groupId>net.sf.ehcache</ ...
随机推荐
- 54.nodejs nodemailer不兼容
转自:https://blog.csdn.net/q36835109/article/details/53067917 注:由于本人使用最新版本的nodemailer不兼容,所以目前使用的是0.7.1 ...
- TensorFlow的学习
1.先判断python的版本(因为有些python版本自带pip,可以参考我写的对pip的认识的博客文章),选择是否安装pip,然后安装更新tensorflow如:sudo pip install - ...
- enable&&builtin---shell内部命令
用enable命令显示所有激活的内部命令: [root@localhost ~]# enable -a builtin命令用于执行指定的shell内部命令,并返回内部命令的返回值 [root@xiao ...
- understand软件使用教程(转)
源代码阅读工具(Scientific Toolworks Understand)的特色 1.支持多语言:Ada, C, C++, C#, Java, FORTRAN, Delphi, Jovial, ...
- 数字签名算法--3.ECDSA
package Imooc; import java.security.KeyFactory; import java.security.KeyPair; import java.security.K ...
- php数组插入数据
php数组插入数据 一.总结 代码要多敲,看是看不会的 php代码直接在页面不好敲,可以去控制器里面敲 二.目标 在一组数中,要求插入一个数,按其原来顺序插入,维护原来排序方式. 三.代码 (1).思 ...
- python学习一,python基本语法
python基本语法: 1.python基本语句结构: 首先,在其他的语言中,比如java,c++,c#等,没写完一行语句之后,都需要在语句的末尾加一个分号,表示该语句结束,但是在python中,我们 ...
- BZOJ1444: [Jsoi2009]有趣的游戏(Trie图,矩乘)
Description Input 注意 是0<=P, n , l, m≤ 10. Output Sample Input input 1 3 2 2 1 2 1 2 AB BA AA inpu ...
- 【Codeforces Round #453 (Div. 2) B】Coloring a Tree
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从根节点开始. 显然它是什么颜色.就要改成对应的颜色.(如果上面已经有某个点传了值就不用改 然后往下传值. [代码] #includ ...
- Microsoft SQL Server Version List(SQL Server 版本)
原帖地址 What version of SQL Server do I have? This unofficial build chart lists all of the known Servic ...