forked from tanglijiong/MyJavaStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisTest.java
More file actions
125 lines (98 loc) · 2.87 KB
/
Copy pathRedisTest.java
File metadata and controls
125 lines (98 loc) · 2.87 KB
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
package com.xjj.cache;
import java.util.HashSet;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import com.xjj.BaseJunit4Test;
import com.xjj.spring.data.XjjStringRedisOps;
@ContextConfiguration ({"classpath:spring/applicationContext-redis.xml"}) //加载配置文件
public class RedisTest extends BaseJunit4Test {
@Autowired
XjjStringRedisOps redisOps;
@Test
public void deleteTestKeys(){
redisOps.DEL("xjj.v1");
redisOps.DEL("xjj.v2");
redisOps.DEL("xjj.h1");
redisOps.DEL("xjj.11");
redisOps.DEL("xjj.z1");
HashSet<String> keySet = (HashSet<String>) redisOps.keys("xjj*");
System.out.println(keySet);
Assert.assertEquals("keys删除不成功!", new HashSet<String>(), keySet);
}
//String类型测试
@Test
public void setStringValue(){
String key = "xjj.v1";
String value = "Hello World2!";
redisOps.SET(key, value);
System.out.println("value of xjj: " + redisOps.GET(key));
Assert.assertEquals(value, redisOps.GET(key));
}
@Test
public void setStringValueWithTimeout(){
String key = "xjj.v2";
String value = "Hello World, xjj1!";
long timeout = 100;
redisOps.SET(key, value, timeout);
System.out.println("ttl of xjj1: " + redisOps.TTL(key));
Assert.assertEquals(timeout, redisOps.TTL(key));
}
//Hash类型测试
@Test
public void setHashValue(){
String key = "xjj.h1";
String field1 = "f1";
String value1 = "123";
redisOps.HSET(key, field1, value1);
System.out.println(redisOps.HGET(key, field1));
String field2 = "f2";
String value2 = String.valueOf(888);
redisOps.HSET(key, field2, value2);
System.out.println(redisOps.HGET(key, field2));
System.out.println(redisOps.HGETALL(key));
}
@Test
public void delHashKeys(){
redisOps.HDEL("xjj.h1", "f1", "f2"); //删除xjj.h1中的f1和f2
}
//List类型测试
@Test
public void ListOps(){
String key = "xjj.l1";
String value1 = "Java";
redisOps.LPUSH(key, value1);
String value2 = "php";
redisOps.RPUSH(key, value2);
System.out.println(redisOps.RPOP(key));
System.out.println(redisOps.LPOP(key));
}
//Set类型测试
@Test
public void SetOps(){
String key = "xjj.s1";
String member1 = "Java";
String member2 = "Python";
String member3 = "PHP";
redisOps.SADD(key, member1);
redisOps.SADD(key, member2);
redisOps.SADD(key, member3);
System.out.println(redisOps.SMEMEBERS(key));
}
//SortedSet类型测试
@Test
public void ZSetOps(){
String key = "xjj.z1";
String member1 = "Java";
String member2 = "Python";
String member3 = "PHP";
double score1 = 1.2;
double score2 = 100.5;
double score3 = 188;
redisOps.ZADD(key, score1, member1);
redisOps.ZADD(key, score2, member2);
redisOps.ZADD(key, score3, member3);
System.out.println(redisOps.ZRANGE(key, 1, 150));
}
}