西木-Lee

 

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);
    }
}

 

posted on 2018-09-05 17:16  西木-Lee  阅读(814)  评论(0编辑  收藏  举报

导航