前言
看到同事手动写的缓存代码,我实在是看不下去了,于是决定改造一下,最终达到自动Redis缓存,不用手动去set了。
使用场景
实时性要求不那么高的业务数据,我们可以在Service上进行一些缓存的操作。这样就可以减少访问数据库的频率。(脱离的场景的技术都是在纸上谈兵)
实战
1 话不多说,上代码自动化配置
源码
import java.time.Duration;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.sprin