• RedisUtil


    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.stereotype.Component;
    
    import java.io.Serializable;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    
    @Component(value = "redisUtil")
    @Slf4j
    public class RedisUtil {
        @SuppressWarnings("rawtypes")
        @Autowired
        private RedisTemplate redisTemplate;
    
        /**
         * 批量删除对应的value
         *
         * @param keys
         */
        public void remove(final String... keys) {
            for (String key : keys) {
                remove(key);
            }
        }
    
        /**
         * 批量删除key
         *
         * @param pattern
         */
        @SuppressWarnings("unchecked")
        public void removePattern(final String pattern) {
            Set<Serializable> keys = redisTemplate.keys(pattern);
            if (keys.size() > 0) {
                redisTemplate.delete(keys);
            }
        }
    
        /**
         * 删除对应的value
         *
         * @param key
         */
        @SuppressWarnings("unchecked")
        public void remove(final String key) {
            if (exists(key)) {
                redisTemplate.delete(key);
            }
        }
    
        /**
         * 判断缓存中是否有对应的value
         *
         * @param key
         * @return
         */
        @SuppressWarnings("unchecked")
        public boolean exists(final String key) {
            return redisTemplate.hasKey(key);
        }
    
        /**
         * 读取缓存
         *
         * @param key
         * @return
         */
        @SuppressWarnings("unchecked")
        public Object get(final String key) {
            Object result = null;
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            result = operations.get(key);
            return result;
        }
    
        /**
         * 写入缓存
         *
         * @param key
         * @param value
         * @return
         */
        @SuppressWarnings("unchecked")
        public boolean set(final String key, Object value) {
            boolean result = false;
            try {
                ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
                operations.set(key, value);
                result = true;
            } catch (Exception e) {
                log.error(" set redis err:key {},value {}", key, value, e);
            }
            return result;
        }
    
        /**
         * 写入缓存
         *
         * @param key
         * @param value
         * @return
         */
        @SuppressWarnings("unchecked")
        public boolean set(final String key, Object value, Long expireTime) {
            boolean result = false;
            try {
                ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
                operations.set(key, value);
                redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
                result = true;
            } catch (Exception e) {
                log.error(" set redis err:key {},value {}", key, value, e);
            }
            return result;
        }
    
    
    }
  • 相关阅读:
    流体力学笔记 第二章 流体力学的基本概念
    jvm常用的参数
    链表的反转
    数据流中的中位数
    二叉树对称
    二叉树镜像
    输入框校验
    判断单选或者复选框中选中的值
    网页中window.open 弹出 父页面和子页面数值交互
    数组去重
  • 原文地址:https://www.cnblogs.com/jpejie/p/9577117.html
Copyright © 2020-2023  润新知