puzhibing
2024-02-05 640ff18d2d7f4be02ddb7f8f75e899f05545eb98
cloud-server-other/src/main/java/com/dsh/other/util/RedisUtil.java
New file
@@ -0,0 +1,185 @@
package com.dsh.other.util;
import com.alibaba.csp.sentinel.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * Redis工具类
 */
@Component
public class RedisUtil {
    @Autowired
    private JedisPool jedisPool;
    private static final int TIMEOUT = 10 * 1000; // 10秒超时时间
    private static final int SLEEP_TIME = 1000; // 1秒休眠时间
    /**
     * 向redis中存储字符串没有过期时间
     * Storing strings in Redis without an expiration time.
     *
     * @param key
     * @param value
     */
    public void setStrValue(String key, String value) {
        if (StringUtil.isNotEmpty(key)) {
            Jedis resource = jedisPool.getResource();
            String set = resource.set(key, value);
            closeJedis(resource);
        }
    }
    /**
     * 以分钟为单位设置存储值(设置过期时间)
     * Set storage value in minutes (set expiration time) as units.
     *
     * @param key
     * @param value
     * @param time  秒
     */
    public void setStrValue(String key, String value, int time) {
        if (StringUtil.isNotEmpty(key)) {
            Jedis resource = jedisPool.getResource();
            String setex = resource.setex(key, time, value);
            closeJedis(resource);
        }
    }
    /**
     * 从redis中获取值
     *
     * @param key
     * @return
     */
    public String getValue(String key) {
        if (StringUtil.isNotEmpty(key)) {
            Jedis resource = jedisPool.getResource();
            String data = resource.get(key);
            closeJedis(resource);
            return data;
        }
        return null;
    }
    /**
     * 批量获取
     *
     * @param kes
     * @return
     */
    public List<Object> getValues(List<String> kes) {
        if (null != kes) {
            Jedis resource = jedisPool.getResource();
            Pipeline pipelined = resource.pipelined();
            for (String key : kes) {
                pipelined.get(key);
            }
            List<Object> list = pipelined.syncAndReturnAll();
            closeJedis(resource);
            pipelined.clear();
            try {
                pipelined.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            List<Object> data = new ArrayList<>();
            for (Object o : list) {
                if (null != o) {
                    data.add(o);
                }
            }
            return data;
        }
        return null;
    }
    /**
     * 删除key
     *
     * @param key
     */
    public void remove(String key) {
        if (StringUtil.isNotEmpty(key)) {
            Jedis resource = jedisPool.getResource();
            Long del = resource.del(key);
            closeJedis(resource);
        }
    }
    /**
     * 删除资源
     *
     * @param jedis
     */
    public void closeJedis(Jedis jedis) {
        if (null != jedis) {
            jedis.close();
        }
    }
    public boolean acquireLock(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            long start = System.currentTimeMillis();
            String[] split = key.split(";");
            for (String s : split) {
                while (true) {
                    // 尝试获取锁
                    String result = jedis.set(s, value, "NX", "PX", TIMEOUT);
                    if ("OK".equals(result)) {
                        return true;
                    }
                    // 超时则返回失败
                    if (System.currentTimeMillis() - start > TIMEOUT) {
                        return false;
                    }
                    // 休眠一段时间后重试
                    Thread.sleep(SLEEP_TIME);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return false;
    }
    public void releaseLock(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}