jedis常规操作演示

1.常用操作

本次只列出了String 、hash、List,顺便复习了一下map的几种遍历方法

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
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import redis.clients.jedis.Jedis;


public class redisTest {
@Test
public void testString(){
//1.连接redis
Jedis jedis=new Jedis("127.0.0.1",6379);
//2.操作redis
jedis.set("name", "dumeng");
String name=jedis.get("name");
System.out.println(name);
//3.关闭redis
jedis.close();
}

@Test
public void testList(){
//1.连接redis
Jedis jedis=new Jedis("127.0.0.1",6379);
//2.操作redis
jedis.lpush("list1", "a","b","c");
jedis.rpush("list1", "d");
List<String> list1 = jedis.lrange("list1", 0, -1);
for (String string : list1) {
System.out.print(string+" ");
}
System.out.println("\n"+jedis.llen("list1"));
System.out.println();
//3.关闭redis
jedis.close();
}
@Test
public void testHash(){
//1.连接redis
Jedis jedis=new Jedis("127.0.0.1",6379);
//2.操作redis
jedis.hset("hash1", "a1", "a1");
jedis.hset("hash1", "a2", "a2");
Map<String, String> map1 = jedis.hgetAll("hash1");

//常见遍历map的几种方法
//1.foreach 方法
// for(Map.Entry<String, String> entry : map1.entrySet()){
// System.out.println("key: "+entry.getKey()+" value: "+entry.getValue());
// }
//2.foreach循环中遍历keys或者values
// for(String key:map1.keySet()){
// System.out.println("key: "+key);
// }
// for(String value:map1.values()){
// System.out.println("value: "+value);
// }
//3.使用Iterator
Iterator<Map.Entry<String, String>> entry=map1.entrySet().iterator();
while(entry.hasNext()){
Map.Entry<String, String> entries=entry.next();
System.out.println("key: "+entries.getKey()+" value: "+entries.getValue());

}
System.out.println(jedis.hlen("hash1"));
System.out.println();
//3.关闭redis
jedis.close();
}

}

2.实际案例

image-20200117103507090

代码:

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
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;

public class Service {
private String id;
private int num;
public Service(String id,int num){
this.id=id;
this.num=num;
}
// 控制单元
public void service() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
String value = jedis.get("comid" + id);
// 判断该值是否存在
try {
if (value == null) {
// 不存在
jedis.setex("comid" + id, 5, Long.MAX_VALUE - num + "");
} else {

Long incr = jedis.incr("comid" + id);
business(id,num-(Long.MAX_VALUE-incr));

}
} catch (JedisDataException e) {
System.out.println("使用次数到达上限,请升级会员");
return;
} finally {
jedis.close();
}

}

// 业务操作
public void business(String id, Long incr) {
System.out.println("用户"+id+"业务操作执行"+incr+"次");

}

public static void main(String[] args) {
// TODO Auto-generated constructor stub
MyThread mt1 = new MyThread("初级用户",10);
MyThread mt2 = new MyThread("高级用户",30);
mt1.start();
mt2.start();
}

}

class MyThread extends Thread {
Service sc;
public MyThread(String id,int num){
sc = new Service(id,num);
}

@Override
public void run() {
while (true) {
sc.service();
try {
Thread.sleep(300L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

运行截图:

image-20200117111521663

-------------本文结束感谢您的阅读-------------
0%