码迷,mamicode.com
首页 > 编程语言 > 详细

九、springboot整合redis二之缓冲配置

时间:2018-05-07 19:46:44      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:创建   tde   tcl   bool   jackson   http   href   lte   exce   

1.创建Cache配置类

@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {

    @Value("${redis.cache.expiration}")
    private Long expiration;
    
    /**
     * 
     * 管理缓存
     */
    @Bean
    public CacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        cacheManager.setDefaultExpiration(expiration);// 设置缓存默认过期时间(全局的)秒
        return cacheManager;
    }
    
    /**
     * 自定义生成的redis key
     * 在使用@Cacheable时,如果不指定key,则使用找个默认的key生成器生成的key
     * @return
     */
    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }
    
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

2.使用

  1.手动方式:

    Jedis工具类

@Component  
public class RedisUtil {  
    
    private Logger logger = LoggerFactory.getLogger(getClass());
      
    @Autowired
    private StringRedisTemplate stringRedisTemplate;  
      
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;  
      
    public void set(String key, String value){  
        ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();  
        boolean isExsit = this.stringRedisTemplate.hasKey(key);  
        if (isExsit) {
            logger.warn("key值已存在");
        }else{  
            ops.set(key, value);  
        }  
    }  
      
    public String get(String key){        
        return this.stringRedisTemplate.opsForValue().get(key);  
    } 
  

    /** 
     * 获取hashKey对应的所有键值 
     * @param key 键 
     * @return 对应的多个键值 
     */  
    public Map<Object, Object> hmget(String key){  
        return this.redisTemplate.opsForHash().entries(key);  
    }
    
   /** 
    * HashSet 并设置时间 
    * @param key 键 
    * @param map 对应多个键值 
    * @param time 时间(秒) 
    * @return true成功 false失败 
    */  
   public boolean hmset(String key, Map<String,Object> map, long time){    
       try {  
               this.redisTemplate.opsForHash().putAll(key, map);  
           if(time>0){  
               this.redisTemplate.expire(key,time,TimeUnit.SECONDS);  
           }  
           return true;  
       } catch (Exception e) {  
           e.printStackTrace();  
           return false;  
       }  
   } 
   
    public Boolean exists(String key) {
        Boolean result = Boolean.FALSE;
        result = this.stringRedisTemplate.hasKey(key);
        return result;
    } 
    
    public void del(String key){  
        this.stringRedisTemplate.delete(key);  
    }
    
    public void delhm(String key){  
        this.redisTemplate.delete(key);  
    }
    
     public boolean lock(String key, int seconds) {
         boolean result = redisTemplate.getConnectionFactory().getConnection().setNX(key.getBytes(), "LOCKED".getBytes());
         redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
         
         return result;
     }
} 

  2.自动方式:

    添加@Cacheable注解实现缓存添加

    添加@CacheEvict注解实现缓存删除

   具体实现参考上一篇文章:八、springboot整合redis

 

九、springboot整合redis二之缓冲配置

标签:创建   tde   tcl   bool   jackson   http   href   lte   exce   

原文地址:https://www.cnblogs.com/soul-wonder/p/9003842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!