Redis工具类 SCAN扫描key

RedisUtil.java

package com.xxxx.auth.utils;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;

public abstract class RedisUtil {

    /**
     * scan 实现
     *
     * @param redisTemplate redisTemplate
     * @param pattern       表达式,如:abc*,找出所有以abc开始的键
     */
    public static Set<String> scan(RedisTemplate<String, String> redisTemplate, String pattern) {
        return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
            Set<String> keysTmp = new HashSet<>();
            try (Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder()
                    .match(pattern)
                    .count(10000).build())) {

                while (cursor.hasNext()) {
                    keysTmp.add(new String(cursor.next(), "Utf-8"));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return keysTmp;
        });
    }

    public static Set<String> scanX(RedisTemplate<Serializable, Object> redisTemplate, String pattern) {
        return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
            Set<String> keysTmp = new HashSet<>();
            try (Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder()
                    .match(pattern)
                    .count(10000).build())) {

                while (cursor.hasNext()) {
                    keysTmp.add(new String(cursor.next(), "Utf-8"));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return keysTmp;
        });
    }

    /**
     * 从redis中获取key对应的过期时间;
     * 如果该值有过期时间,就返回相应的过期时间;
     * 如果该值没有设置过期时间,就返回-1;
     * 如果没有该值,就返回-2;
     * @param key
     * @return
     */
    public static long expire(RedisTemplate<String, String> redisTemplate,String key) {
        return redisTemplate.opsForValue().getOperations().getExpire(key);
    }

    /**
     * 计算相差的秒数
     * @param smdate
     * @param bdate
     * @return
     * @throws ParseException
     */
    public static int secondsBetween(String smdate,String bdate) throws ParseException{  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(sdf.parse(smdate));    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(sdf.parse(bdate));    
        long time2 = cal.getTimeInMillis();         
        long diff=(time2-time1)/1000;

       return Integer.parseInt(String.valueOf(diff));     
    } 


}

使用

Set<String> kaptchaHistory = RedisUtil.scanX(redisConfigX.getRedisTemplateByDb(2),  "hist-*");
评论