SpringCache

SpringCache  属于Spring

缓存管理器(定义规则)   创建出   不同的缓存组件(CRUD缓存)[人为的分组件,其实都存在一起]  

官网地址

视频简介

ctrl+n   查找CacheManager  接口

ctrl+h  查看他有几个实现类

 

 

1.引入依赖

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

2.引入redis,使用redis做缓存

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>

源码:(看自动配置)

CacheAutoConfiguration   69  =>  CacheConfigurations   =>

mappings.put(CacheType.REDIS, RedisCacheConfiguration.class);

RedisCacheConfiguration:     @Bean  cacheManager   

配置 redis为缓存

spring:
  redis:
    host: 127.0.0.1
    port: 6379
  cache:
    type: redis

 

启动类开启缓存功能

@EnableCaching

使用注解完成缓存@Cacheable({"A","B"})

有缓存就不用进入方法

 

 

 

默认OK

 

自定义规则

1.指定生存缓存的key

 @Cacheable(value = {"A"},key = "#root.methodName"

2.指定数据的存活时间

spring.cache.redis.time-to-live=3600000

3.将数据存为json  格式

复制代码
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;

/**
 * springcach配置
 */
@EnableConfigurationProperties(CacheProperties.class)
@EnableCaching
@Configuration
public class MyCacheConfig {

//    @Autowired
//    CacheProperties cacheProperties;

    /**
     * 需要将配置文件中的配置设置上
     * 1、使配置类生效
     * 1)开启配置类与属性绑定功能EnableConfigurationProperties
     *
     * @ConfigurationProperties(prefix = "spring.cache")  public class CacheProperties
     * 2)注入就可以使用了
     * @Autowired CacheProperties cacheProperties;
     * 3)直接在方法参数上加入属性参数redisCacheConfiguration(CacheProperties redisProperties)
     * 自动从IOC容器中找
     * <p>
     * 2、给config设置上
     */
    @Bean
    RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

        // 当自己往IOC注入了RedisCacheConfiguration配置类时,以下参数全都失效,需要手动设置
        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }
}
View Code
复制代码

 

 

 

 

 方法执行删除缓存

    //  删除哪个片区的(A) ,key值为什么的
    @CacheEvict(value = "A",key = "'getAll'")

 

 清除多个缓存

 

 删除分区全部数据

 

 双写模式[写入DB写入cache](有返回值的时候)

 

 

不足与解决

 

 解决缓存击穿    sync = true

 

 总结:  读多写少, (及时性,一致性)要求不高的数据  就可以用springCache

 

posted @   磕伴  阅读(82)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示