Spring的缓存
来源:互联网 发布:淘宝网已买到的宝贝 编辑:程序博客网 时间:2024/05/21 00:20
1.Spring 缓存介绍
1.1 Spring 缓存简介
Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现。它支持注解方式使用缓存,非常方便。
1.2 缓存管理器
Spring 3.1内置了五个缓存管理器实现,如下所示:
- SimpleCacheManager
- NoOpCacheManager
- ConcurrentMapCacheManager
- CompositeCacheManager
- EhCacheCacheManagerSpring 3.2引入了另外一个缓存管理器,这个管理器可以用在基于JCach e(JSR-107)的缓存提供商之中。除了核心的Spring框架,Spring Data又提供了两个缓存管理器:
- RedisCacheManager(来自于Spring Data Redis项目)
- GemfireCacheManager(来自于Spring Data GemFire项目)
2. Spring 缓存使用
2.1 注解方式
@Cacheable
说明: 表明Spring在调用方法之前,首先应该在缓存中查找方法的返回值。如果这个值能够找到,就会返回缓存的值。否则的话,这个方法就会被调用,返回值会放到缓存之中
@CachePut
说明: 表明Spring应该将方法的返回值放到缓存中。在方法的调用前并不会检查缓存,方法始终都会被调用
@CacheEvict
说明: 表明Spring应该在缓存中清除一个或多个条目
@Caching
说明: 使用同一个缓存注解多次修饰一个方法.
例:
@CacheConfig
说明: 与前面的缓存注解不同,这是一个类级别的注解。如果类的所有操作都是缓存操作,你可以使用@CacheConfig来指定类,省去一些配置。
例:
SpEL扩展
Spring提供了多个用来定义缓存规则的SpEL扩展
3.Spring配置Ehcache缓存
3.1 Ehcache缓存简介
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点。它是Hibernate中的默认缓存框架。
- 主要的特性有:
1. 快速
2. 简单
3. 多种缓存策略
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5. 缓存数据会在虚拟机重启的过程中写入磁盘
6. 可以通过RMI、可插入API等方式进行分布式缓存
7. 具有缓存和缓存管理器的侦听接口
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
9. 提供Hibernate的缓存实现
3.2 配置Ehcache缓存
第一步: maven 依赖添加
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>${ehcache.version}</version></dependency> <!-- 这个jar包中含有Spring对于缓存功能的抽象封装接口。--><dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version></dependency>
第二步: applicationContext.xml 配置
<!-- 在spring.xml的配置文件中引入schema --> xmxmlns:cache="http://www.springframework.org/schema/cache" http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 --> <cache:annotation-driven cache-manager="ehcacheManager"/> <!-- 声明Ehcache缓存管理器 --> <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehcacheManagerFactory" /> </bean> <!-- 加载Ehcache配置 --> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"/> </bean>
第三步: 配置ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache> <!-- 磁盘缓存位置 --> <diskStore path="java.io.tmpdir"/> <!-- 默认缓存 --> <defaultCache maxEntriesLocalHeap="10000" eternal="false" overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30" maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> <!-- 自定义缓存 --> <cache name="myCache" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="30" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" transactionalMode="off"> <persistence strategy="localTempSwap"/> </cache> </ehcache>
3.3 Ehcache 配置参数介绍
3.4 注意事项
- 要实现缓存的实体必须要序列化
- spring security的缓存
- Spring Cache的缓存
- 谈谈spring的缓存
- Spring的缓存实现
- Spring的缓存注解
- Spring的缓存
- spring-boot的缓存
- Spring缓存机制的理解
- Spring缓存机制的理解
- Spring缓存机制的理解
- 基于Spring的Web缓存
- 基于Spring的Web缓存
- 使用spring的简单缓存
- Spring缓存机制的理解
- spring缓存cache的使用
- Spring容器的缓存技术
- Spring Cache 缓存的使用
- spring的eheache缓存机制
- 一系列问题
- Spring的事务详解
- this的用法
- LRU 和 FIFO 页面置换算法
- expect调用shell命令或shell脚本
- Spring的缓存
- 在ArcGIS中如何快速查找所要查看的要素?
- 学习网络通信框架
- 我所理解的大学-好友问答
- 操作系统
- Linux用户和文件权限管理
- 单链表及系列操作
- 模板方法模式
- Java String内存模型