zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
    温故而知新
  • 相关阅读:
    HTML_from
    HTML_img
    python_Django默认转换器
    python_虚拟环境
    python_正则表达式
    mysql_pymysql模块
    mysql_权限管理
    mysql_子查询
    sudo权限造成的故障
    22.Linux定时任务
  • 原文地址:https://www.cnblogs.com/Uzai/p/10985567.html
Copyright © 2011-2022 走看看