前两节“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配置启用注解驱动的缓存

  1. package com.snieri.authentication.ehcache;
  2. import org.springframework.cache.CacheManager;
  3. import org.springframework.cache.annotation.EnableCaching;
  4. import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. @EnableCaching//启用缓存
  8. public class CachingConfig {
  9. /**
  10. * 声明缓存管理器
  11. */
  12. public CacheManager cacheManager() {
  13. return new ConcurrentMapCacheManager();
  14. }
  15. }
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配置启用注解驱动缓存

  1. <cache:annotation-driven/>
  2. 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

  1. package com.snieri.authentication.ehcache;
  2. import org.springframework.cache.annotation.EnableCaching;
  3. import org.springframework.cache.ehcache.EhCacheCacheManager;
  4. import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.core.io.ClassPathResource;
  8. import net.sf.ehcache.CacheManager;
  9. @Configuration
  10. @EnableCaching//启用缓存
  11. public class CachingConfig {
  12. /**
  13. * @param cacheManager 是 net.sf.ehcache.CacheManager的一个实例
  14. * 配置EhCacheCacheManager缓存管理器
  15. */
  16. @Bean
  17. public EhCacheCacheManager cacheManager(CacheManager cacheManager) {
  18. return new EhCacheCacheManager(cacheManager);
  19. }
  20. @Bean
  21. public EhCacheManagerFactoryBean ehcache() {
  22. EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
  23. ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("**/**/ehcache.xml"));
  24. return ehCacheManagerFactoryBean;
  25. }
  26. }
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

  1. <!-- 启用缓存 -->
  2. <cache:annotation-driven cache-manager ="cacheManager" />
  3. <!--声明一个缓存管理器(EhCacheCacheManager) 这里的实现代码是通过传入EhCache的CacheManager实例实现的 -->
  4. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>
  5. <!--这里并不是EhCacheManagerFactoryBean的实例,而是EhCache中CacheManager的一个实例  -->
  6. <!--因为Spring和EhCache都定义了CacheManager类型  -->
  7. <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  8. <property name="configLocation" value="classpath:/ehcache.xml" />
  9. <property name="shared" value="true"/>
  10. </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缓存这件事(转)的更多相关文章

  1. Spring整合Ehcache管理缓存

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

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

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

  3. Spring整合EHCache框架

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

  4. Spring整合EhCache详解

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

  5. spring整合ehcache实现缓存

    Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它支持注解方式使用缓存,非常方便. spring本身内置了对Cache的支持,之 ...

  6. spring整合ehcache注解实现查询缓存,并实现实时缓存更新或删除

    转载: http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天 ...

  7. spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除

    写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主题有所感触.不多说了,开干! 注:引入jar <!-- 引入ehcach ...

  8. 2.spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除

    转自:http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主 ...

  9. spring+shiro+ehcache整合

    1.导入jar包(pom.xml文件) <!-- ehcache缓存框架 --> <dependency> <groupId>net.sf.ehcache</ ...

随机推荐

  1. 120.VS调试技巧

    设置断点调试 在一行代码的左侧点击即可设置断点,按F5(调试->开始调试)即可运行到第一个端点处暂停 逐语句调试 按F11(调试->逐语句)即可开始一步一步执行 逐过程调试 按F10(调试 ...

  2. VMware Vsphere 6.0安装部署 总体部署架构

    (一)总体部署架构 本教程用于学习目的,力求详尽的介绍安装部署过程和各组件之间的关系,部署过程从最简单的模型开始,系列文章按时间顺序依次展开,每篇介绍一个组件. 开始阶段,按照一台物理服务器,部署所有 ...

  3. Linux 时区的修改

    Linux 时区的修改 1. CentOS和Ubuntu的时区文件是/etc/localtime,但是在CentOS7以后localtime以及变成了一个链接文件 ``` [root@centos7 ...

  4. js18--继承方式

    方式1:子类.prototype = 父类对象 Boy.prototype = new Person(); Sub.prototype = new Sup('张三');   //可以传参数也可以不传 ...

  5. Vue神之大坑处理:获取通过URL的的参数不可直接操作

    比如: $router.query['isZero'] == 'false';  //不会生效,刷新页面又好使了.打印处理是蓝色的false,再次刷新字体就变浅黑了. 解决:($router.quer ...

  6. [Vue + TS] Create Type-Safe Vue Directives in TypeScript

    Directives allow us to apply DOM manipulations as side effects. We’ll show you how you can create yo ...

  7. UART和RS232/RS485的关系,RS232与RS485编程

    http://wpp9977777.blog.163.com/blog/static/4625100720138495943540/ 串口通讯是电子工程师和嵌入式开发工程师面对的最基本问题,RS232 ...

  8. localStorage存储数据位置

    chrome浏览器:C:\Users\Username\AppData\Local\Google\Chrome\User Data\Default\Local Storage 中,虽然后缀名是.loc ...

  9. Django模板变量,过滤器和静态文件引用

    模版路径查找 首先去settings.py里面找TEMPLATES ,在TEMPLATES下面找DIRS,找到就返回,没找到就继续往下,如果APP_DIRS设置为为Ture,那么就会到上面 INSTA ...

  10. python文件的操作

    文件的操作,归根结底就只有两种:打开文件.操作文件 一.打开文件:文件句柄 = open('文件路径', '模式') python中打开文件有两种方式,即:open(...) 和  file(...) ...