RedisTemplate uses scan to scan data

Find the corresponding key set according to the specified key prefix 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;

@RestController
public class HomeController {


    @Autowired
    private RedisConnectionFactory connectionFactory;


    String prefix = "ad\\.jpush\\.prfix\\.";

    @GetMapping("/")
    public String index() {
        RedisConnection connection = null;
        try {
            connection = connectionFactory.getConnection();
            ScanOptions scanOptions = ScanOptions.scanOptions().count(10L).match(prefix + "*").build();
            Cursor<byte[]> cursors = connection.scan(scanOptions);

            cursors.forEachRemaining(new Consumer<byte[]>() {
                @Override
                public void accept(byte[] bytes) {
                    String key = new String(bytes, StandardCharsets.UTF_8);
                    System.out.println(key);

                }
            });
        } finally {
            RedisConnectionUtils.releaseConnection(connection, connectionFactory);
        }

        return "index";
    }


}

RedisConnection encapsulates the operation of redis, you can click in to see

 

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324108538&siteId=291194637