zoukankan      html  css  js  c++  java
  • spring boot 2.0.4 Redis缓存配置

    spring boot 2 使用RedisTemplate操作redis存取对象时,需要先进行序列化操作

    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.annotation.EnableCaching;
    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 java.time.Duration;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * Redis缓存配置类
     *
     * @author lee
     */
    @Configuration
    @EnableCaching
    public class RedisConfig extends CachingConfigurerSupport {
    
        //缓存管理器
        @Bean
        public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
            //user信息缓存配置
            RedisCacheConfiguration userCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10)).disableCachingNullValues().prefixKeysWith("user");
            Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
            redisCacheConfigurationMap.put("user", userCacheConfiguration);
            //初始化一个RedisCacheWriter
            RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
    
    
            RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
    
            //设置默认超过期时间是30秒
            defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
            //初始化RedisCacheManager
            RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap);
            return cacheManager;
        }
    }

    包装RedisTemplate,把常用操作写一个redis工具类

    import com.alibaba.fastjson.JSON;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Service;
    
    import java.util.concurrent.TimeUnit;
    
    @Service
    public class RedisService {
    
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
        @Autowired
        private RedisTemplate redisTemplate;
    
        public <T> T getJson2Object(String key, Class<T> clazz) {
    
            return JSON.parseObject(stringRedisTemplate.opsForValue().get(key), clazz);
        }
    
        public <T> void setObject(String key, T clazz) {
            redisTemplate.opsForValue().set(key, clazz);
        }
    
        /**
         * 设置对象,含有效期,单位为秒
         */
        public <T> void setObject(String key, T clazz, long timeout) {
            redisTemplate.opsForValue().set(key, clazz, timeout, TimeUnit.SECONDS);
        }
    
        /**
         * 设置对象,含有效期,单位可以自定义
         */
        public <T> void setObject(String key, T clazz, long timeout, TimeUnit unit) {
    
            redisTemplate.opsForValue().set(key, clazz, timeout, unit);
    
        }
    
        @SuppressWarnings("unchecked")
        public <T> T getObject(String key, Class<T> clazz) {
    
            return (T) redisTemplate.opsForValue().get(key);
    
        }
    
        public String getString(String key) {
            return stringRedisTemplate.opsForValue().get(key);
        }
    
        public void setString(String key, String value) {
            stringRedisTemplate.opsForValue().set(key, value);
        }
    }
  • 相关阅读:
    [ 转载 ] Java基础4--Java中的static关键字解析
    [ 原创 ]新手作品-我的第一款安卓自学作品---YY音乐诞生了
    [ 转载 ]学习笔记-svn用法详解
    [ 原创 ]学习笔记-Android 学习笔记 Contacts (一)ContentResolver query 参数详解 [转载]
    [ 原创 ]学习笔记-Android 中关于Cursor类的介绍
    《JavaScript DOM 编程艺术 第 2 版》
    test-ra
    2019-4-22
    《写给大家看的设计书》
    2018-5
  • 原文地址:https://www.cnblogs.com/lixyu/p/9593311.html
Copyright © 2011-2022 走看看