StringRedisTemplate使用scan

 public Set scan(String key) {
	        return redisTemplate.execute((RedisCallback>) connection -> {
	            Set keys = Sets.newHashSet();

	            JedisCommands commands = (JedisCommands) connection.getNativeConnection();
	            MultiKeyCommands multiKeyCommands = (MultiKeyCommands) commands;

	            ScanParams scanParams = new ScanParams();
	            scanParams.match("*" + key + "*");
	            scanParams.count(1000); // 这个不是返回结果的数量,应该是每次scan的数量
	            ScanResult scan = multiKeyCommands.scan("0", scanParams);
	            while (null != scan.getCursor()) {
	            	LogManager.info("scan="+scan.getCursor()+" Result="+JsonTool.encode(scan.getResult()));	               
	            	keys.addAll(scan.getResult()); // 这一次scan match到的结果
	                if (!scan.getCursor().equals("0")) { // 不断拿着新的cursor scan,最终会拿到所有匹配的值
	                    scan = multiKeyCommands.scan(scan.getCursor(), scanParams);
	                    continue;
	                } else {
	                    break;
	                }
	                
	            }

	            return keys;
	        });
	    }

你可能感兴趣的:(java,springboot,redis,redis,java)