From fa222376beee43c206387f3f94f057d17af3733a Mon Sep 17 00:00:00 2001 From: Minsim <284943353@qq.com> Date: Thu, 12 Aug 2021 15:44:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9token=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E7=9A=84=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pig/auth/endpoint/PigTokenEndpoint.java | 48 ++++--------------- 1 file changed, 8 insertions(+), 40 deletions(-) diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/PigTokenEndpoint.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/PigTokenEndpoint.java index 74bef319..719efe29 100755 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/PigTokenEndpoint.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/PigTokenEndpoint.java @@ -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 tokenList(@RequestBody Map params) { // 根据分页参数获取对应数据 String key = String.format("%sauth_to_access:*", CacheConstants.PROJECT_OAUTH_ACCESS); - List 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 keys = redisTemplate.keys(key); + List 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 findKeysForPage(String patternKey, int pageNum, int pageSize) { - ScanOptions options = ScanOptions.scanOptions().count(1000L).match(patternKey).build(); - RedisSerializer redisSerializer = (RedisSerializer) redisTemplate.getKeySerializer(); - Cursor cursor = (Cursor) redisTemplate.executeWithStickyConnection( - redisConnection -> new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize)); - List 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; - } }