| | |
| | | package com.hx.redis; |
| | | |
| | | import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | |
| | | /** 文件处理工具 |
| | | /** |
| | | * 文件处理工具 |
| | | * |
| | | * @author wangrenhuang |
| | | * @Date 2021-10-19 |
| | | */ |
| | |
| | | @Resource |
| | | private RedisTemplate<String, Object> redisTemplate; |
| | | |
| | | |
| | | /** |
| | | * 切换数据库 |
| | | * @param dataNumber 数据库编号 |
| | | */ |
| | | public void setDataBase(int dataNumber) { |
| | | LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory(); |
| | | if (connectionFactory != null && dataNumber != connectionFactory.getDatabase()) { |
| | | connectionFactory.setDatabase(dataNumber); |
| | | this.redisTemplate.setConnectionFactory(connectionFactory); |
| | | connectionFactory.resetConnection(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * [判断key是否存在] |
| | | * |
| | | * @param key 键 |
| | | * @return true 存在 false不存在 |
| | | */ |
| | |
| | | |
| | | /** |
| | | * [普通缓存获取] |
| | | * |
| | | * @param key 键 |
| | | * @return 值 |
| | | */ |
| | | public Object get(String key) { |
| | | return key == null ? null : redisTemplate.opsForValue().get(key); |
| | | } |
| | | |
| | | /** |
| | | * [普通缓存删除] |
| | | * |
| | | * @param key 键 |
| | | * @return 值 |
| | | */ |
| | |
| | | try { |
| | | Boolean aBoolean = redisTemplate.hasKey(key); |
| | | return aBoolean == false ? true : redisTemplate.delete(key); |
| | | }catch (Exception e){ |
| | | } catch (Exception e) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * [普通缓存放入] |
| | | * @param key 键 |
| | | * |
| | | * @param key 键 |
| | | * @param value 值 |
| | | * @return true成功 false失败 |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 普通缓存放入并设置时间 |
| | | * @param key 键 |
| | | * |
| | | * @param key 键 |
| | | * @param value 值 |
| | | * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 |
| | | * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public boolean set(String key, Object value, long time) { |
| | |
| | | } |
| | | } |
| | | |
| | | } |
| | | /** |
| | | * 26 |
| | | * 指定缓存失效时间 |
| | | * 27 |
| | | * |
| | | * @param key 键 |
| | | * 28 |
| | | * @param time 时间(秒) |
| | | * 29 |
| | | * @return 30 |
| | | */ |
| | | |
| | | 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; |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 设置到期时间 |
| | | * @param key 对应键 |
| | | * @param time 时长 |
| | | * @param timeUnit 时间单位 |
| | | * @return |
| | | */ |
| | | public boolean expire(String key, long time, TimeUnit timeUnit) { |
| | | try { |
| | | if (time > 0) { |
| | | return redisTemplate.expire(key, time, timeUnit); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return false; |
| | | |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * hash 删除操作 |
| | | * |
| | | * @param key 键 |
| | | * @param valueKey 值 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public boolean hashDel(String key, Object... valueKey) { |
| | | try { |
| | | redisTemplate.opsForHash().delete(key,valueKey); |
| | | return true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * hash 设置hashMapt操作 |
| | | * |
| | | * @param key 键 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public boolean hashSetMap(String key, Map<String,Object> map) { |
| | | try { |
| | | redisTemplate.opsForHash().putAll(key, map); |
| | | return true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * hash 设置单个hashMap操作 |
| | | * |
| | | * @param key 键 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public boolean hashSet(String hashKey, String key, Object value) { |
| | | try { |
| | | redisTemplate.opsForHash().put(hashKey,key,value); |
| | | return true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * hash 获取整个hashKey数据 |
| | | * |
| | | * @param hashKey 键 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public Map<Object, Object> hashGetAll(String hashKey) { |
| | | try { |
| | | return redisTemplate.opsForHash().entries(hashKey); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * hash 获取单个hashKey |
| | | * |
| | | * @param hashKey 键 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public Object hashGet(String hashKey,Object key) { |
| | | try { |
| | | return redisTemplate.opsForHash().get(hashKey,key); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * list 新增-左插入 |
| | | * |
| | | * @param listKey 键 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public Object leftPush(String listKey,Object value) { |
| | | try { |
| | | return redisTemplate.opsForList().leftPush(listKey,value); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * list 新增-左插入(存在才插入) |
| | | * |
| | | * @param listKey 键 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public Object leftPushIfPresent(String listKey,Object value) { |
| | | try { |
| | | return redisTemplate.opsForList().leftPushIfPresent(listKey,value); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * list 新增-右插入(存在才插入) |
| | | * |
| | | * @param listKey 键 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public Object rightPushIfPresent(String listKey,Object value) { |
| | | try { |
| | | return redisTemplate.opsForList().rightPushIfPresent(listKey,value); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * list 新增-右插入 |
| | | * |
| | | * @param listKey 键 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public Object rightPush(String listKey,Object value) { |
| | | try { |
| | | return redisTemplate.opsForList().rightPush(listKey,value); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * list 弹出-左 |
| | | * |
| | | * @param listKey 键 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public Object leftPop(String listKey) { |
| | | try { |
| | | return redisTemplate.opsForList().leftPop(listKey); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * list 弹出-右 |
| | | * |
| | | * @param listKey 键 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public Object rightPop(String listKey) { |
| | | try { |
| | | return redisTemplate.opsForList().rightPop(listKey); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * list 获取索引下的值 |
| | | * |
| | | * @param key 键 |
| | | * @param index 索引 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public Object listGet(String key, long index) { |
| | | try { |
| | | return redisTemplate.opsForList().index(key,index); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * list 获取索引下的值 |
| | | * |
| | | * @param key 键 |
| | | * @param start 开始位置 0是开始位置 |
| | | * @param end 结束位置,-1返回所有 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public List<Object> listGetRange(String key, long start , long end) { |
| | | try { |
| | | return redisTemplate.opsForList().range(key, start, end); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * list 获取长度 |
| | | * |
| | | * @param listKey 键 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public Object listSize(String listKey) { |
| | | try { |
| | | return redisTemplate.opsForList().size(listKey); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * list 删除 |
| | | * |
| | | * @param listKey 键 |
| | | * @param index index=0, 删除所有值等于value的元素; index>0, 从头部开始删除第一个值等于value的元素; index<0, 从尾部开始删除第一个值等于value的元素 |
| | | * @param value 删除的值 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public Object listRemove(String listKey,long index,Object value) { |
| | | try { |
| | | return redisTemplate.opsForList().remove(listKey, index, value); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @param key 键 |
| | | * @param value 值 |
| | | * @param timeOut 时间 |
| | | * @param unit 时间单位 |
| | | */ |
| | | public Boolean setIfAbsent(String key, Object value, long timeOut, TimeUnit unit) { |
| | | try { |
| | | return redisTemplate.opsForValue().setIfAbsent(key, value, timeOut, unit); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | } |