springboot使用本地或Redis做缓存
2022/2/2 19:42:25
本文主要是介绍springboot使用本地或Redis做缓存,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.springboot版本:1.5.13.RELEASE
2.引入如下依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency>
3.配置CacheManager
-
GuavaCacheConfig
@Configuration public class GuavaCacheConfig { @Primary @Bean("guavaCacheManager") public CacheManager guavaCacheManager(){ GuavaCacheManager manager = new GuavaCacheManager(); manager.setCacheBuilder(CacheBuilder.newBuilder() .expireAfterAccess(10, TimeUnit.SECONDS) .maximumSize(1000)); return manager; } }
-
RedisCacheConfig
@Configuration public class RedisCacheConfig { @Autowired @Qualifier("springCacheRedisTemplate") RedisTemplate redisTemplate; @Bean("redisCacheManager") public CacheManager redisCacheManager(){ RedisCacheManager manager = new RedisCacheManager(redisTemplate); manager.setDefaultExpiration(10 * 60); // 设置过期时间30秒 return manager; } @Bean("springCacheRedisTemplate") public RedisTemplate redisTemplate(RedisConnectionFactory factory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); template.setKeySerializer(jackson2JsonRedisSerializer); template.setValueSerializer(jackson2JsonRedisSerializer); template.setHashKeySerializer(jackson2JsonRedisSerializer); template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); template.setEnableTransactionSupport(true); return template; } }
3.使用缓存
在启动类上添加@EnableCaching注解开启缓存
@EnableCaching @SpringBootApplication @MapperScan("com.xys.springboot.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
4.使用缓存
@Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Override @Cacheable( value = "user-cache", key = "#id", cacheManager = "guavaCacheManager") public User getUserByCache(Integer id) { return this.getById(id); } @Override @Cacheable( value = "user-cache", key = "'user:username:' + #username", cacheManager = "redisCacheManager") public User getUserByCache(String username) { return this.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username)); } }
这篇关于springboot使用本地或Redis做缓存的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南