/** * 封装redis模糊查询 * @param keyPrefix * @return */ public Set<String> keys(String keyPrefix) { // String realKey = "*" + keyPrefix + "*"; try { return redisTemplate.execute((RedisCallback<Set<String>>) connection -> { Set<String> binaryKeys = new HashSet<>(); Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder().match(keyPrefix).count(Integer.MAX_VALUE).build()); while (cursor.hasNext()) { binaryKeys.add(new String(cursor.next())); } return binaryKeys; }); } catch (Throwable e) { LOG.error("模糊搜索redis键值异常{}",e.getLocalizedMessage()); } return null; }
RedisTemplate + redis 使用scan代替keys
最新推荐文章于 2024-04-24 14:50:57 发布