参考:
Redis工具类
package com.xd.common.server;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.schema.*;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Redis工具类
*/
@Slf4j
@Component
public class RedisServer {
/**
* 超時時間30M
*/
public static final long TIME_OUT_HALF_HOUR = 1800L;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
RedissonClient redissonClient;
public RedissonClient getRedissonClient() {
return redissonClient;
}
// =============================com.xd.common.common============================
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return 0
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间
* @param timeUnit 时间单位
* @return
*/
public boolean expire(String key, long time, TimeUnit timeUnit) {
try {
if (time > 0) {
redisTemplate.expire(key, time, timeUnit);
}
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public Long getExpire(String key) {
try {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 模糊搜索
*
* @param fuzzyKey 模糊搜索键
* @param count 搜索范围
* @return
* @Author sfh
* @Date 2019/12/5 16:17
*/
public Set<String> scanKey(String fuzzyKey, int count) {
RedisConnection connection = null;
try {
Set<String> keys = new HashSet<>();
RedisConnectionFactory connectionFactory = redisTemplate.getConnectionFactory();
connection = connectionFactory.getConnection();
Cursor<byte[]> scan = connection.scan(ScanOptions.scanOptions().count(count).match(fuzzyKey).build());
String key;
while (scan.hasNext()) {
byte[] next = scan.next();
key = new String(next, StandardCharsets.UTF_8);
keys.add(key);
}
return keys;
} catch (Exception e) {
log.error("redisServer", e);
} finally {
if (connection != null) {
connection.close();
}
}
return null;
}
/**
* 删除缓存
*
* @param key 可以传一个值
* @return
*/
public Boolean del(String key) {
try {
return redisTemplate.delete(key);
} catch (Exception e) {
log.error("redisServer", e);
}
return false;
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
* @return
*/
public Long del(String... key) {
try {
if (key != null && key.length > 0) {
return redisTemplate.delete(CollectionUtils.arrayToList(key));
}
return null;
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 批量删除key
*
* @Author sfh
* @Date 2019/9/6 16:20
*/
public Long del(Collection<String> key) {
try {
return redisTemplate.delete(key);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
// ============================String=============================
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public String get(String key) {
try {
return key == null ? null : stringRedisTemplate.opsForValue().get(key);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 获取对象类型数据
*
* @Author sfh
* @Date 2019/8/26 17:01
*/
public <T> T get(String key, Class<T> clazz) {
try {
String str = stringRedisTemplate.opsForValue().get(key);
T t = stringToBean(str, clazz);
return t;
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, String value) {
try {
stringRedisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 普通缓存放入并设置时间时间单位S
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, String value, long time) {
try {
if (time > 0) {
stringRedisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
public boolean set(String key, String value, long time, TimeUnit timeUnit) {
try {
if (time > 0) {
stringRedisTemplate.opsForValue().set(key, value, time, timeUnit);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 普通缓存放入并设置时间时间单位S
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
public boolean set(String key, Object value, long time, TimeUnit timeUnit) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, timeUnit);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 递增
*
* @param key 键
* @return
*/
public Long incr(String key) {
try {
return stringRedisTemplate.opsForValue().increment(key);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 递增
*
* @param key 键
* @param delta 要增加几(大于0)
* @return
*/
public Long incr(String key, long delta) {
try {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return stringRedisTemplate.opsForValue().increment(key, delta);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return
*/
public Long decr(String key, long delta) {
try {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return stringRedisTemplate.opsForValue().decrement(key, delta);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 原子操作直接修改数值
*
* @param key redis key
* @param delta -2^64-1 =>+2^64
* @return
* @Author sfh
* @Date 2020/8/26 16:39
* @version:
*/
public Long incrUpdate(String key, long delta) {
try {
return stringRedisTemplate.opsForValue().increment(key, delta);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
// ================================Map=================================
/**
* HashGet
*
* @param key 键 不能为null
* @param item 项 不能为null
* 0
* @return 值
*/
public Object hget(String key, String item) {
try {
return redisTemplate.opsForHash().get(key, item);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
*/
public Map<Object, Object> hmget(String key) {
try {
return redisTemplate.opsForHash().entries(key);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* HashSet
*
* @param key 键
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* HashSet 并设置时间
*
* @param key 键
* @param map 对应多个键值
* @param time 时间(秒)
* @return true成功 false失败
*/
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 删除hash表中的值
*
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
* @return
*/
public Long hdel(String key, Object... item) {
try {
return redisTemplate.opsForHash().delete(key, item);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
try {
return redisTemplate.opsForHash().hasKey(key, item);
} catch (Exception e) {
log.error("redisServer", e);
}
return false;
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key 键
* @param item 项
* @param by 要增加几(大于0)
* @return
*/
public Double hincr(String key, String item, double by) {
try {
return redisTemplate.opsForHash().increment(key, item, by);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* hash递减
*
* @param key 键
* @param item 项
* @param by 要减少记(小于0)
* @return
*/
public Double hdecr(String key, String item, double by) {
try {
return redisTemplate.opsForHash().increment(key, item, -by);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
// ============================set=============================
/**
* 根据key获取Set中的所有值
*
* @param key 键
* @return
*/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 根据value从一个set中查询,是否存在
*
* @param key 键
* @param value 值
* @return true 存在 false不存在
*/
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 将数据放入set缓存
*
* @param key 键
* @param values 值 可以是多个
* @return 成功个数
*/
public Long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 将set数据放入缓存
*
* @param key 键
* @param time 时间(秒)
* @param values 值 可以是多个
* @return 成功个数
*/
public Long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) {
expire(key, time);
}
return count;
} catch (Exception e) {
log.error("redisServer", e);
}
return 0L;
}
/**
* 获取set缓存的长度
*
* @param key 键
* @return
*/
public Long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
log.error("redisServer", e);
}
return 0L;
}
/**
* 移除值为value的
*
* @param key 键
* @param values 值 可以是多个
* @return 移除的个数
*/
public Long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
log.error("redisServer", e);
}
return 0L;
}
// ===============================list=================================
/**
* 获取list缓存的内容
*
* @param key 键
* @param start 开始
* @param end 结束 到 - 代表所有值
* @return
*/
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 获取list缓存的长度
*
* @param key 键
* @return
*/
public Long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
log.error("redisServer", e);
}
return 0L;
}
/**
* 通过索引 获取list中的值
*
* @param key 键
* @param index 索引 index>=0时, 表头, 第二个元素,依次类推;index<0时,- ,表尾,- 倒数第二个元素,依次类推
* @return 0
*/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
log.error("redisServer", e);
return null;
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @return
*/
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 将list放入缓存
*
* @param key 键
* 0
* @param value 值
* @param time 时间(秒)
* @return
*/
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 将list放入缓存
*
* @param value 值
* @return
*/
public boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return
*/
public boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 根据索引修改list中的某条数据
*
* @param key 键
* @param index 索引
* @param value 值
* @return
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
log.error("redisServer", e);
return false;
}
}
/**
* 移除N个值为value
*
* @param key 键
* @param count 移除多少个
* @param value 值
* @return 移除的个数
*/
public Long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
log.error("redisServer", e);
return 0L;
}
}
@SuppressWarnings("unchecked")
private <T> T stringToBean(String str, Class<T> clazz) {
if (str == null || str.length() <= 0 || clazz == null) {
return null;
}
if (clazz == int.class || clazz == Integer.class) {
return (T) Integer.valueOf(str);
} else if (clazz == String.class) {
return (T) str;
} else if (clazz == long.class || clazz == Long.class) {
return (T) Long.valueOf(str);
} else {
return JSON.toJavaObject(JSON.parseObject(str), clazz);
}
}
/**
* 模糊删除缓存
*
* @param key 前缀
* @return
*/
public Long fuzzyDel(String key) {
try {
Set<String> keys = redisTemplate.keys(key + "*");
return redisTemplate.delete(keys);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 模糊匹配key
*
* @param key 前缀
* @return
*/
public Set<String> fuzzyKey(String key) {
try {
Set<String> keys = redisTemplate.keys(key + "*");
return keys;
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 有序集合添加
*
* @param key
* @param value
* @param score
*/
public boolean zSet(String key, Object value, double score) {
try {
return redisTemplate.opsForZSet().add(key, value, score);
} catch (Exception e) {
log.error("redisServer", e);
}
return false;
}
/**
* 有序集合添加分数
*
* @param key
* @param value
* @param score
*/
public Double incrementScore(String key, Object value, double score) {
try {
return redisTemplate.opsForZSet().incrementScore(key, value, score);
} catch (Exception e) {
log.error("redisServer", e);
}
return null;
}
/**
* 获取有序集 key 中成员 member 的排名 。
* 其中有序集成员按 score 值递减 (从大到小) 排序。
*
* @param key
* @param start
* @param end
* @return
*/
public Set<ZSetOperations.TypedTuple<Object>> getZSetRank(String key, long start, long end) {
return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
}
/**
* 有序集合获取排名 倒序
*
* @param key 集合名称
* @param value 值
*/
public Long reverseRank(String key, Object value) {
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.reverseRank(key, value);
}
/**
* 得分范围内有序集合获取排名
*
* @param key
*/
public Long ZSetRemove(String key, String value) {
return redisTemplate.opsForZSet().remove(key, value);
}
/**
* 存放单个hash缓存
*
* @param key 键
* @param hKey 键
* @param value 值
* @return
*/
public boolean hPut(String key, String hKey, Object value) {
try {
redisTemplate.opsForHash().put(key, hKey, value);
log.debug("hput {} = {}", key + hKey, value);
return true;
} catch (Exception e) {
log.warn("hput {} = {}", key + hKey, value, e);
}
return false;
}
/**
* 分页存取数据
*
* @param key hash存取的key
* @param hkey hash存取的hkey
* @param score 指定字段排序
* @param value
* @return
*/
public boolean setPage(String key, String hkey, double score, String value) {
boolean result = false;
try {
result = redisTemplate.opsForZSet().add(key + ":page", hkey, score);
//result = hput(key, hkey, value);
} catch (Exception e) {
e.printStackTrace();
}
//设置辅助分页的过期时间
redisTemplate.expire(key + ":page", 1800000, TimeUnit.MILLISECONDS);
//redisTemplate.expire(key,60000 , TimeUnit.MILLISECONDS);
return result;
}
/**
* @Description:生成辅助分页,自动加上:page
* @Author: zcm
* @Version:v.2.7.3
* @Date:2021/3/27 11:18
*/
public void setAuxiliaryPage(Cursor<byte[]> scan, String tableName) {
//setPage初始化值,zset的sort值
int i = 1;
//遍历模糊查询结果,将模糊查询的结果生成分页数据,用zset存储模糊查询的数据排序
//存储名称是查询name+:page,值是hash类型的key
while (scan.hasNext()) {
byte[] next = scan.next();
String key = new String(next, StandardCharsets.UTF_8);
//存储对应的辅助分页数据
if (!tableName.equals(key)) {
setPage(tableName, key, i, null);
}
i++;
}
}
public List<Object> getPage(String key, Long offset, Long count) {
List<Object> result = null;
try {
result = redisTemplate.opsForList().range(key, offset, count);//1 100000代表score的排序氛围值,即从1-100000的范围
log.debug("getPage {}", key);
} catch (Exception e) {
log.warn("getPage {}", key, e);
}
return result;
}
public Cursor<byte[]> getPage3(String key, Long offset, Long count) {
// Set<Object> keys = new HashSet<>();
RedisConnectionFactory connectionFactory = redisTemplate.getConnectionFactory();
RedisConnection connection = null;
connection = connectionFactory.getConnection();
Cursor<byte[]> scan = connection.scan(ScanOptions.scanOptions().count(count).match(key).build());
// while (scan.hasNext()) {
// byte[] next = scan.next();
// String key1 = new String(next, StandardCharsets.UTF_8);
// keys.add(key1);
// }
return scan;
}
/**
* 计算key值对应的数量
*
* @param key
* @return
*/
public Integer getSize(String key) {
Integer num = 0;
try {
Long size = redisTemplate.opsForZSet().zCard(key + ":page");
log.debug("getSize {}", key);
return size.intValue();
} catch (Exception e) {
log.warn("getSize {}", key, e);
}
return num;
}
}
<properties>
<spring-data-redis.version>2.1.6.RELEASE</spring-data-redis.version>
<redisson.version>3.10.5</redisson.version>
</properties>
<!--redis-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring-data-redis.version}</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>${redisson.version}</version>
</dependency>
<!-- redis-->
实现代码
@Override
public ReturnPageList redisList(ManageVo vo) {
// 登入控制缓存
String kickOutRedisKey = UserRedisKeyUtil.getKickOutRedisKey("*");
// String userKey = kickOutRedisKey + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ2aXAiOjAsImV4cCI6MTYxODczNDU4MiwidXNlcklkIjoiMTM4Mjg2MjgyNTA5NDY3NjQ4IiwiaWF0IjoxNjE2MTQyNTgyLCJqd3RJZCI6Im9KcXYyNnF0VU9YQ0VNMzl6Qk5HeGlQRUxOSU0ifQ.12jhaXMGxSbI_mx8IrZ-vgIbwdaUTKneRa4XXIat6fg";
// redisServer.setPage(kickOutRedisKey,"tool:user:kickout:213123123:123eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ2aXAiOjEsImV4cCI6MTYxOTIyODIwMywidXNlcklkIjoiMTM4NjcxOTg1MTQ1NDEzNjMyIiwiaWF0IjoxNjE2NjM2MjAzLCJqd3RJZCI6Im9KcXYyNm91WjRyWno5cEl1ZUFvNjMyd0tnbjAifQ.UeQK8QgagoW4fBtCsZrQBjviR5Cm4Om-NXW058HLC08",1,"123");
// redisServer.setPage(kickOutRedisKey,"tool:user:kickout:213123123:1323eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ2aXAiOjEsImV4cCI6MTYxOTIyODIwMywidXNlcklkIjoiMTM4NjcxOTg1MTQ1NDEzNjMyIiwiaWF0IjoxNjE2NjM2MjAzLCJqd3RJZCI6Im9KcXYyNm91WjRyWno5cEl1ZUFvNjMyd0tnbjAifQ.UeQK8QgagoW4fBtCsZrQBjviR5Cm4Om-NXW058HLC08",1,"123");
// redisServer.setPage(kickOutRedisKey,"tool:user:kickout:213123123:142eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ2aXAiOjEsImV4cCI6MTYxOTIyODIwMywidXNlcklkIjoiMTM4NjcxOTg1MTQ1NDEzNjMyIiwiaWF0IjoxNjE2NjM2MjAzLCJqd3RJZCI6Im9KcXYyNm91WjRyWno5cEl1ZUFvNjMyd0tnbjAifQ.UeQK8QgagoW4fBtCsZrQBjviR5Cm4Om-NXW058HLC08",1,"123");
String key = kickOutRedisKey+":page";
List<Object> list = new ArrayList();
//是否存在对应的辅助分页辅助分页key-value
Boolean ifExist = redisTemplate.hasKey(key);
//如果不存在,生成辅助分页,同时查询
if (!ifExist) {
//如果传入参数为空则查询所有
//模糊查询返回结果
//Cusor中存储的是查询key对应的Map
RedisConnectionFactory connectionFactory = redisTemplate.getConnectionFactory();
RedisConnection connection = connectionFactory.getConnection();
Cursor<byte[]> scan = connection.scan(ScanOptions.scanOptions().count(vo.getPageSize()).match(kickOutRedisKey).build());
//生成分页key-value
redisServer.setAuxiliaryPage(scan, kickOutRedisKey);
}
Set<Object> result = null;
try {
result = redisTemplate.opsForZSet().rangeByScore(key, 1, 100000, (vo.getPageIndex() - 1) * vo.getPageSize(), vo.getPageSize());//1 100000代表score的排序氛围值,即从1-100000的范围
log.debug("getPage {}", key);
} catch (Exception e) {
log.warn("getPage {}", key, e);
}
//得到分页数据总条数
Integer totalCount = redisServer.getSize(key);
vo.setPageSize(result.size());
for (Object keyPage : result) {
// //根据zset的key查询hash对应的数据
// String JSONObject = (String) redisTemplate.boundHashOps(key).get(keyPage);
// //转为对应的实体类
T Object = (T) JSON.parseObject(JSONObject, (Type) t);
list.add(keyPage);
}
ReturnPageList pageList = new ReturnPageList(vo.getPageIndex(), vo.getPageSize(), totalCount);
pageList.setList(list);
return pageList;
}