admin
2021-08-27 8fee151ffae0c3818694b7318583814bf92663e2
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package com.yeshi.buwan.util;
 
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Component;
 
import com.google.gson.Gson;
import com.yeshi.buwan.vo.video.VideoListResultVO;
 
import org.yeshi.utils.SerializeUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;
 
//抢红包采用的redis
@Component
public class RedisManager {
 
    @Resource
    private JedisPool jedisPool;
 
    public Jedis getJedis() {
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }
 
    /**
     * 缓存字符串
     *
     * @param key
     * @param value
     */
    private void setString(String key, String value) {
        Jedis jedis = getJedis();
        SetParams params = new SetParams().nx().ex(60);
        jedis.set(key, value, params);
        try {
            jedis.set(key, value);
        } finally {
            jedis.close();
        }
 
    }
 
    /**
     * 删除某个键值
     *
     * @param key
     * @param value
     */
    private void removeKey(String key) {
        Jedis jedis = getJedis();
        try {
            jedis.del(key);
        } finally {
            jedis.close();
        }
 
    }
 
    /**
     * 缓存字符串
     *
     * @param key
     * @param value
     * @param seconds -缓存时间(s)
     */
    private void setString(String key, String value, int seconds) {
        Jedis jedis = getJedis();
        try {
            jedis.setex(key, seconds, value);
        } finally {
            jedis.close();
        }
    }
 
    private String getString(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.get(key);
        } finally {
            jedis.close();
        }
    }
 
    public void increase(String key) {
        Jedis jedis = getJedis();
        try {
            jedis.incr(key);
        } finally {
            jedis.close();
        }
    }
 
 
    public void increase(String key, int s) {
        Jedis jedis = getJedis();
        try {
            jedis.incr(key);
            jedis.expire(key, s);
        } finally {
            jedis.close();
        }
    }
 
    public void expire(String key, int seconds) {
        Jedis jedis = getJedis();
        try {
            jedis.expire(key, seconds);
        } finally {
            jedis.close();
        }
    }
 
    public void cacheCommonString(String key, String value, int seconds) {
        setString(key, value, seconds);
    }
 
    public void cacheCommonString(String key, String value) {
        setString(key, value);
    }
 
    public String getCommonString(String key) {
        return getString(key);
    }
 
    public void removeCommonString(String key) {
        removeKey(key);
    }
 
    public void remove(String key) {
        removeKey(key);
    }
 
    /**
     * 保存视频数据
     *
     * @param key
     * @param videoData
     */
    public void saveVideoList(String key, VideoListResultVO videoData) {
        Gson gson = new Gson();
        cacheCommonString(key, gson.toJson(videoData), 60 * 60 * 8);
    }
 
    /**
     * 获取视频列表
     *
     * @param key
     * @return
     */
    public VideoListResultVO getVideoList(String key) {
        String st = getCommonString(key);
        if (!StringUtil.isNullOrEmpty(st)) {
            return new Gson().fromJson(st, VideoListResultVO.class);
        }
        return null;
    }
 
    /**
     * 保存列表
     *
     * @param clazzList
     * @param key
     * @param seconds
     */
    public <T> T saveObjList(List<T> clazzList, String key, Integer seconds) {
        if (clazzList == null)
            return null;
        String value = new Gson().toJson(clazzList);
        if (seconds != null)
            cacheCommonString(key, value, seconds);
        else
            cacheCommonString(key, value);
        return null;
    }
 
    /**
     * 获取对象
     *
     * @param clazz
     * @param key
     * @return
     */
    public Class<?> getObj(Class<?> clazz, String key) {
        String value = getCommonString(key);
        return (Class<?>) new Gson().fromJson(value, clazz);
    }
 
    /**
     * 获取列表
     *
     * @param clazz
     * @param key
     * @return
     */
    public <T> List<T> getObjList(Class<T> clazz, String key) {
        String value = getCommonString(key);
        return JsonUtil.jsonToList(value, clazz);
    }
 
    public boolean isSmsFrequencyLimit(String phone, Integer type) {
        if (type == null) {
            type = 0;
        }
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + type);
        String value = getCommonString(key);
        if (StringUtil.isNullOrEmpty(value))
            return false;
        else
            return true;
    }
 
 
    /**
     * 从redis缓存中查询,反序列化
     *
     * @param redisKey
     * @return
     */
    public Object getDataFromRedis(String redisKey) {
        //查询
        Jedis jedis = jedisPool.getResource();
        try {
            byte[] result = jedis.get(redisKey.getBytes());
 
            //如果查询没有为空
            if (null == result) {
                return null;
            }
 
            //查询到了,反序列化
            return SerializeUtil.unSerialize(result);
        } finally {
            jedis.close();
        }
    }
 
    /**
     * 将数据库中查询到的数据放入redis
     *
     * @param redisKey
     * @param obj
     */
    public void setDataToRedis(String redisKey, Object obj, Integer seconds) {
 
        //序列化
        byte[] bytes = SerializeUtil.serialize(obj);
 
        SetParams params = null;
        if (seconds != null) {
            params = new SetParams().nx().ex(seconds);
        }
        //存入redis
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(redisKey.getBytes(), bytes, params);
        } finally {
            jedis.close();
        }
    }
 
}