SpringCache
https://www.cnblogs.com/niechen/p/8760139.html
- Cache:該接口定義了操作緩存的最基本行為
package org.springframework.cache;
public interface Cache {
String getName(); //緩存的名字
Object getNativeCache(); //得到底層使用的緩存囊卜,如Ehcache
ValueWrapper get(Object key); //根據(jù)key得到一個(gè)ValueWrapper,然后調(diào)用其get方法獲取值
<T> T get(Object key, Class<T> type);//根據(jù)key泄伪,和value的類型直接獲取value
void put(Object key, Object value);//往緩存放數(shù)據(jù)
void evict(Object key);//從緩存中移除key對(duì)應(yīng)的緩存
void clear(); //清空緩存
interface ValueWrapper { //緩存值的Wrapper
Object get(); //得到真實(shí)的value
}
}
- CacheManager:該接口主要作用是獲取緩存和獲取緩存名稱(因?yàn)槲覀冊(cè)趹?yīng)用中并不是使用一個(gè)Cache粥脚,而是多個(gè),因此Spring提供了CacheManager抽象,用于緩存的管理)
/**
*默認(rèn)提供的實(shí)現(xiàn):
*ConcurrentMapCacheManager/ConcurrentMapCacheFactoryBean:管理ConcurrentMapCache矢沿;
*GuavaCacheManager;
*EhCacheCacheManager/EhCacheManagerFactoryBean酸纲;
*JCacheCacheManager/JCacheManagerFactoryBean捣鲸;
*另外還提供了CompositeCacheManager用于組合CacheManager,即可以從多個(gè)CacheManager中輪詢得到相應(yīng)的Cache
*
*當(dāng)我們調(diào)用cacheManager.getCache(cacheName) 時(shí)闽坡,會(huì)先從第一個(gè)cacheManager中查找有沒(méi)有cacheName的cache栽惶,如果沒(méi)有接著查找第二個(gè),如果最后找不到疾嗅,因?yàn)閒allbackToNoOpCache=true外厂,那么將返回一個(gè)NOP的Cache否則返回null。
*除了GuavaCacheManager之外代承,其他Cache都支持Spring事務(wù)的汁蝶,即如果事務(wù)回滾了,Cache的數(shù)據(jù)也會(huì)移除掉论悴。
*Spring不進(jìn)行Cache的緩存策略的維護(hù)掖棉,這些都是由底層Cache自己實(shí)現(xiàn)墓律,Spring只是提供了一個(gè)Wrapper,提供一套對(duì)外一致的API幔亥。
*/
package org.springframework.cache;
import java.util.Collection;
public interface CacheManager {
Cache getCache(String name); //根據(jù)Cache名字獲取Cache
Collection<String> getCacheNames(); //得到所有Cache的名字
}
- @EnableCaching:用于開啟緩存注解耻讽,通常需要和@Configuration使用
- @CacheEvict 觸發(fā)Cache接口中的evict方法,相當(dāng)于移除key對(duì)應(yīng)的值
SpringBoot-2.0之前
http://www.reibang.com/p/5a70b13a4fa7
要啟用緩存支持紫谷,我們需要?jiǎng)?chuàng)建一個(gè)新的 CacheManager bean齐饮。CacheManager 接口有很多實(shí)現(xiàn)捐寥,本文演示的是和 Redis 的集成笤昨,自然就是用 RedisCacheManager 了。Redis 不是應(yīng)用的共享內(nèi)存握恳,它只是一個(gè)內(nèi)存服務(wù)器瞒窒,就像 MySql 似的,我們需要將應(yīng)用連接到它并使用某種“語(yǔ)言”進(jìn)行交互乡洼,因此我們還需要一個(gè)連接工廠以及一個(gè) Spring 和 Redis 對(duì)話要用的 RedisTemplate崇裁,這些都是 Redis 緩存所必需的配置,把它們都放在自定義的 CachingConfigurerSupport 中:
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {
@Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
// Defaults
redisConnectionFactory.setHostName("192.168.1.166");
redisConnectionFactory.setPort(6379);
return redisConnectionFactory;
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(cf);
return redisTemplate;
}
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
// Number of seconds before expiration. Defaults to unlimited (0)
cacheManager.setDefaultExpiration(3000); // Sets the default expire time (in seconds)
return cacheManager;
}
}
我們使用@EnableCaching注解來(lái)開啟我們的項(xiàng)目支持緩存束昵,并在配置類內(nèi)添加了方法cacheManager()拔稳,方法的返回值則是使用了我們的Redis緩存的管理器,SpringBoot項(xiàng)目啟動(dòng)時(shí)就會(huì)去找自定義配置的CacheManager對(duì)象并且自動(dòng)應(yīng)用到項(xiàng)目中锹雏。
SpringBoot-2.0
http://www.reibang.com/p/4ccd4512700f
添加redis依賴并在application.properties中添加redis配置后巴比,Spring Boot會(huì)在偵測(cè)到存在Redis的依賴并且Redis的配置是可用的情況下,使用RedisCacheManager初始化CacheManager礁遵。也就是說(shuō)要使用緩存的話轻绞,SpringBoot就會(huì)選擇Redis來(lái)作為緩存的容器。Spring會(huì)執(zhí)行RedisAutoConfiguration佣耐,初始化RedisTemplate和StringRedisTemplate政勃。然后RedisCacheConfiguration會(huì)將RedisAutoConfiguration生成的RedisTemplate注入方法生成RedisCacheManager 并進(jìn)行了相關(guān)配置。(我們還可以對(duì)這個(gè)RedisCacheManager進(jìn)行二次配置兼砖,這里只列出配置key的有效期)
/**
* 重新配置RedisCacheManager
* @param rd
*/
@Autowired
public void configRedisCacheManger(RedisCacheManager rd){
rd.setDefaultExpiration(100L);
}
注意:
請(qǐng)不要在applicaion.properties中配置: spring.cache.cache-names=book1,book2奸远,否則會(huì)導(dǎo)致我們新的配置無(wú)法作用到這些配置的cache上。這是因?yàn)镽edisCacheConfiguration 初始化RedisCacheManager后讽挟,會(huì)立即調(diào)用RedisCacheConfiguration 的初始化cache然走,而此時(shí)configRedisCacheManger還沒(méi)有執(zhí)行此方法,使得我們的配置無(wú)法啟作用戏挡。反之芍瑞,如果不配置,則后創(chuàng)建cache褐墅,會(huì)使用我們的配置拆檬。