springboot redisTemplate 外部反序列化


import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 外部反序列化
 *
 */
@Component
public class RedisTools<T> {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 存储
     *
     * @param key
     * @param instance 类对象
     * @param timeOut 超时时间 单位/小时
     */
    public void set(String key, T instance, int timeOut){
        String value = JSON.toJSONString(instance);
        redisTemplate.opsForValue().set(key, value, timeOut, TimeUnit.HOURS);
    }

    /**
     * 获取
     *
     * @param key
     * @param clazz
     */
    public T get(String key, Class<T> clazz){
        String value = redisTemplate.boundValueOps(key).get();
        if(value == null) return null;
        return JSON.parseObject(value, clazz);
    }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM