SpringCache緩存自定義配置的實現(xiàn)
Cacheable指定自定義屬性

詳情請參考spring官網(wǎng)添加鏈接描述
1.key的名字和TTL時間

/**
* 查詢所有1級分類
* @Cacheable代表當(dāng)前方法的結(jié)果需要緩存,若緩存中有則方法不會調(diào)用,若緩存中沒有會調(diào)用方法并將結(jié)果放入緩存
* 緩存默認(rèn)行為:
* a.若緩存中有則方法不會被調(diào)用
* b.key默認(rèn)自動生成,緩存的名字::SimpleKey [] (自動生成的key值)
* c.緩存的value值,默認(rèn)使用jdk序列化機制,將序列化后的數(shù)據(jù)存到redis
* d.默認(rèn)ttl時間為-1
* @return
*/
@Cacheable(value = {"category"},key ="'TopCategorys'" )
@Override
public List<CategoryEntity> getTopCategorys() {
System.out.println(".....getTopCategorys..........");
long startTime = System.currentTimeMillis();
List<CategoryEntity> categoryEntityList = this.baseMapper.selectList(
new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
System.out.println("消耗時間:" + (System.currentTimeMillis() - startTime));
return categoryEntityList;
}

/**
* 查詢所有1級分類
* @Cacheable代表當(dāng)前方法的結(jié)果需要緩存,若緩存中有則方法不會調(diào)用,若緩存中沒有會調(diào)用方法并將結(jié)果放入緩存
* 緩存默認(rèn)行為:
* a.若緩存中有則方法不會被調(diào)用
* b.key默認(rèn)自動生成,緩存的名字::SimpleKey [] (自動生成的key值)
* c.緩存的value值,默認(rèn)使用jdk序列化機制,將序列化后的數(shù)據(jù)存到redis
* d.默認(rèn)ttl時間為-1
* @return
*/
// @Cacheable(value = {"category"},key ="'TopCategorys'" )
@Cacheable(value = {"category"},key ="#root.method.name" )
@Override
public List<CategoryEntity> getTopCategorys() {
System.out.println(".....getTopCategorys..........");
long startTime = System.currentTimeMillis();
List<CategoryEntity> categoryEntityList = this.baseMapper.selectList(
new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
System.out.println("消耗時間:" + (System.currentTimeMillis() - startTime));
return categoryEntityList;
}

2.緩存數(shù)據(jù)保存為json格式





* 原理:
* CacheAutoConfiguration(selectImports方法)--->CacheConfigurations(MAPPINGS)
* --->RedisCacheConfiguration-->cacheManager方法--->RedisCacheManager初始化所有的緩存(determineConfiguration方法
* 每個緩存決定使用什么配置) --->createConfiguration方法
在config包下新建MyCacheConfig配置類
package com.atguigu.gulimall.product.config;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* 緩存配置
* @author zfh
* @email hst1406959716@163.com
* @date 2021-12-25 09:40:46
*/
@EnableCaching
@Configuration
public class MyCacheConfig {
@Bean
RedisCacheConfiguration redisCacheConfiguration(){
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
// config = config.entryTtl();
config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return config;
}
}

發(fā)現(xiàn)ttl變成了-1,我們的application.properties沒起作用
package com.atguigu.gulimall.product.config;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* 緩存配置
* @author zfh
* @email hst1406959716@163.com
* @date 2021-12-25 09:40:46
*/
@EnableConfigurationProperties(CacheProperties.class)
@EnableCaching
@Configuration
public class MyCacheConfig {
@Bean
RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
// config = config.entryTtl();
config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
//將配置文件中所有的配置都生效
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
return config;
}
}

3.使用緩存前綴
在application.properties文件中
spring.cache.type=redis #spring.cache.cache-names=qq #TTL 毫秒為單位 spring.cache.redis.time-to-live=3600000 #如果指定了前綴就用我們指定的前綴,如果沒有就默認(rèn)使用緩存的名字作為前綴 spring.cache.redis.key-prefix=CACHE_ spring.cache.redis.use-key-prefix=true

4.緩存null,防止緩存穿透
在application.properties文件中
spring.cache.type=redis #spring.cache.cache-names=qq #TTL 毫秒為單位 spring.cache.redis.time-to-live=3600000 #如果指定了前綴就用我們指定的前綴,如果沒有就默認(rèn)使用緩存的名字作為前綴 spring.cache.redis.key-prefix=CACHE_ spring.cache.redis.use-key-prefix=true #是否緩存空值,防止緩存穿透 spring.cache.redis.cache-null-values=true
代碼中直接返回null
/**
* 查詢所有1級分類
* @Cacheable代表當(dāng)前方法的結(jié)果需要緩存,若緩存中有則方法不會調(diào)用,若緩存中沒有會調(diào)用方法并將結(jié)果放入緩存
* 緩存默認(rèn)行為:
* a.若緩存中有則方法不會被調(diào)用
* b.key默認(rèn)自動生成,緩存的名字::SimpleKey [] (自動生成的key值)
* c.緩存的value值,默認(rèn)使用jdk序列化機制,將序列化后的數(shù)據(jù)存到redis
* d.默認(rèn)ttl時間為-1
*
* 原理:
* CacheAutoConfiguration(selectImports方法)--->CacheConfigurations(MAPPINGS)
* --->RedisCacheConfiguration-->cacheManager方法--->RedisCacheManager初始化所有的緩存(determineConfiguration方法
* 每個緩存決定使用什么配置) --->createConfiguration方法
* @return
*/
// @Cacheable(value = {"category"},key ="'TopCategorys'" )
@Cacheable(value = {"category"},key ="#root.method.name" )
@Override
public List<CategoryEntity> getTopCategorys() {
System.out.println(".....getTopCategorys..........");
long startTime = System.currentTimeMillis();
List<CategoryEntity> categoryEntityList = this.baseMapper.selectList(
new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
System.out.println("消耗時間:" + (System.currentTimeMillis() - startTime));
// return categoryEntityList;
return null;
}

到此這篇關(guān)于SpringCache緩存自定義配置的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringCache緩存自定義配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot集成camunda的實現(xiàn)示例
本文主要介紹了springboot集成camunda的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
MySQL查詢字段實現(xiàn)字符串分割split功能的示例代碼
本文主要介紹了MySQL查詢字段實現(xiàn)字符串分割split功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
SpringBoot中@Autowired爆紅原理分析及解決
這篇文章主要介紹了SpringBoot中@Autowired爆紅原理分析及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
解決Spring Cloud中Feign/Ribbon第一次請求失敗的方法
這篇文章主要給大家介紹了關(guān)于解決Spring Cloud中Feign/Ribbon第一次請求失敗的方法,文中給出了三種解決的方法,大家可以根據(jù)需要選擇對應(yīng)的方法,需要的朋友們下面來一起看看吧。2017-02-02

