SpringCache self-learning

cacheable put the queried data in the cache
cacheevict update -> delete mode
cacheput update -> double write mode
Two ways to write delete mode

Insert picture description here
Insert picture description here
Cacheable() -> value is the partition name and key is the cache name

Configuration class

@Configuration
@EnableCaching  //开启缓存
@EnableConfigurationProperties(CacheProperties.class)  //使用redis的容器里面的配置
public class MyCacheConfig {
    
    

   /* @Autowired
    private CacheProperties cacheProperties;*/

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
    
    
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        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;
    }
}

Some basic rules in the main configuration file
Insert picture description here

Disadvantage

There is no guarantee that the lock added by strong consistency is not a distributed lock
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36905956/article/details/112204653