博客
关于我
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/

你可能感兴趣的文章
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>