#
Junjie
3 天以前 8b60bf565eb475c13e39caa305c4415b34d8ec02
src/main/java/com/zy/common/utils/RedisUtil.java
@@ -1,11 +1,13 @@
package com.zy.common.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -23,6 +25,9 @@
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public RedisUtil(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
@@ -90,6 +95,31 @@
    //============================ String =============================
    public Set<String> searchKeys(String key) {
        Set<String> keys = redisTemplate.keys(key + "*");
        return keys;
    }
    public Set<String> scanKeys(String key, int limit) {
        return (Set<String>) redisTemplate.execute((org.springframework.data.redis.core.RedisCallback<Set<String>>) connection -> {
            org.springframework.data.redis.core.ScanOptions options = org.springframework.data.redis.core.ScanOptions.scanOptions().match(key + "*").count(limit).build();
            org.springframework.data.redis.core.Cursor<byte[]> cursor = connection.scan(options);
            java.util.LinkedHashSet<String> result = new java.util.LinkedHashSet<>();
            while (cursor.hasNext()) {
                result.add(new String(cursor.next()));
                if (result.size() >= limit) {
                    break;
                }
            }
            try { cursor.close(); } catch (Exception e) {}
            return result;
        });
    }
    public java.util.List<Object> multiGet(java.util.Collection<String> keys) {
        return redisTemplate.opsForValue().multiGet(keys);
    }
    /**
     * 普通缓存获取
     *
@@ -98,6 +128,21 @@
     */
    public Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }
    /**
     * 获取全部数据
     * @return
     */
    public HashMap<Object, Object> getRedis() {
        Set<String> keys = redisTemplate.keys("*");
        HashMap<Object, Object> map = new HashMap<>();
        for (String key : keys) {
            Object value = redisTemplate.opsForValue().get(key);
            map.put(key, value);
        }
        return map;//返回全部数据集合
    }
    /**
@@ -110,6 +155,36 @@
    public boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            redisTemplate.execute((RedisCallback<Void>) connection -> null);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 普通缓存放入
     *
     * @param key   键
     * @param value 值
     * @return true成功 false失败
     */
    public boolean setSync(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            redisTemplate.execute((RedisCallback<Void>) connection -> null);
            long start = System.currentTimeMillis();
            while (System.currentTimeMillis() - start < 10000) {//有效期10s
                Object o = redisTemplate.opsForValue().get(key);
                if (o == null) {
                    continue;
                }
                if (o.equals(value)) {
                    break;
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
@@ -132,6 +207,7 @@
            } else {
                set(key, value);
            }
            redisTemplate.execute((RedisCallback<Void>) connection -> null);
            return true;
        } catch (Exception e) {
            e.printStackTrace();