提交 fa222376 编写于 作者: Dichotomy_'s avatar Dichotomy_

修改token列表的获取方式

上级 f82cbd4c
...@@ -52,6 +52,8 @@ import javax.servlet.http.HttpSession; ...@@ -52,6 +52,8 @@ import javax.servlet.http.HttpSession;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/** /**
* @author lengleng * @author lengleng
...@@ -160,49 +162,15 @@ public class PigTokenEndpoint { ...@@ -160,49 +162,15 @@ public class PigTokenEndpoint {
public R<Page> tokenList(@RequestBody Map<String, Object> params) { public R<Page> tokenList(@RequestBody Map<String, Object> params) {
// 根据分页参数获取对应数据 // 根据分页参数获取对应数据
String key = String.format("%sauth_to_access:*", CacheConstants.PROJECT_OAUTH_ACCESS); String key = String.format("%sauth_to_access:*", CacheConstants.PROJECT_OAUTH_ACCESS);
List<String> pages = findKeysForPage(key, MapUtil.getInt(params, CommonConstants.CURRENT), int current = MapUtil.getInt(params, CommonConstants.CURRENT);
MapUtil.getInt(params, CommonConstants.SIZE)); int size = MapUtil.getInt(params, CommonConstants.SIZE);
Set<String> keys = redisTemplate.keys(key);
redisTemplate.setKeySerializer(new StringRedisSerializer()); List<String> pages = keys.stream().skip((current - 1) * size).limit(size).collect(Collectors.toList());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); Page result = new Page(current, size);
Page result = new Page(MapUtil.getInt(params, CommonConstants.CURRENT),
MapUtil.getInt(params, CommonConstants.SIZE));
result.setRecords(redisTemplate.opsForValue().multiGet(pages)); result.setRecords(redisTemplate.opsForValue().multiGet(pages));
result.setTotal(redisTemplate.keys(key).size()); result.setTotal(keys.size());
return R.ok(result); return R.ok(result);
} }
private List<String> findKeysForPage(String patternKey, int pageNum, int pageSize) {
ScanOptions options = ScanOptions.scanOptions().count(1000L).match(patternKey).build();
RedisSerializer<String> redisSerializer = (RedisSerializer<String>) redisTemplate.getKeySerializer();
Cursor cursor = (Cursor) redisTemplate.executeWithStickyConnection(
redisConnection -> new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize));
List<String> result = new ArrayList<>();
int tmpIndex = 0;
int startIndex = (pageNum - 1) * pageSize;
int end = pageNum * pageSize;
assert cursor != null;
while (cursor.hasNext()) {
if (tmpIndex >= startIndex && tmpIndex < end) {
result.add(cursor.next().toString());
tmpIndex++;
continue;
}
if (tmpIndex >= end) {
break;
}
tmpIndex++;
cursor.next();
}
try {
cursor.close();
}
catch (Exception e) {
log.error("关闭cursor 失败");
}
return result;
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册