Pu Zhibing
2025-08-11 b6da936b0c7379c3e1ef07409f81702074b45f47
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
package com.sinata.push.util;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
 
 
/**
 * Redis工具类
 */
@Component
public class RedisUtil {
 
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
 
 
    /**
     * 向redis中存储字符串没有过期时间
     * @param key
     * @param value
     */
    public void setStrValue(String key, String value){
        redisTemplate.opsForValue().set(key, value);
    }
 
 
    /**
     * 以分钟为单位设置存储值(设置过期时间)
     * @param key
     * @param value
     * @param time 秒
     */
    public void setStrValue(String key, String value, int time){
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }
 
 
    /**
     * 从redis中获取值
     * @param key
     * @return
     */
    public String getValue(String key){
        return (String) redisTemplate.opsForValue().get(key);
    }
 
 
}