博客
关于我
Spring缓存使用
阅读量:324 次
发布时间:2019-03-04

本文共 3033 字,大约阅读时间需要 10 分钟。

对于Java中Cache的使用,Java做出了规范

JSR107定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry和Expiry。
CachingProvider定义了创建、配置、获取、管理和控制多个CacheManager。如redis。一个应用可以在运行期访问多个CachingProvider。
CacheManager定义了创建、配置、获取、管理和控制多个唯一命名的Cache,这些Cache存在于CacheManager的上下文中。一个CacheManager仅被一个CachingProvider所拥有。
Cache是一个类似Map的数据结构并临时存储以Key为索引的值。一个Cache仅被一个CacheManager所拥有。
Entry是一个存储在Cache中的key-value对。
Expiry每一个存储在Cache中的条目有一个定义的有效期。一旦超过这个时间,条目为过期的状态。一旦过期,条目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。

Spring对Java的Cache标准进行实现,且更加易于使用。Spring从3.1开始定义了和org.springframework.cache.CacheManager接口来统一不同的缓存技术;并支持使用JCache(JSR-107)注解简化我们开发;所以在使用时,我们可以使用JSR规定的注解,也可以使用Spring定义的注解,但是Spring强烈建议不要混用Spring Cache和JCache的注解。下面我们来说使用时涉及的原理。

CacheManger是缓存的管理者,

Spring支持全透明加入Cache到应用中,调用者不用调用任何接口,只需使用@EnableCaching注解即可将Cache加入到应用中。如果你的应用中没有引入任何cache library(如redis),Spring Boot会自动配置一个使用concurrent maps的simple provider,当你需要使用cache的使用,simple provider会为你创建cache,Spring不建议你在生产环境中使用这个默认的simple provider。

使用Spring Cache你需要定义一个CacheManager or a CacheResolver named cacheResolver,否则Spring会按照下列顺序检测cache provider

Generic

JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, and others)
EhCache 2.x
Hazelcast
Infinispan
Couchbase
Redis
Caffeine
Simple

当然你可以配置spring.cache.type这个属性来指定cache provider,如果你指定spring.cache.type=none则会关闭Spring Cache。如果需要Spring Cache可以正常工作,必须配置一个CacheManager。

cacheManager接口的作用是用来获取Cache,类似一种对象工厂,所有的Cache,必须依赖与CacheManager来获取。

在Spring内部提供了三个默认的实现:
SimpleCacheManager 简单的集合实现
ConcurrentMapCacheManager 内部使用ConcurrentHashMap作为缓存容器
NoOpCacheManager 一个空的实现,不会保存任何记录(spring.cache.type=none)

下面就来说说具体的操作,通常情况下,你需要定义的一个CacheManager给到Spring管理:

@Configuration@EnableCaching@AutoConfigureAfter(RedisAutoConfiguration.class)public class CacheManagerConfiguration extends CachingConfigurerSupport {    public interface CacheManagerNames {        String REDIS_CACHE_MANAGER = "redisCacheManager";    }    @Bean(name = CacheManagerNames.REDIS_CACHE_MANAGER)    public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {        // 建议配置,比如动态创建出来的都会走此默认配        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()                .entryTtl(Duration.ofHours(1))                .computePrefixWith(cacheName -> "default:" + cacheName);        // Cache属性配置        Map
cacheConfigurations = ImmutableMap.
builder() // method-cache对应着cacheName,可以为多个cacheName独立设置 .put(CacheConstant.METHOD_CACHE, RedisCacheConfiguration. defaultCacheConfig(). entryTtl(Duration.ofMinutes(15)). disableCachingNullValues()). build(); // RedisCacheManager构建 RedisCacheManager redisCacheManager = RedisCacheManager.RedisCacheManagerBuilder. fromConnectionFactory(redisConnectionFactory). cacheDefaults(defaultCacheConfig). withInitialCacheConfigurations(cacheConfigurations). build(); return redisCacheManager; }}

 

转载地址:http://vuzh.baihongyu.com/

你可能感兴趣的文章
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>