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

spring本身内置了对Cache的支持,之前记录的是基于Java API的ConcurrentMap的CacheManager配置,现使用ehcache实现。

1、引入相关依赖

    <!-- 引入ehcache缓存 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${ehcache-version}</version>
</dependency>

2、声明对cache的支持

<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd"> <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven/> </beans>

2、配置CacheManager

先使用Spring提供的EhCacheCacheManager来生成一个Spring的CacheManager,让其接收一个Ehcache的CacheManager,因为真正用来存入缓存数据的还是Ehcache。

Ehcache的CacheManager是通过Spring提供的EhCacheManagerFactoryBean来生成的,它可以通过指定ehcache的配置文件位置来生成一个Ehcache的CacheManager.

若未指定则将按照Ehcache的默认规则取classpath根路径下的ehcache.xml文件,若该文件也不存在,则获取Ehcache对应jar包中的ehcache-failsafe.xml文件作为配置文件。

    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:config/ehcache.xml" />
</bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>

3、ehcache.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <!-- 默认缓存 -->
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"/> <!-- 用户详情缓存 -->
<cache name="userDetailCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/> </ehcache>

还有,以上说的是Spring内置的对Cache的支持,也可以通过Spring自己单独的使用Ehcache的CacheManager或Ehcache对象。

在配置文件中配置EhCacheManagerFactoryBean和EhCacheFactoryBean

1、EhCacheManagerFactoryBean

EhCacheManagerFactoryBean是Spring内置的一个可以产生Ehcache的CacheManager对象的FactoryBean。

可以通过属性configLocation指定用于创建CacheManager的Ehcache配置文件的路径,通常是ehcache.xml文件的路径。

如果没有指定configLocation,则将使用默认位置的配置文件创建CacheManager(即如果在classpath根路径下存在ehcache.xml文件,则直接使用该文件作为Ehcache的配置文件,否则将使用ehcache-xxx.jar中的ehcache-failsafe.xml文件作为配置文件来创建Ehcache的CacheManager)。

2、EhCacheFactoryBean

EhCacheFactoryBean是用来产生Ehcache的Ehcache对象的FactoryBean。

cacheManager属性,其可以指定将用来获取或创建Ehcache的CacheManager对象,若未指定则将通过CacheManager.create()获取或创建默认的CacheManager。

cacheName属性,其表示当前EhCacheFactoryBean对应的是CacheManager中的哪一个Ehcache对象,若未指定默认使用beanName作为cacheName。若CacheManager中不存在对应cacheName的Ehcache对象,则将使用CacheManager创建一个名为cacheName的Cache对象。

    <!-- 定义CacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<!-- 指定配置文件的位置 -->
<property name="configLocation" value="classpath:config/ehcache.xml"/>
<!-- 指定新建的CacheManager的名称 -->
<property name="cacheManagerName" value="cacheManagerName"/>
</bean> <!-- 定义一个Ehcache -->
<bean id="userCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheName" value="user"/>
<property name="cacheManager" ref="cacheManager"/>
</bean>

spring整合ehcache实现缓存的更多相关文章

  1. Spring整合Ehcache管理缓存

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

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

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

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

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

  4. Spring整合EHCache框架

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

  5. Spring整合EhCache详解

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

  6. mybatis 高级映射和spring整合之查询缓存(5)

    mybatis 高级映射和spring整合之查询缓存(5) 2.0 查询缓存 2.0.1 什么是查询缓存 mybatis提供缓存,用于减轻数据压力,提高数据库性能. mybatis提供一级缓存和二级缓 ...

  7. SpringBootsad整合EhCache做缓存处理

    轻量级的缓存框架Ehcache实现其功能.从以下几点切入: 什么是EhCache? 它和redis.membercache比较有什么优势? 和SpringBoot怎么整合? 实现机制? 有哪些坑? E ...

  8. redis集群配置,spring整合jedis,缓存同步

    前台的商品数据(图片等加载缓慢)查询,先从redis缓存查询数据. redis是一个nosql数据库,内存版数据库,读取速度11w/s.本身具有内存淘汰机制,是单线程服务器(分时操作系统),线程安全. ...

  9. 【spring-boot】spring-boot 整合 ehcache 实现缓存机制

    方式一:老 不推荐 参考:https://www.cnblogs.com/lic309/p/4072848.html /*************************第一种   引入 ehcach ...

随机推荐

  1. build模式入门,build模式理解(转载)

    问:为何要用? 普通做法: 1.创建pojo public class Person { private String name; private int age; private double he ...

  2. [原]排错实战——VS清空最近打开的工程记录

    原脚本how-toprocess monitorsysinternalsvsvisual studiovs2017vs2019注册表 缘起 vs有一个功能 -- 在起始页会显示最近打开的工程列表,方便 ...

  3. python编程:从入门到实践----第六章:字典>练习

    6-1 人:使用一个字典来存储一个熟人的信息,包括名.姓.年龄和居住的城市.该字典应包含键first_name .last_name .age 和city .将存储在该字典中的每项信息都打印出来. f ...

  4. Java web之jsp,xml(2020.1.7)

    1.xml文档规则 xml声明 字符集 xml元素的基本规则: 合法标签名 嵌套子元素 空元素

  5. ios ktvhttpcache 音视频缓存插件使用

    1.PodFile 文件增加 pod 'KTVHTTPCache',  '~> 2.0.0' 2.在终端 需要先cd到podfile文件所在目录  执行pod install 3.在header ...

  6. maven打包springboot项目的插件配置概览

    jar包的术语背景: normal jar: 普通的jar,用于项目依赖引入,不能通过java -jar xx.jar执行,一般不包含其它依赖的jar包. fat jar: 也叫做uber jar,是 ...

  7. vue-resource CRUD示例

    GET请求 var demo = new Vue({ el: '#app', data: { gridColumns: ['customerId', 'companyName', 'contactNa ...

  8. Docker容器化【Docker镜像与容器相关命令】

    # Docker 学习目标: 掌握Docker基础知识,能够理解Docker镜像与容器的概念 完成Docker安装与启动 掌握Docker镜像与容器相关命令 掌握Tomcat Nginx 等软件的常用 ...

  9. maven tomcat 自动部署配置

    1:Tomacat 配置 /tomcat-users.xml 添加如下: <role rolename="manager-gui"/> <role rolenam ...

  10. Flume(三) —— 断点续传 与 事务

    断点续传 # Name the components on this agent a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe / ...