mirror of https://gitee.com/maxjhandsome/pig
修改token列表的获取方式
This commit is contained in:
parent
f82cbd4c1d
commit
fa222376be
|
@ -52,6 +52,8 @@ import javax.servlet.http.HttpSession;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
|
@ -160,49 +162,15 @@ public class PigTokenEndpoint {
|
|||
public R<Page> tokenList(@RequestBody Map<String, Object> params) {
|
||||
// 根据分页参数获取对应数据
|
||||
String key = String.format("%sauth_to_access:*", CacheConstants.PROJECT_OAUTH_ACCESS);
|
||||
List<String> pages = findKeysForPage(key, MapUtil.getInt(params, CommonConstants.CURRENT),
|
||||
MapUtil.getInt(params, CommonConstants.SIZE));
|
||||
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
|
||||
Page result = new Page(MapUtil.getInt(params, CommonConstants.CURRENT),
|
||||
MapUtil.getInt(params, CommonConstants.SIZE));
|
||||
int current = MapUtil.getInt(params, CommonConstants.CURRENT);
|
||||
int size = MapUtil.getInt(params, CommonConstants.SIZE);
|
||||
Set<String> keys = redisTemplate.keys(key);
|
||||
List<String> pages = keys.stream().skip((current - 1) * size).limit(size).collect(Collectors.toList());
|
||||
Page result = new Page(current, size);
|
||||
result.setRecords(redisTemplate.opsForValue().multiGet(pages));
|
||||
result.setTotal(redisTemplate.keys(key).size());
|
||||
result.setTotal(keys.size());
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue