fwq
2022-12-26 217258891f8d90a0586a1d785b9ce72f7e226666
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.data.redis.core.ListOperations;
5 import org.springframework.data.redis.core.RedisTemplate;
6 import org.springframework.data.redis.core.ValueOperations;
7 import org.springframework.stereotype.Component;
8
9 import java.util.concurrent.TimeUnit;
10
11 /**
12  * @PackageName com.hx.util
13  * @ProjectName hx-parent
14  * @Author: ChenJiaHe
15  * @Date: Create in 16:51 2019/7/22
16  * @Description:
17  * @Copyright Copyright (c) 2019, hx01@163.com All Rights Reserved.
18  */
19 @Component
20 public class MyRedisTemplate<E> {
21     @Autowired
22     private RedisTemplate redisTemplate;
23
24     @Autowired
25     private SerializeUtil<E> serialize;
26
27     public static ListOperations<String, String> list;
28
29     /**
30      * 根据键值获取value
31      * @param key
32      * @return
33      */
34     public E get(String key) {
35         ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
36         return key == null ? null : serialize.unserialize(opsForValue.get(key));
37     }
38
39     /**
40      * 设置键值对(无超时时间)
41      * @param key
42      * @param value
43      * @return
44      */
45     public boolean set(String key, E value) {
46         ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
47         try {
48             opsForValue.set(key, serialize.serialize(value));
49             return true;
50         } catch (Exception e) {
51             e.printStackTrace();
52             return false;
53         }
54     }
55
56     /**
57      * 设置键值对 (设置有效时间,以秒为单位)
58      *
59      * @param key
60      * @param value
61      * @param expire
62      * @return boolean
63      */
64     public boolean set(String key, E value, long expire) {
65         ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
66         try {
67             opsForValue.set(key, serialize.serialize(value), expire, TimeUnit.SECONDS);
68             return true;
69         } catch (Exception e) {
70             e.printStackTrace();
71             return false;
72         }
73     }
74
75     /**
76      * 根据key删除键值对
77      *
78      * @param key
79      * @return
80      */
81     public boolean delete(String key) {
82         try {
83             redisTemplate.delete(key);
84         } catch (Exception e) {
85             e.printStackTrace();
86             return false;
87         }
88         return true;
89     }
90
91     /*
92      * 模拟队列
93      */
94
95     /**
96      * lpush 进队列
97      *
98      * @param key
99      * @param value
100      * @return
101      */
102     public Long lpush(String key, String value) {
103         list = redisTemplate.opsForList();
104         try {
105             Long leftPush = list.leftPush(key, value);
106             return leftPush;
107         } catch (Exception e) {
108             e.printStackTrace();
109             return Long.valueOf(-1);
110         }
111     }
112
113     /**
114      * rpop 出队列
115      *
116      * @param key
117      * @return
118      */
119     public String rpop(String key) {
120         list = redisTemplate.opsForList();
121         try {
122             return list.rightPop(key);
123         } catch (Exception e) {
124             e.printStackTrace();
125             return null;
126         }
127     }
128
129     /**
130      * 返回指定队列(list)名队列长度
131      *
132      * @param key
133      * @return
134      */
135     public Long llen(String key) {
136         list = redisTemplate.opsForList();
137         return list.size(key);
138     }
139 }