cacheable put the queried data in the cache
cacheevict update -> delete mode
cacheput update -> double write mode
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
Disadvantage
There is no guarantee that the lock added by strong consistency is not a distributed lock