redis 做默认缓存

Posted lshan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis 做默认缓存相关的知识,希望对你有一定的参考价值。

 

 

配置:

技术图片
server.port=9999
# REDIS (RedisProperties)
# Redisu6570u636Eu5E93u7D22u5F15uFF08u9ED8u8BA4u4E3A0uFF09
spring.redis.database=0  
# Redisu670Du52A1u5668u5730u5740
spring.redis.host=127.0.0.1
# Redisu670Du52A1u5668u8FDEu63A5u7AEFu53E3
spring.redis.port=6379  
# Redisu670Du52A1u5668u8FDEu63A5u5BC6u7801uFF08u9ED8u8BA4u4E3Au7A7AuFF09
spring.redis.password=  
# u8FDEu63A5u6C60u6700u5927u8FDEu63A5u6570uFF08u4F7Fu7528u8D1Fu503Cu8868u793Au6CA1u6709u9650u5236uFF09
spring.redis.pool.max-active=8  
# u8FDEu63A5u6C60u6700u5927u963Bu585Eu7B49u5F85u65F6u95F4uFF08u4F7Fu7528u8D1Fu503Cu8868u793Au6CA1u6709u9650u5236uFF09
spring.redis.pool.max-wait=-1  
# u8FDEu63A5u6C60u4E2Du7684u6700u5927u7A7Au95F2u8FDEu63A5
spring.redis.pool.max-idle=8  
# u8FDEu63A5u6C60u4E2Du7684u6700u5C0Fu7A7Au95F2u8FDEu63A5
spring.redis.pool.min-idle=0  
# u8FDEu63A5u8D85u65F6u65F6u95F4uFF08u6BEBu79D2uFF09
spring.redis.timeout=0 
View Code

 

 

 

技术图片
 **************************************************************************
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public CacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        cacheManager.setDefaultExpiration(10);
        return cacheManager;
    }
    
    
    
    
    
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
       RedisTemplate<Object, Object> template = new RedisTemplate<>();
       template.setConnectionFactory(connectionFactory);
       //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
       Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
       ObjectMapper mapper = new ObjectMapper();
       mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
       serializer.setObjectMapper(mapper);
       template.setValueSerializer(serializer);
       
       template.setKeySerializer(new JdkSerializationRedisSerializer());
       template.afterPropertiesSet();
       return template;
    }
}
View Code

 

 

使用:

技术图片
    @Cacheable(cacheNames = {"cacheManager"}, cacheManager = "cacheManager")
    public String getvalue(){
        
        System.err.println("hahhhahahaahhahahahahahahahahahahahahahahahah");
        System.err.println("hahhhahahaahhahahahahahahahahahahahahahahahah");
        System.err.println("hahhhahahaahhahahahahahahahahahahahahahahahah");
        System.err.println("hahhhahahaahhahahahahahahahahahahahahahahahah");
        String aa="nihao ";
        return aa;
    }
    
View Code

 

 

  •  
    @Cacheable(value="users", key="#id")
  •  
    public User find(Integer id) {
  •  
    returnnull;
  •  
    }

 

以上是关于redis 做默认缓存的主要内容,如果未能解决你的问题,请参考以下文章

django-redis结合drf实现缓存

使用redis做mybaties的二级缓存-Mybatis 二级缓存小心使用

Springboot2.x+shiro+redis整合填坑 redis只做缓存的情况

springboot 用redis做缓存。缓存可以用。可是存对象的时候,只有key,没有值,是?

我配置了redis注解缓存,为啥不起作用

mybatis二级缓存redis,update数据库表的时候,为啥会清空redis数据库???