随笔 - 232  文章 - 1  评论 - 24  阅读 - 45万

Redis部分数据结构方法小结

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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package com.practice.util;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
 
 
public class RedisUtil {
    private static final Log log = LogFactory.getLog(RedisUtil.class);
 
    private static JedisPool jedisPool;//非切片连接池
     
    private static final Object lock = new Object();
     
    private static final int DEFAULT_TIME_OUT = 30000;
     
    private static String redisIp = "192.168.77.153";
     
    private static Integer redisPort = 7000;
     
    /**
     * 构建redis切片连接池
     *
     * @param ip
     * @param port
     * @return JedisPool
     */
    public static JedisPool getJedisPool() {
        if (jedisPool == null) {
            synchronized (lock) {
                if (jedisPool == null) {
                    JedisPoolConfig config = new JedisPoolConfig();
                    //设置连接池初始化大小和最大容量
                     
                    // 控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
                    // 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
                    config.setMaxTotal(-1);
                     
                    // 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
                    config.setMaxIdle(1000);
                    // 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
                    config.setMaxWaitMillis(1000 * 30);
                    // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
                    config.setTestOnBorrow(true);
                    // 写
                    jedisPool = new JedisPool(config, redisIp, redisPort,DEFAULT_TIME_OUT);
                     
                }
            }
        }
        return jedisPool;
    }
     
    /**
     * 返还到连接池
     *
     * @param pool
     * @param redis
     */
    public static void returnJedisResource(Jedis redis) {
        if (redis != null) {
            redis.close();
        }
    }
     
    //直接set key-value
    public static void setStructure(String key,String value){
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            jedis.set(key, value);
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }
     
    public static void getSetStructure(String key){
        JedisPool pool = null;
        Jedis jedis = null;
        String value = "";
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            value = jedis.get(key);
            System.out.println(value);
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }
     
    //通过key删除数据
    public static void delKey(String key){
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            jedis.del(key);
            System.out.println("del key success");
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }
     
    //mset相当于 jedis.set("key1","value1");jedis.set("key2","value2")
    public static void msetData(String key1,String value1,String key2,String value2){
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            jedis.mset(key1,value1,key2,value2);
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }
     
    public static void flushData(){
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            jedis.flushAll();
            System.out.println("flushAll success");
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }
     
    //判断key是否存在,如果存在则返回1,否则则返回0
    public static boolean booleanExsit(String key){
        JedisPool pool = null;
        Jedis jedis = null;
        Boolean exsit = false;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            exsit =  jedis.exists(key);
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
        return exsit;
    }
     
    public static void appendData(String key,String data){
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            jedis.append(key, data);
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }
     
    //截取value的值
    public static void getRange(String key){
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            String value = jedis.getrange(key, 0, 1);
            System.out.println(value);
            System.out.println();
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }
     
    //列表操作 用于将一个或多个值插入到列表的尾部(最右边), 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。
    public static void rpush(String key){
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            jedis.rpush(key, "Hello how are you?"); 
            jedis.rpush(key, "Fine thanks. I'm having fun with redis."); 
            jedis.rpush(key, "I should look into this NOSQL thing ASAP");        
            } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }
     
    //取出列表中相应位置的值
    public static void getPushValue(String key){
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            //第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有 
            List<String> values = jedis.lrange(key, 0, -1);
            System.out.println(values);
            } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }
     
    public static void Set(String key){
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            jedis.sadd("myset", "1"); 
            jedis.sadd("myset", "2"); 
            jedis.sadd("myset", "3"); 
            jedis.sadd("myset", "4"); 
            Set<String> setValues = jedis.smembers("myset");
            System.out.println(setValues);
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }
     
    public static void srem(String key){
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            jedis.srem(key, "4");
            Set<String> setValues = jedis.smembers("myset");
            System.out.println(setValues);
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }
     
    public static void hmset(){
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            Map<String, String> pairs = new HashMap<String, String>();
            pairs.put("name", "Akshi"); 
            pairs.put("age", "2"); 
            pairs.put("sex", "Female"); 
            jedis.hmset("kid", pairs); 
            List<String> name = jedis.hmget("kid", "name");
            System.out.println(name);
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }
     
    public static void increment(){
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getJedisPool();
            jedis = pool.getResource();
            jedis.hset("hashs", "entryKey", "1"); 
            jedis.hset("hashs", "entryKey1", "entryValue1"); 
            jedis.hset("hashs", "entryKey2", "entryValue2"); 
            // 判断某个值是否存在
            jedis.hexists("hashs", "entryKey");
            System.out.println(jedis.hincrBy("hashs", "entryKey", 1)); 
        } catch (Exception e) {
            log.error(e);
        } finally {
            //返还到连接池
              returnJedisResource(jedis);
        }
    }

  

Redis在工程开发中还是比较常用的Nosql内存数据库,简单巩固一下它的各种数据类型与用法~

posted on   松伯  阅读(286)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示