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

 

posted @   Heliocc  阅读(575)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示