ToB企服应用市场:ToB评测及商务社交产业平台
标题:
redis 基于SpringBoot Reids 的工具类
[打印本页]
作者:
飞不高
时间:
2022-9-17 08:42
标题:
redis 基于SpringBoot Reids 的工具类
redis 基于SpringBoot Reids 的工具类
[code]package com.mhy.springredis.utils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.geo.*;import org.springframework.data.redis.connection.RedisGeoCommands;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ZSetOperations;import org.springframework.stereotype.Component;import java.util.*;import java.util.List;import java.util.concurrent.CopyOnWriteArrayList;import java.util.concurrent.TimeUnit;@Componentpublic class RedisUtils { private RedisTemplate redisTemplate; @Autowired public void setRedisTemplate(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } /** * redis基本指令 set * @param key 键 * @param val 值 * @return 返回是否插入成功 */ public boolean set(String key, Object val){ try { redisTemplate.opsForValue().set(key,val); }catch (Exception e){ e.printStackTrace(); return false; } return true; } /** * redis基本指令 get * @param key 键值 * @return 返回值 */ public Object get(String key){ try { return redisTemplate.opsForValue().get(key); }catch (Exception e){ e.printStackTrace(); return null; } } /** * redis基本指令 move * @param key 移除的键 * @param dbIndex 那个数据库 * @return 移除的结果 */ public Boolean move(String key,int dbIndex){ try{ return redisTemplate.move(key,dbIndex); }catch (Exception e){ e.printStackTrace(); return false; } } /** * redis基本指令 exists * @param key 需要判断的键 * @return 返回是否存在这个键 */ public Boolean exists(String key){ try{ return redisTemplate.hasKey(key); }catch (Exception e){ e.printStackTrace(); return false; } } /** * redis基本指令 expire * @param key 键 * @param seconds 设置的秒数 * @return 是否设置成功 */ public Boolean expire(String key,long seconds){ try { return redisTemplate.expire(key,seconds, TimeUnit.SECONDS); }catch (Exception e){ e.printStackTrace(); return false; } } /** * redis基本指令 ttl * @param key 键 * @return 返回该键的剩余时间 -1表示没有设置时间 -2表示不存在这个键 -3表示发生错误 */ public Long ttl(String key){ try { return redisTemplate.getExpire(key,TimeUnit.SECONDS); }catch (Exception e){ e.printStackTrace(); return -3L; } } /** * redis基本指令 type * @param key 需要查看类型的键 * @return 返回类型 */ public Object type(String key){ try{ return redisTemplate.type(key); }catch (Exception e){ e.printStackTrace(); return null; } } /** * redis基本指令 append * @param key 需要追加字符串的键 * @param appendStr 需要追加的字符串 * @return 追加后的字符数 如果没有这个字符串就新建一个 */ public Integer append(String key,String appendStr){ try{ return redisTemplate.opsForValue().append(key,appendStr); }catch (Exception e){ e.printStackTrace(); return -1; } } /** * redis基本指令 strLen * @param key 需要查看字符串长度的键 * @return 字符串的长度 0表示不存在 -1表示发生错误 */ public Long strLen(String key){ try{ return redisTemplate.opsForValue().size(key); }catch (Exception e){ e.printStackTrace(); return -1L; } } /** * redis基本指令 getRange * @param key 需要查询的键 * @param start 开始位置 * @param end 结束位置 * @return 返回查询的字符串 */ public String getRange(String key,long start,long end){ try{ return redisTemplate.opsForValue().get(key,start,end); }catch (Exception e){ e.printStackTrace(); return null; } } /** * redis基本指令 incr * @param key 需要+1的键 * @return 返回+1后的值 */ public Long incr(String key){ return redisTemplate.opsForValue().increment(key); } /** * redis基本指令 decr * @param key 需要-1的键 * @return 返回-1后的值 */ public Long decr(String key){ return redisTemplate.opsForValue().decrement(key); } /** * redis基本指令 incrBy * @param key 需要的键 * @param val 加的值 * @return 加后的值 */ public Long incrBy(String key,Long val){ return redisTemplate.opsForValue().increment(key,val); } /** * redis基本指令 decrBy * @param key 需要的键 * @param val 减的值 * @return 减后的值 */ public Long decrBy(String key,Long val){ return redisTemplate.opsForValue().decrement(key,val); } /** * redis基本指令 setRange * @param key 需要修改的键 * @param index 下标 * @param val 修改的值 * @return 返回是否修改成功 */ public Boolean setRange(String key,Long index,String val){ try{ redisTemplate.opsForValue().set(key,val,index); return true; }catch (Exception e){ e.printStackTrace(); return false; } } /** * redis基本指令 setEx * @param key 设置的键 * @param val 设置的值 * @param time 设置是时间 单位秒 * @return 返回是否设置成功 */ public Boolean setEx(String key,String val,Long time){ try{ redisTemplate.opsForValue().set(key,val,time,TimeUnit.SECONDS); return true; }catch (Exception e){ e.printStackTrace(); return false; } } /** * redis基本指令 存在这个键就失败 setNx * @param key 需要设置的键 * @param val 需要设置的值 * @return 返回是否设置成功 */ public Boolean setNx(String key,Object val){ try{ return redisTemplate.opsForValue().setIfAbsent(key,val); }catch (Exception e){ e.printStackTrace(); return false; } } /** * redis基本指令 mSet * @param map 需要批量增加的map集合 * @return 返回是否成功 */ public Boolean mSet(Map map){ try{ redisTemplate.opsForValue().multiSet(map); return true; }catch (Exception e){ e.printStackTrace(); return false; } } /** * redis基本指令 mGet * @param list 批量查询的键集合 * @return 返回值的集合 */ public List mGet(List list){ try{ return redisTemplate.opsForValue().multiGet(list); }catch (Exception e){ e.printStackTrace(); return null; } } /** * redis基本指令 getSet * @param key 获取(修改)的键 * @param val 修改的值 * @return 返回修改之前的值 */ public Object getSet(String key,String val){ try{ return redisTemplate.opsForValue().getAndSet(key,val); }catch (Exception e){ e.printStackTrace(); return null; } } /** * List命令 lPush 采用的头插入 * @param key 存入的键 * @param val 存入的值 * @return 存入后集合的个数 */ public Long lPush(String key,Object val){ try{ return redisTemplate.opsForList().leftPush(key,val); }catch (Exception e){ e.printStackTrace(); return 0L; } } /** * List命令 rPush 采用的尾插入 * @param key 存入的键 * @param val 存入的值 * @return 存入后集合的个数 */ public Long rPush(String key,Object val){ try{ return redisTemplate.opsForList().rightPush(key,val); }catch (Exception e){ e.printStackTrace(); return 0L; } } /** * List命令 lRange * @param key 获取集合的键 * @param start 开始的位置从0开始 * @param end 结束的位置 * @return 返回list的集合数据 */ public List lRange(String key,long start,long end){ try{ return redisTemplate.opsForList().range(key,start,end); }catch (Exception e){ e.printStackTrace(); return null; } } /** * List命令 lPop * @param key 需要弹出的键 * @return 返回头部元素 */ public Object lPop(String key){ try { return redisTemplate.opsForList().leftPop(key); }catch (Exception e){ e.printStackTrace(); return null; } } /** * List命令 lPop * @param key 需要弹出的键 * @return 返回尾部元素 */ public Object rPop(String key){ try { return redisTemplate.opsForList().rightPop(key); }catch (Exception e){ e.printStackTrace(); return null; } } /** * List命令 lIndex * @param key 需要查询集合的键 * @param index 查询集合的下标 * @return 这个下标的值 */ public Object lIndex(String key,long index){ try { return redisTemplate.opsForList().index(key,index); }catch (Exception e){ e.printStackTrace(); return null; } } /** * List命令 lLen * @param key 需要查看长度的键 * @return 返回这个集合的长度 */ public Long lLen(String key){ try{ return redisTemplate.opsForList().size(key); }catch (Exception e){ e.printStackTrace(); return 0L; } } /** * List命令 lRem * @param key 需要移除集合的键 * @param index 填一个数 * @param val 移除的值 * @return 返回移除的个数 */ public Long lRem(String key,Long index,Object val){ try{ return redisTemplate.opsForList().remove(key,index,val); }catch (Exception e){ e.printStackTrace(); return 0L; } } /** * List命令 lRem * @param key 需要截取的集合的键 * @param start 开始位置,0最小 * @param end 结束位置 * @return 返回是否截取成功 */ public Boolean lTrim(String key,Long start,Long end){ try{ redisTemplate.opsForList().trim(key,start,end); return true; }catch (Exception e){ e.printStackTrace(); return false; } } /** * List命令 lSet * @param key 需要修改的集合的键 * @param index 需要修改的下标 * @param val 修改的值 * @return 返回是否修改成功 */ public Boolean lSet(String key,long index,Object val){ if(lLen(key)
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4