这篇文章将为大家详细讲解有关SpringCache缓存自定义配置的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
Cacheable指定自定义属性
详情请参考spring官网添加链接描述
1.key的名字和TTL时间
@Cacheable(value = {"category"},key ="'TopCategorys'" )@Overridepublic 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@Configurationpublic 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@Configurationpublic 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缓存自定义配置的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。