|
@@ -1,306 +0,0 @@
|
|
|
-package com.lantone.external.util;
|
|
|
-
|
|
|
-import org.apache.commons.collections4.CollectionUtils;
|
|
|
-import org.apache.commons.collections4.MapUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import java.util.*;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-/**
|
|
|
- * @description: redis工具类
|
|
|
- * @author: zhoutg
|
|
|
- * @time: 2020/8/11 19:52
|
|
|
- */
|
|
|
-@Component
|
|
|
-public class RedisUtil {
|
|
|
-
|
|
|
- @Autowired
|
|
|
- @Qualifier("redisTemplateForTable")
|
|
|
- RedisTemplate redisTemplate;
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据指定key获取value
|
|
|
- *
|
|
|
- * @param key 键
|
|
|
- */
|
|
|
- public <T> T get(String key) {
|
|
|
- return (T) redisTemplate.opsForValue().get(key);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据指定key设置obj
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @param obj
|
|
|
- */
|
|
|
- public void set(String key, Object obj) {
|
|
|
- redisTemplate.opsForValue().set(key, obj);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据指定key设置obj
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @param obj
|
|
|
- */
|
|
|
- public boolean setFlag(String key, Object obj) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForValue().set(key, obj);
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 批量获取
|
|
|
- *
|
|
|
- * @param keys
|
|
|
- * @return
|
|
|
- */
|
|
|
- public <T> List<T> multiGet(Collection<String> keys) {
|
|
|
- return redisTemplate.opsForValue().multiGet(keys);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 批量设置
|
|
|
- *
|
|
|
- * @param map
|
|
|
- * @return
|
|
|
- */
|
|
|
- public void multiSet(Map<String, Object> map) {
|
|
|
- redisTemplate.opsForValue().multiSet(map);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新指定的数据
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @param str
|
|
|
- */
|
|
|
- public void updateValue(String key, String str) {
|
|
|
- redisTemplate.opsForValue().set(key, str);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除key
|
|
|
- *
|
|
|
- * @param key
|
|
|
- */
|
|
|
- public void delete(String key) {
|
|
|
- redisTemplate.delete(key);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 批量删除key
|
|
|
- *
|
|
|
- * @param keys
|
|
|
- */
|
|
|
- public void delete(Collection<String> keys) {
|
|
|
- redisTemplate.delete(keys);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据前缀删除key
|
|
|
- *
|
|
|
- * @param prex
|
|
|
- */
|
|
|
- public void deleteByPrex(String prex) {
|
|
|
- prex = prex + "**";
|
|
|
- Set<String> keys = getKeyList(prex);
|
|
|
- if (CollectionUtils.isNotEmpty(keys)) {
|
|
|
- redisTemplate.delete(keys);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据前缀删除key
|
|
|
- *
|
|
|
- * @param prex
|
|
|
- */
|
|
|
- public <T> List<T> getByPrex(String prex) {
|
|
|
- prex = prex + "**";
|
|
|
- Set<String> keys = getKeyList(prex);
|
|
|
- if (CollectionUtils.isNotEmpty(keys)) {
|
|
|
- return multiGet(keys);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据正则key获取value列表
|
|
|
- *
|
|
|
- * @param pattern 键
|
|
|
- */
|
|
|
- public <T> List<T> getByRegex(String pattern) {
|
|
|
- Set<String> keys = getKeyList(pattern);
|
|
|
- return multiGet(keys);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查找匹配的key
|
|
|
- *
|
|
|
- * @param pattern
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Set<String> getKeyList(String pattern) {
|
|
|
- return redisTemplate.keys(pattern);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 序列化key
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @return
|
|
|
- */
|
|
|
- public byte[] dump(String key) {
|
|
|
- return redisTemplate.dump(key);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 是否存在key
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Boolean hasKey(String key) {
|
|
|
- return redisTemplate.hasKey(key);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置过期时间
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @param timeout
|
|
|
- * @param unit
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Boolean expire(String key, long timeout, TimeUnit unit) {
|
|
|
- return redisTemplate.expire(key, timeout, unit);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置过期时间
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @param date
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Boolean expireAt(String key, Date date) {
|
|
|
- return redisTemplate.expireAt(key, date);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 移除 key 的过期时间,key 将持久保持
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Boolean persist(String key) {
|
|
|
- return redisTemplate.persist(key);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 返回 key 的剩余的过期时间
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @param unit
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Long getExpire(String key, TimeUnit unit) {
|
|
|
- return redisTemplate.getExpire(key, unit);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 返回 key 的剩余的过期时间
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Long getExpire(String key) {
|
|
|
- return redisTemplate.getExpire(key);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 修改 key 的名称
|
|
|
- *
|
|
|
- * @param oldKey
|
|
|
- * @param newKey
|
|
|
- */
|
|
|
- public void rename(String oldKey, String newKey) {
|
|
|
- redisTemplate.rename(oldKey, newKey);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 仅当 newkey 不存在时,将 oldKey 改名为 newkey
|
|
|
- *
|
|
|
- * @param oldKey
|
|
|
- * @param newKey
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Boolean renameIfAbsent(String oldKey, String newKey) {
|
|
|
- return redisTemplate.renameIfAbsent(oldKey, newKey);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置指定 key 的值
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @param value
|
|
|
- */
|
|
|
- public void set(String key, String value) {
|
|
|
- redisTemplate.opsForValue().set(key, value);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将值 value 关联到 key ,并将 key 的过期时间设为 timeout
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @param value
|
|
|
- * @param timeout 过期时间
|
|
|
- * @param unit 时间单位, 天:TimeUnit.DAYS 小时:TimeUnit.HOURS 分钟:TimeUnit.MINUTES
|
|
|
- * 秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDS
|
|
|
- */
|
|
|
- public void setEx(String key, Object value, long timeout, TimeUnit unit) {
|
|
|
- redisTemplate.opsForValue().set(key, value, timeout, unit);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 只有在 key 不存在时设置 key 的值
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @param value
|
|
|
- * @return 之前已经存在返回false, 不存在返回true
|
|
|
- */
|
|
|
- public boolean setIfAbsent(String key, String value) {
|
|
|
- return redisTemplate.opsForValue().setIfAbsent(key, value);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * map集合的形式添加键值对
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @param map
|
|
|
- */
|
|
|
- public void putHashMap(String key, Map<String, Object> map) {
|
|
|
- if (MapUtils.isNotEmpty(map)) {
|
|
|
- redisTemplate.opsForHash().putAll(key, map);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取集合中指定field的内容
|
|
|
- * @param key
|
|
|
- * @param field
|
|
|
- * @param <T>
|
|
|
- * @return
|
|
|
- */
|
|
|
- public <T> T getByKeyAndField(String key, String field) {
|
|
|
- return (T)redisTemplate.opsForHash().get(key, field);
|
|
|
- }
|
|
|
-}
|