Set redisTemplate.keys("ss:*");
//使用keys *进行模糊匹配引发Redis锁(因为redis单线程,keys会阻塞),造成Redis锁住,CPU飙升,引起了所有调用链路的超时并且卡住,其他的redis请求都会被堵塞;业务量大的时候会出问题,可使用scan代替
2、ValueOperations 字符串
1、 获取
ValueOperations redisTemplate.opsForValue();
2、 set
void set(K key, V value);//设置过期时间,Duration.ofDays(1L)
void set(K key, V value, Duration timeout);//设置过期时间
void set(K key, V value, final long timeout, final TimeUnit unit)//替换从指定位置开始的字串
void set(K key, V value, long offset)//set value,返回set之前的值
V getAndSet(K key, V newValue)//不存在才会set进去,返回:不存在-true;存在-false
Boolean setIfAbsent(K key, V value)
Boolean setIfAbsent(K key, V value, Duration timeout)
Boolean setIfAbsent(K key, V value, long timeout, TimeUnit unit)
3、 get
//当其他工程需要该实体类进行反序列化时要满足:1.serialVersionUID要一致;2.反序列的对象和反序列化的对象所属的包名一致V get(Object key)//获取指定位置的子串
String get(K key, long start, long end);//根据key集合获取
List multiGet(Collection keys);
/*** 异常:JedisDataException: ERR value is not an integer or out of range,该异常就是使用该api时,序列化策略有问题* * GenericJackson2JsonRedisSerializer、Jackson2JsonRedisSerializer:是先将对象转为json,然后再保存到redis,value在redis中是字符串,无法进行加、减* JdkSerializationRedisSerializer使用的jdk对象序列化,序列化后的值有类信息、版本号等,是一个包含很多字母的字符串,无法加、减* GenericToStringSerializer、StringRedisSerializer将字符串的值直接转为字节数组,保存到redis中是数字,可以进行加、减** 解决:* 1.使用StringRedisTemplate* 2.redis全局设置的value序列化无法修改, 先局部设置value的序列化,再set、加减存入的value一定要是String类型,不要Long、Double、Integer:valueOperations.set(key,String.valueOf(batch));*///加,返回加之后的值
//+1
Long increment(String key);Long increment(String key, long delta);
Double increment(String key, double delta);//减,返回减之后的值
//-1
Long decrement(K key);Long decrement(K key, long delta);
//左侧
//返回添加后集合的长度
Long leftPush(key, value)
Long leftPushAll(K key, V... values)
Long leftPushAll(K key, Collection values)//如果key存在则添加,否则返回0
Long leftPushIfPresent(K key, V value)//右侧~左侧//指定下标处添加
void set(K key, final long index, V value)
5、获取指定区间的元素
//start、end:首尾下标;0 - -1获取集合的所有元素
List range(key,long start, long end)
6、删除并返回元素
//左侧
V leftPop(key)
V leftPop(K key, long timeout, TimeUnit unit)
//如果在设置的超时时间内集合中仍为空,则退出//右侧~左侧//右侧移出一个元素,添加到另一个集合左侧
//返回移出元素
V rightPopAndLeftPush(K sourceKey, K destinationKey)
V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout,TimeUnit unit)
7、删除元素
//根据值、数量,返回删除的元素数量
Long remove(K key, long count, Object value)
//count=0:删除元素值=value的所有元素
//count>0:删除从左侧开始的count个元素
//count<0:删除从右侧开始的count个元素//保留指定区间元素,其余删除
void trim(K key, final long start, final long end)
//返回添加后的集合长度
//添加的元素是集合中存在的元素时,不会添加
Long add(K key, V... values)
5、移除元素
//返回移除后的集合长度
Long remove(K key, Object... values)
6、获取所有元素
//key不存在或没有一个元素时,set的=empty,!=null
Set members(key)
7、随机获取集合中元素
//1个
V randomMember(String key)//多个
List randomMembers(String key, long count)//多个并去重,如果指定2个,去重后可能返回的是一个元素
Set distinctRandomMembers(K key, long count)
8、将一个集合中的元素移动至另一个集合
Boolean move(String key, V value, String destKey)
9、移除并返回几个随机的元素
//1个
V pop(K key)//多个
List pop(K key, long count)
10、交集
//返回key与otherKey的交集
Set intersect(String key, String otherKey)//返回key与otherKey多个集合的交集
Set intersect(K key, Collection otherKeys)//key与otherKey集合交集存入destKey集合中,返回交集元素个数
Long intersectAndStore(String key, String otherKey, String destKey)//key与otherKey多个集合交集存入destKey集合中,返回交集元素个数
Long intersectAndStore(K key, Collection otherKeys, K destKey)
11、并集
//返回key与otherKey的并集
Set union(String key, String otherKey)//返回key与otherKey多个集合的并集
Set union(K key, Collection otherKeys)//key与otherKey集合并集存入destKey集合中,返回并集元素个数
Long unionAndStore(String key, String otherKey, String destKey)//key与otherKey多个集合并集存入destKey集合中,返回并集元素个数
Long unionAndStore(K key, Collection otherKeys, K destKey)
12、差集
//返回key与otherKey的差集
Set difference(String key, String otherKey)//返回key与otherKey多个集合的差集
Set difference(K key, Collection otherKeys)//key与otherKey集合差集存入destKey集合中,返回差集元素个数
Long differenceAndStore(String key, String otherKey, String destKey)//key与otherKey多个集合差集存入destKey集合中,返回差集元素个数
Long differenceAndStore(K key, Collection otherKeys, K destKey)
//1个
Boolean add(K key, V value, double score)//多个,TypedTuple包含value、score属性
Long add(K key, Set> tuples)
4、移除元素
//返回移除后的集合长度//根据元素值
Long remove(K key, Object... values)//根据下标
Long removeRange(K key, long start, long end)//根据score
Long removeRangeByScore(K key, double min, double max)
5、增加元素的score值
//返回增加后的score值
Double incrementScore(String key, V value, double delta)
6、查询一个元素的score
Double score(K key, Object o)
7、查询一个元素在集合中的排名
//从小到大,从0开始
Long rank(String key, Object o)//从大到小
Long reverseRank(Stirng key, Object o)
8、统计分数区间数量
//score>=min&&<=max
Long count(K key, double min, double max)
9、获取指定区间的元素
//0 - -1获取该集合的所有元素//根据下标
//先将集合元素按照score从小到大排列,获取下标 start-end的集合元素值
//返回的集合元素值按照score从小到大排列
Set range(K key, long start, long end)//先将集合元素按照score从大到小排列,获取下标 start-end的集合元素值
//返回的集合元素值按照score从大到小排列
Set reverseRange(K key, long start, long end)//先将集合元素按照score从小到大排列,获取下标 start-end的集合元素
//返回的集合元素按照score从小到大排列
Set> rangeWithScores(K key, long start, long end)//先将集合元素按照score从大到小排列,获取下标 start-end的集合元素
//返回的集合元素按照score从大到小排列
Set> reverseRangeWithScores(K key, long start, long end)//根据分数
//获取score>=min&&<=max的元素值
//返回的集合元素值按照score从小到大排列
Set reverseRangeByScore(K key, double min, double max)
//获取分数>=min&&<=max的元素值
//返回的集合元素值按照score从大到小排列
Set rangeByScore(K key, double min, double max)//先将集合元素按照score从小到大排列
//获取分数>=min&&<=max的元素值,并且从下标offset开始,获取count个
//返回的集合元素值按照score从小到大排列
Set rangeByScore(K key, double min, double max, long offset, long count)//先将集合元素按照score从大到小排列
//获取分数>=min&&<=max的元素值,并且从下标offset开始,获取count个
//返回的集合元素值按照score从大到小排列
Set reverseRangeByScore(K key,double min,double max,long offset,
long count)//获取分数>=min&&<=max的元素
//返回的集合元素按照score从小到大排列
Set> rangeByScoreWithScores(K key, double min, double max)//获取分数>=min&&<=max的元素
//返回的集合元素按照score从大到小排列
Set> reverseRangeByScoreWithScores(K key,double min,
double max)//先将集合元素按照score从小到大排列
//获取分数>=min&&<=max的元素,并且从下标offset开始,获取count个
//返回的集合元素按照score从小到大排列
Set> rangeByScoreWithScores(K key, double min, double max,
long offset, long count)//先将集合元素按照score从大到小排列
//获取分数>=min&&<=max的元素,并且从下标offset开始,获取count个
//返回的集合元素按照score从大到小排列
Set> reverseRangeByScoreWithScores(K key, double min,
double max, long offset, long count)//根据条件
Set rangeByLex(K key, Range range)
Set rangeByLex(K key, Range range, Limit limit)
//Range RedisZSetCommands.Range.range().gt(1.2)
//Limit RedisZSetCommands.Limit.limit().offset(0).count(2)
10、交集
//将key和otherKey的交集存入destKey集合中,返回交集元素个数
Long intersectAndStore(K key, K otherKey, K destKey)//将key和otherKey集合的交集存入destKey集合中,返回交集元素个数
Long intersectAndStore(K key, Collection otherKeys, K destKey)
11、并集
//key与otherKey集合并集存入destKey集合中,返回并集元素个数
Long unionAndStore(String key, String otherKey, String destKey)//key与otherKey多个集合并集存入destKey集合中,返回并集元素个数
Long unionAndStore(K key, Collection otherKeys, K destKey)
12、差集
//key与otherKey集合差集存入destKey集合中,返回差集元素个数
Long differenceAndStore(String key, String otherKey, String destKey)//key与otherKey多个集合差集存入destKey集合中,返回差集元素个数
Long differenceAndStore(K key, Collection otherKeys, K destKey)