packagecom.handsight.platform.fras.service.impl;importjava.util.Collection;importjava.util.List;importjava.util.Map;importjava.util.Set;importjava.util.concurrent.TimeUnit;importjavax.annotation.Resource;importorg.springframework.data.redis.core.HashOperations;importorg.springframework.data.redis.core.RedisTemplate;importorg.springframework.data.redis.core.ZSetOperations.TypedTuple;importorg.springframework.stereotype.Service;importcom.handsight.platform.fras.constant.Constants;importcom.handsight.platform.fras.mgt.pojo.T_user;importcom.handsight.platform.fras.service.RedisService;/***@authorCarey 2018年1月23日
*
* redis 操作工具类*/@Servicepublic class RedisServiceImpl implementsRedisService {
@Resource(name= "FrasRedisTemplate")private RedisTemplateredisTemplate;public RedisTemplategetInstance() {returnredisTemplate;
}/***
* 判断key是否存在
*
*@paramkey
*@return
*/@Overridepublic booleanhasKey(String key) {returnredisTemplate.hasKey(key);
}/*** 设置 String 类型 key-value
*
*@paramkey
*@paramvalue*/@Overridepublic voidset(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}/*** 获取 String 类型 key-value
*
*@paramkey
*@return
*/@OverridepublicString get(String key) {return(String) redisTemplate.opsForValue().get(key);
}/*** 设置 String 类型 key-value 并添加过期时间 (毫秒单位)
*
*@paramkey
*@paramvalue
*@paramtime 过期时间,毫秒单位*/@Overridepublic void setForTimeMS(String key, String value, longtime) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.MILLISECONDS);
}/*** 设置 String 类型 key-value 并添加过期时间 (分钟单位)
*
*@paramkey
*@paramvalue
*@paramtime 过期时间,分钟单位*/
public void setForTimeMIN(String key, String value, longtime) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.MINUTES);
}/*** 设置 String 类型 key-value 并添加过期时间 (分钟单位)
*
*@paramkey
*@paramvalue
*@paramtime 过期时间,分钟单位*/
public void setForTimeCustom(String key, String value, longtime, TimeUnit type) {
redisTemplate.opsForValue().set(key, value, time, type);
}/*** 如果 key 存在则覆盖,并返回旧值. 如果不存在,返回null 并添加
*
*@paramkey
*@paramvalue
*@return
*/
publicString getAndSet(String key, String value) {return(String) redisTemplate.opsForValue().getAndSet(key, value);
}/*** 批量添加 key-value (重复的键会覆盖)
*
*@paramkeyAndValue*/
public void batchSet(MapkeyAndValue) {
redisTemplate.opsForValue().multiSet(keyAndValue);
}/*** 批量添加 key-value 只有在键不存在时,才添加 map 中只要有一个key存在,则全部不添加
*
*@paramkeyAndValue*/
public void batchSetIfAbsent(MapkeyAndValue) {
redisTemplate.opsForValue().multiSetIfAbsent(keyAndValue);
}/*** 对一个 key-value 的值进行加减操作, 如果该 key 不存在 将创建一个key 并赋值该 number 如果 key 存在,但 value
* 不是长整型 ,将报错
*
*@paramkey
*@paramnumber*/
public Long increment(String key, longnumber) {returnredisTemplate.opsForValue().increment(key, number);
}/*** 对一个 key-value 的值进行加减操作, 如果该 key 不存在 将创建一个key 并赋值该 number 如果 key 存在,但 value 不是
* 纯数字 ,将报错
*
*@paramkey
*@paramnumber*/
public Double increment(String key, doublenumber) {returnredisTemplate.opsForValue().increment(key, number);
}/*** 给一个指定的 key 值附加过期时间
*
*@paramkey
*@paramtime
*@paramtype
*@return
*/
public boolean expire(String key, longtime, TimeUnit type) {returnredisTemplate.boundValueOps(key).expire(time, type);
}/*** 移除指定key 的过期时间
*
*@paramkey
*@return
*/
public booleanpersist(String key) {returnredisTemplate.boundValueOps(key).persist();
}/*** 获取指定key 的过期时间
*
*@paramkey
*@return
*/
publicLong getExpire(String key) {returnredisTemplate.boundValueOps(key).getExpire();
}/*** 修改 key
*
*@paramkey
*@return
*/
public voidrename(String key, String newKey) {
redisTemplate.boundValueOps(key).rename(newKey);
}/*** 删除 key-value
*@paramkey
*@return
*/@Overridepublic voiddelete(String key){
redisTemplate.delete(key);
}//hash操作
/*** 添加 Hash 键值对
*
*@paramkey
*@paramhashKey
*@paramvalue*/
public voidput(String key, String hashKey, String value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}/*** 批量添加 hash 的 键值对 有则覆盖,没有则添加
*
*@paramkey
*@parammap*/
public void putAll(String key, Mapmap) {
redisTemplate.opsForHash().putAll(key, map);
}/*** 添加 hash 键值对. 不存在的时候才添加
*
*@paramkey
*@paramhashKey
*@paramvalue
*@return
*/
public booleanputIfAbsent(String key, String hashKey, String value) {returnredisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
}/*** 删除指定 hash 的 HashKey
*
*@paramkey
*@paramhashKeys
*@return删除成功的 数量*/
publicLong delete(String key, String... hashKeys) {returnredisTemplate.opsForHash().delete(key, hashKeys);
}/*** 给指定 hash 的 hashkey 做增减操作
*
*@paramkey
*@paramhashKey
*@paramnumber
*@return
*/
public Long increment(String key, String hashKey, longnumber) {returnredisTemplate.opsForHash().increment(key, hashKey, number);
}/*** 给指定 hash 的 hashkey 做增减操作
*
*@paramkey
*@paramhashKey
*@paramnumber
*@return
*/
publicDouble increment(String key, String hashKey, Double number) {returnredisTemplate.opsForHash().increment(key, hashKey, number);
}/*** 获取指定 key 下的 hashkey
*
*@paramkey
*@paramhashKey
*@return
*/
publicObject getHashKey(String key, String hashKey) {returnredisTemplate.opsForHash().get(key, hashKey);
}/*** 获取 key 下的 所有 hashkey 和 value
*
*@paramkey
*@return
*/
public MapgetHashEntries(String key) {returnredisTemplate.opsForHash().entries(key);
}/*** 验证指定 key 下 有没有指定的 hashkey
*
*@paramkey
*@paramhashKey
*@return
*/
public booleanhashKey(String key, String hashKey) {returnredisTemplate.opsForHash().hasKey(key, hashKey);
}/*** 获取 key 下的 所有 hashkey 字段名
*
*@paramkey
*@return
*/
public SethashKeys(String key) {returnredisTemplate.opsForHash().keys(key);
}/*** 获取指定 hash 下面的 键值对 数量
*
*@paramkey
*@return
*/
publicLong hashSize(String key) {returnredisTemplate.opsForHash().size(key);
}//List 操作
/*** 指定 list 从左入栈
*
*@paramkey
*@return当前队列的长度*/@OverridepublicLong leftPush(String key, Object value) {returnredisTemplate.opsForList().leftPush(key, value);
}/*** 指定 list 从左出栈 如果列表没有元素,会堵塞到列表一直有元素或者超时为止
*
*@paramkey
*@return出栈的值*/
publicObject leftPop(String key) {returnredisTemplate.opsForList().leftPop(key);
}/*** 从左边依次入栈 导入顺序按照 Collection 顺序 如: a b c => c b a
*
*@paramkey
*@paramvalues
*@return
*/
public Long leftPushAll(String key, Collectionvalues) {returnredisTemplate.opsForList().leftPushAll(key, values);
}/*** 指定 list 从右入栈
*
*@paramkey
*@return当前队列的长度*/
publicLong rightPush(String key, Object value) {returnredisTemplate.opsForList().rightPush(key, value);
}/*** 指定 list 从右出栈 如果列表没有元素,会堵塞到列表一直有元素或者超时为止
*
*@paramkey
*@return出栈的值*/@OverridepublicObject rightPop(String key) {returnredisTemplate.opsForList().rightPop(key);
}/*** 从右边依次入栈 导入顺序按照 Collection 顺序 如: a b c => a b c
*
*@paramkey
*@paramvalues
*@return
*/
public Long rightPushAll(String key, Collectionvalues) {returnredisTemplate.opsForList().rightPushAll(key, values);
}/*** 根据下标获取值
*
*@paramkey
*@paramindex
*@return
*/
public Object popIndex(String key, longindex) {returnredisTemplate.opsForList().index(key, index);
}/*** 获取列表指定长度
*
*@paramkey
*@paramindex
*@return
*/@OverridepublicLong listSize(String key) {returnredisTemplate.opsForList().size(key);
}/*** 获取列表 指定范围内的所有值
*
*@paramkey
*@paramstart
*@paramend
*@return
*/
public List listRange(String key, long start, longend) {returnredisTemplate.opsForList().range(key, start, end);
}/*** 删除 key 中 值为 value 的 count 个数.
*
*@paramkey
*@paramcount
*@paramvalue
*@return成功删除的个数*/
public Long listRemove(String key, longcount, Object value) {returnredisTemplate.opsForList().remove(key, count, value);
}/*** 删除 列表 [start,end] 以外的所有元素
*
*@paramkey
*@paramstart
*@paramend*/
public void listTrim(String key, long start, longend) {
redisTemplate.opsForList().trim(key, start, end);
}/*** 将 key 右出栈,并左入栈到 key2
*
*@paramkey 右出栈的列表
*@paramkey2 左入栈的列表
*@return操作的值*/
publicObject rightPopAndLeftPush(String key, String key2) {returnredisTemplate.opsForList().rightPopAndLeftPush(key, key2);
}//set 操作 无序不重复集合
/*** 添加 set 元素
*
*@paramkey
*@paramvalues
*@return
*/
publicLong add(String key, String... values) {returnredisTemplate.opsForSet().add(key, values);
}/*** 获取两个集合的差集
*
*@paramkey
*@paramkey2
*@return
*/
public Setdifference(String key, String otherkey) {returnredisTemplate.opsForSet().difference(key, otherkey);
}/*** 获取 key 和 集合 collections 中的 key 集合的差集
*
*@paramkey
*@paramcollections
*@return
*/
public Set difference(String key, CollectionotherKeys) {returnredisTemplate.opsForSet().difference(key, otherKeys);
}/*** 将 key 与 otherkey 的差集 ,添加到新的 newKey 集合中
*
*@paramkey
*@paramotherkey
*@paramnewKey
*@return返回差集的数量*/
publicLong differenceAndStore(String key, String otherkey, String newKey) {returnredisTemplate.opsForSet().differenceAndStore(key, otherkey, newKey);
}/*** 将 key 和 集合 collections 中的 key 集合的差集 添加到 newkey 集合中
*
*@paramkey
*@paramotherKeys
*@paramnewKey
*@return返回差集的数量*/
public Long differenceAndStore(String key, CollectionotherKeys, String newKey) {returnredisTemplate.opsForSet().differenceAndStore(newKey, otherKeys, newKey);
}/*** 删除一个或多个集合中的指定值
*
*@paramkey
*@paramvalues
*@return成功删除数量*/
publicLong remove(String key, Object... values) {returnredisTemplate.opsForSet().remove(key, values);
}/*** 随机移除一个元素,并返回出来
*
*@paramkey
*@return
*/
publicObject randomSetPop(String key) {returnredisTemplate.opsForSet().pop(key);
}/*** 随机获取一个元素
*
*@paramkey
*@return
*/
publicObject randomSet(String key) {returnredisTemplate.opsForSet().randomMember(key);
}/*** 随机获取指定数量的元素,同一个元素可能会选中两次
*
*@paramkey
*@paramcount
*@return
*/
public List randomSet(String key, longcount) {returnredisTemplate.opsForSet().randomMembers(key, count);
}/*** 随机获取指定数量的元素,去重(同一个元素只能选择两一次)
*
*@paramkey
*@paramcount
*@return
*/
public Set randomSetDistinct(String key, longcount) {returnredisTemplate.opsForSet().distinctRandomMembers(key, count);
}/*** 将 key 中的 value 转入到 destKey 中
*
*@paramkey
*@paramvalue
*@paramdestKey
*@return返回成功与否*/
public booleanmoveSet(String key, Object value, String destKey) {returnredisTemplate.opsForSet().move(key, value, destKey);
}/*** 无序集合的大小
*
*@paramkey
*@return
*/
publicLong setSize(String key) {returnredisTemplate.opsForSet().size(key);
}/*** 判断 set 集合中 是否有 value
*
*@paramkey
*@paramvalue
*@return
*/
public booleanisMember(String key, Object value) {returnredisTemplate.opsForSet().isMember(key, value);
}/*** 返回 key 和 othere 的并集
*
*@paramkey
*@paramotherKey
*@return
*/
public SetunionSet(String key, String otherKey) {returnredisTemplate.opsForSet().union(key, otherKey);
}/*** 返回 key 和 otherKeys 的并集
*
*@paramkey
*@paramotherKey key 的集合
*@return
*/
public Set unionSet(String key, CollectionotherKeys) {returnredisTemplate.opsForSet().union(key, otherKeys);
}/*** 将 key 与 otherKey 的并集,保存到 destKey 中
*
*@paramkey
*@paramotherKey
*@paramdestKey
*@returndestKey 数量*/
publicLong unionAndStoreSet(String key, String otherKey, String destKey) {returnredisTemplate.opsForSet().unionAndStore(key, otherKey, destKey);
}/*** 将 key 与 otherKey 的并集,保存到 destKey 中
*
*@paramkey
*@paramotherKeys
*@paramdestKey
*@returndestKey 数量*/
public Long unionAndStoreSet(String key, CollectionotherKeys, String destKey) {returnredisTemplate.opsForSet().unionAndStore(key, otherKeys, destKey);
}/*** 返回集合中所有元素
*
*@paramkey
*@return
*/
public Setmembers(String key) {returnredisTemplate.opsForSet().members(key);
}//Zset 根据 socre 排序 不重复 每个元素附加一个 socre double类型的属性(double 可以重复)
/*** 添加 ZSet 元素
*
*@paramkey
*@paramvalue
*@paramscore*/
public boolean add(String key, Object value, doublescore) {returnredisTemplate.opsForZSet().add(key, value, score);
}/*** 批量添加 Zset
* Set> tuples = new HashSet<>();
* TypedTuple objectTypedTuple1 = new
* DefaultTypedTuple("zset-5",9.6);
* tuples.add(objectTypedTuple1);
*
*@paramkey
*@paramtuples
*@return
*/
public Long batchAddZset(String key, Set>tuples) {returnredisTemplate.opsForZSet().add(key, tuples);
}/*** Zset 删除一个或多个元素
*
*@paramkey
*@paramvalues
*@return
*/
publicLong removeZset(String key, String... values) {returnredisTemplate.opsForZSet().remove(key, values);
}/*** 对指定的 zset 的 value 值 , socre 属性做增减操作
*
*@paramkey
*@paramvalue
*@paramscore
*@return
*/
public Double incrementScore(String key, Object value, doublescore) {returnredisTemplate.opsForZSet().incrementScore(key, value, score);
}/*** 获取 key 中指定 value 的排名(从0开始,从小到大排序)
*
*@paramkey
*@paramvalue
*@return
*/
publicLong rank(String key, Object value) {returnredisTemplate.opsForZSet().rank(key, value);
}/*** 获取 key 中指定 value 的排名(从0开始,从大到小排序)
*
*@paramkey
*@paramvalue
*@return
*/
publicLong reverseRank(String key, Object value) {returnredisTemplate.opsForZSet().reverseRank(key, value);
}/*** 获取索引区间内的排序结果集合(从0开始,从小到大,带上分数)
*
*@paramkey
*@paramstart
*@paramend
*@return
*/
public Set> rangeWithScores(String key, long start, longend) {returnredisTemplate.opsForZSet().rangeWithScores(key, start, end);
}/*** 获取索引区间内的排序结果集合(从0开始,从小到大,只有列名)
*
*@paramkey
*@paramstart
*@paramend
*@return
*/
public Set range(String key, long start, longend) {returnredisTemplate.opsForZSet().range(key, start, end);
}/*** 获取分数范围内的 [min,max] 的排序结果集合 (从小到大,只有列名)
*
*@paramkey
*@parammin
*@parammax
*@return
*/
public Set rangeByScore(String key, double min, doublemax) {returnredisTemplate.opsForZSet().rangeByScore(key, min, max);
}/*** 获取分数范围内的 [min,max] 的排序结果集合 (从小到大,集合带分数)
*
*@paramkey
*@parammin
*@parammax
*@return
*/
public Set> rangeByScoreWithScores(String key, double min, doublemax) {returnredisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max);
}/*** 返回 分数范围内 指定 count 数量的元素集合, 并且从 offset 下标开始(从小到大,不带分数的集合)
*
*@paramkey
*@parammin
*@parammax
*@paramoffset 从指定下标开始
*@paramcount 输出指定元素数量
*@return
*/
public Set rangeByScore(String key, double min, double max, long offset, longcount) {returnredisTemplate.opsForZSet().rangeByScore(key, min, max, offset, count);
}/*** 返回 分数范围内 指定 count 数量的元素集合, 并且从 offset 下标开始(从小到大,带分数的集合)
*
*@paramkey
*@parammin
*@parammax
*@paramoffset 从指定下标开始
*@paramcount 输出指定元素数量
*@return
*/
public Set> rangeByScoreWithScores(String key, double min, double max, long offset, longcount) {returnredisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max, offset, count);
}/*** 获取索引区间内的排序结果集合(从0开始,从大到小,只有列名)
*
*@paramkey
*@paramstart
*@paramend
*@return
*/
public Set reverseRange(String key, long start, longend) {returnredisTemplate.opsForZSet().reverseRange(key, start, end);
}/*** 获取索引区间内的排序结果集合(从0开始,从大到小,带上分数)
*
*@paramkey
*@paramstart
*@paramend
*@return
*/
public Set> reverseRangeWithScores(String key, long start, longend) {returnredisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
}/*** 获取分数范围内的 [min,max] 的排序结果集合 (从大到小,集合不带分数)
*
*@paramkey
*@parammin
*@parammax
*@return
*/
public Set reverseRangeByScore(String key, double min, doublemax) {returnredisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
}/*** 获取分数范围内的 [min,max] 的排序结果集合 (从大到小,集合带分数)
*
*@paramkey
*@parammin
*@parammax
*@return
*/
public Set> reverseRangeByScoreWithScores(String key, double min, doublemax) {returnredisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max);
}/*** 返回 分数范围内 指定 count 数量的元素集合, 并且从 offset 下标开始(从大到小,不带分数的集合)
*
*@paramkey
*@parammin
*@parammax
*@paramoffset 从指定下标开始
*@paramcount 输出指定元素数量
*@return
*/
public Set reverseRangeByScore(String key, double min, double max, long offset, longcount) {returnredisTemplate.opsForZSet().reverseRangeByScore(key, min, max, offset, count);
}/*** 返回 分数范围内 指定 count 数量的元素集合, 并且从 offset 下标开始(从大到小,带分数的集合)
*
*@paramkey
*@parammin
*@parammax
*@paramoffset 从指定下标开始
*@paramcount 输出指定元素数量
*@return
*/
public Set> reverseRangeByScoreWithScores(String key, double min, double max, longoffset,longcount) {returnredisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max, offset, count);
}/*** 返回指定分数区间 [min,max] 的元素个数
*
*@paramkey
*@parammin
*@parammax
*@return
*/
public long countZSet(String key, double min, doublemax) {returnredisTemplate.opsForZSet().count(key, min, max);
}/*** 返回 zset 集合数量
*
*@paramkey
*@return
*/
public longsizeZset(String key) {returnredisTemplate.opsForZSet().size(key);
}/*** 获取指定成员的 score 值
*
*@paramkey
*@paramvalue
*@return
*/
publicDouble score(String key, Object value) {returnredisTemplate.opsForZSet().score(key, value);
}/*** 删除指定索引位置的成员,其中成员分数按( 从小到大 )
*
*@paramkey
*@paramstart
*@paramend
*@return
*/
public Long removeRange(String key, long start, longend) {returnredisTemplate.opsForZSet().removeRange(key, start, end);
}/*** 删除指定 分数范围 内的成员 [main,max],其中成员分数按( 从小到大 )
*
*@paramkey
*@parammin
*@parammax
*@return
*/
public Long removeRangeByScore(String key, double min, doublemax) {returnredisTemplate.opsForZSet().removeRangeByScore(key, min, max);
}/*** key 和 other 两个集合的并集,保存在 destKey 集合中, 列名相同的 score 相加
*
*@paramkey
*@paramotherKey
*@paramdestKey
*@return
*/
publicLong unionAndStoreZset(String key, String otherKey, String destKey) {returnredisTemplate.opsForZSet().unionAndStore(key, otherKey, destKey);
}/*** key 和 otherKeys 多个集合的并集,保存在 destKey 集合中, 列名相同的 score 相加
*
*@paramkey
*@paramotherKeys
*@paramdestKey
*@return
*/
public Long unionAndStoreZset(String key, CollectionotherKeys, String destKey) {returnredisTemplate.opsForZSet().unionAndStore(key, otherKeys, destKey);
}/*** key 和 otherKey 两个集合的交集,保存在 destKey 集合中
*
*@paramkey
*@paramotherKey
*@paramdestKey
*@return
*/
publicLong intersectAndStore(String key, String otherKey, String destKey) {returnredisTemplate.opsForZSet().intersectAndStore(key, otherKey, destKey);
}/*** key 和 otherKeys 多个集合的交集,保存在 destKey 集合中
*
*@paramkey
*@paramotherKeys
*@paramdestKey
*@return
*/
public Long intersectAndStore(String key, CollectionotherKeys, String destKey) {returnredisTemplate.opsForZSet().intersectAndStore(key, otherKeys, destKey);
}/*** 添加 Hash 键值对 Object
*
*@paramkey
*@paramhashKey
*@paramvalue*/
public voidhmSet(String key, String hashKey, Object value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}publicObject hmGet(String key, Object hashKey) {
HashOperations hash =redisTemplate.opsForHash();returnhash.get(key, hashKey);
}/*** 删除hash键
*
*@paramkey
*@paramhashKey*/
public voidhmDeleteKey(String key, Object hashKey) {
HashOperations hash =redisTemplate.opsForHash();
hash.delete(key, hashKey);
}publicT_user getSession(String userName) {
T_user session=(T_user) hmGet(Constants.FRAS_SESSION_KEY, userName);returnsession;
}public void setSession(String userName, T_user session, longdayTime) {
hmSet(Constants.FRAS_SESSION_KEY, userName, session);
expire(Constants.FRAS_SESSION_KEY+":" +userName, dayTime, TimeUnit.DAYS );
}public voidhmClear(String key, Object hashKey) {
HashOperations hash =redisTemplate.opsForHash();
hash.delete(key, hashKey);
}public voidclearSession(String username) {
hmClear(Constants.FRAS_SESSION_KEY, username);
}
}