zoukankan      html  css  js  c++  java
  • springboot整合redis 配置文件及配置类(二)

    /**
     * redis
     * 工具类
     *
     */
    @Component
    public class RedisUtil {
        
        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
    
        /**
         * 指定缓存失效时间
         */
        public boolean expire(String key, long time) {
            try {
                if (time > 0) {
                    redisTemplate.expire(key, time, TimeUnit.SECONDS);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }/**
         * 判断key是否存在
         */
        public boolean hasKey(String key) {
            try {
                return redisTemplate.hasKey(key);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 删除缓存
         *
         * @param key 可以传一个值 或多个
         */
        @SuppressWarnings("unchecked")
        public void del(String... key) {
            if (key != null && key.length > 0) {
                if (key.length == 1) {
                    redisTemplate.delete(key[0]);
                } else {
                    redisTemplate.delete(CollectionUtils.arrayToList(key));
                }
            }
        }
    
        /**
         * 普通缓存获取
         * @param key 键
         * @return 值
         */
        public Object get(String key) {
            return key == null ? null : redisTemplate.opsForValue().get(key);
        }
    
        /**
         * 普通缓存放入
         *
         * @param key   键
         * @param value 值
         * @return true成功 false失败
         */
        public boolean set(String key, Object value) {
            try {
                redisTemplate.opsForValue().set(key, value);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
    
        }/**
         * 通过key  获取value
         * @param key
         * @return
         */
        public List<Object> hashList(String key) {
            List<Object> hashList = redisTemplate.opsForHash().values(key);
            return hashList;
        }
        
        /**
         * 通过key获取所有的键值
         * @param key
         * @return
         */
        public Set<Object> keySet(String key) {
            Set<Object> keySet = redisTemplate.opsForHash().keys(key);  
            return keySet;
        }
    }

    更多方法请查看文档https://redis.io/commands

  • 相关阅读:
    fopen flock fclose 文件用法
    thinkphp并发 阻塞模式与非阻塞模式
    thinkphp3.2 控制器导入模型
    thinkphp3.2 session时间周期无效
    UWP滑动后退
    旺信UWP公测邀请
    旺信UWP倒计时
    UWP应用开发系列视频教程简介
    新浪微博UWP UI意见征求
    淘宝UWP--自定义图片缓存
  • 原文地址:https://www.cnblogs.com/ch94/p/14741384.html
Copyright © 2011-2022 走看看