#
Junjie
2 天以前 9af70943c966b0c74dcd735217ff18c9d7564221
src/main/java/com/zy/common/utils/RedisUtil.java
@@ -1,6 +1,7 @@
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;
@@ -24,6 +25,9 @@
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public RedisUtil(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
@@ -91,6 +95,11 @@
    //============================ String =============================
    public Set<String> searchKeys(String key) {
        Set<String> keys = redisTemplate.keys(key + "*");
        return keys;
    }
    /**
     * 普通缓存获取
     *
@@ -126,6 +135,18 @@
    public boolean set(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();
@@ -148,6 +169,7 @@
            } else {
                set(key, value);
            }
            redisTemplate.execute((RedisCallback<Void>) connection -> null);
            return true;
        } catch (Exception e) {
            e.printStackTrace();