Cacheable指定自定义属性
详情请参考spring官网添加链接描述
1.key的名字和TTL时间
@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;
}
// @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.缓存数据保存为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;
@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;
}
}
发现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;
@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
#如果指定了前缀就用我们指定的前缀,如果没有就默认使用缓存的名字作为前缀
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
#如果指定了前缀就用我们指定的前缀,如果没有就默认使用缓存的名字作为前缀
spring.cache.redis.key-prefix=CACHE_
spring.cache.redis.use-key-prefix=true
#是否缓存空值,防止缓存穿透
spring.cache.redis.cache-null-values=true
代码中直接返回null
// @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;
}
到此这篇关于SpringCache缓存自定义配置的实现的文章就介绍到这了,更多相关SpringCache缓存自定义配置内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!