redis使用keys去模糊查询,会遍历redis中的所有key吗?
1条回答 默认 最新
- 小P聊技术 2021-06-29 15:39关注
优先使用scan, 代替keys,scan每次遍历设置的值,对效率有较大的影响 项目中亲测: 当开发环境缓存有10几条的时候,设置每次查询的条数为10,耗时2000毫秒左右 设置每次查询的条数为100时,效率提升十倍 Jedis jedis=RedisUtils.getConn(); ScanParams scanParams = new ScanParams(); scanParams.match(“key*”); **// 这里设置的key对效率影响较大 **scanParams.count(100); Long startTime = System.currentTimeMillis(); List<String> retList = new ArrayList<String>(); final String scanRet = "0"; do { ScanResult<String> ret = jedis.scan(scanRet, scanParams); scanRet = ret.getStringCursor();// 返回用于下次遍历的游标,非零则还有数据没遍历 retList.addAll(ret.getResult());// 返回结果 } while (!scanRet.equals("0")); System.out.println("retList size:"+retList.size()); Long endTime = System.currentTimeMillis(); System.out.println("using time is:" + (endTime - startTime)+"ms");
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报