Pu Zhibing
2024-12-24 905eb707fff6fc6702c1c9e8333520012dd89414
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.sinata.zuul.util;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
 
import java.util.List;
import java.util.concurrent.TimeUnit;
 
 
/**
 * Redis工具类
 */
@Component
public class RedisUtil {
 
    @Autowired
    private RedisTemplate redisTemplate;
    
 
 
    /**
     * 向redis中存储字符串没有过期时间
     * @param key
     * @param value
     */
    public void setStrValue(String key, String value){
        if(StringUtil.isNotEmpty(key)){
            redisTemplate.opsForValue().set(key, value);
        }
    }
 
 
    /**
     * 以分钟为单位设置存储值(设置过期时间)
     * @param key
     * @param value
     * @param time 秒
     */
    public void setStrValue(String key, String value, int time){
        if(StringUtil.isNotEmpty(key)){
            redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
        }
    }
 
 
    /**
     * 从redis中获取值
     * @param key
     * @return
     */
    public String getValue(String key){
        if(StringUtil.isNotEmpty(key)){
            Object o = redisTemplate.opsForValue().get(key);
            return null != o ? o.toString() : null;
        }
        return null;
    }
 
 
   
    
    /**
     * 添加数据到set集群
     * @param key
     * @param value
     */
    public void addListRight(String key, String value){
        redisTemplate.opsForList().rightPush(key, value);
    }
    
    /**
     * 添加数据到set集群
     * @param key
     * @param value
     */
    public void addListLeft(String key, String value){
        redisTemplate.opsForList().leftPush(key, value);
    }
    
    
    /**
     * 获取list中第一个数据
     * @param key
     * @return
     */
    public String getListFirstValue(String key){
        Object o = redisTemplate.opsForList().leftPop(key);
        return null != o ? o.toString() : null;
    }
    
    
 
    /**
     * 删除key
     * @param key
     */
    public void remove(String key){
        redisTemplate.delete(key);
    }
 
 
 
}