转自:http://www.importnew.com/19321.html
1、String类型操作
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
|
public
class StringTest {
Jedis jedis;
@Before//该注解下的方法在@Test执行之前执行
public
void before(){
//创建连接
jedis = new Jedis(
"192.168.25.128"
,6379);
}
@
After
//该注解下的方法在@Test执行之后执行
public
void
after
(){
//关闭连接
jedis.
close
();
}
/*
* 存储,获取,设置过期时间,key命令
*/
@Test
public
void test() throws InterruptedException{
//1.存储值
jedis.
set
(
"name"
,
"张三"
);
//2.取值
String
name
= jedis.get(
"name"
);
System.
out
.println(
name
);
//3.
key
命令:查看有效期,-1表示持久化
Long t = jedis.ttl(
"name"
);
System.
out
.println(t);
//4.
key
命令,对已经存在的
key
设置过期时间
jedis.expire(
"name"
, 5);
while(
true
){
String name2 = jedis.get(
"name"
);
System.
out
.println(name2);
System.
out
.println(
"有效期为:"
+jedis.ttl(
"name"
)+
"秒"
);
Thread.sleep(2000);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
输出结果:
张三
-1
-------------------
张三
有效期为:5秒
张三
有效期为:3秒
张三
有效期为:1秒
null
有效期为:-2秒
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/*
* 测试自增自减:前提,value值能解析为数字类型
* 删除
*/
@Test
public
void test2(){
jedis.
set
(
"age"
,
"18"
);
String age1 = jedis.get(
"age"
);
System.
out
.println(
"当前年龄:"
+age1);
Long age2 = jedis.decr(
"age"
);
System.
out
.println(
"一年前年龄为:"
+age2);
Long age3 = jedis.incr(
"age"
);
System.
out
.println(
"现在年龄又变回为:"
+age3);
//如果
key
不存在则忽略此操作
Long del = jedis.del(
"age1"
);
}
|
1
2
3
4
|
输出结果:
当前年龄:18
一年前年龄为:17
现在年龄又变回为:18
|
1
2
3
4
5
6
7
8
9
10
11
12
|
/*
* 批量存储和获取
*/
@Test
public
void test3(){
jedis.mset(
"a1"
,
"mysql"
,
"a2"
,
"oracle"
,
"a3"
,
"sqlServer"
,
"a4"
,
"redis"
,
"a5"
,
"mongodb"
,
"a6"
,
"hbase"
);
List<string> list = jedis.mget(
"a1"
,
"a2"
,
"a3"
,
"a4"
,
"a5"
,
"a6"
);
for
(String s : list) {
System.
out
.println(s);
}
}</string>
|
1
2
3
4
5
6
7
|
输出结果:
mysql
oracle
sqlServer
redis
mongodb
hbase
|
1
2
3
4
5
6
7
8
9
10
11
12
|
/*
* 存储值的同时设置过期时间,判断key是否存在
*/
@Test
public
void test4() throws InterruptedException{
//jedis.setex(
key
, 过期时间, value)
jedis.setex(
"life"
, 5,
"享受美好"
);
while(jedis.exists(
"life"
)){
System.
out
.println(jedis.get(
"life"
));
Thread.sleep(2000);
}
}
|
1
2
3
4
|
输出结果:在输出三个“享受美好”之后便不再输出
享受美好
享受美好
享受美好
|
2、Hash类型操作【可以用来存实体】
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
|
public
class MapTest {
Jedis jedis;
@Before
public
void before(){
jedis = new Jedis(
"192.168.25.128"
,6379);
}
@
After
public
void
after
(){
jedis.
close
();
}
@Test
public
void test1(){
//1.存储值
jedis.hset(
"student"
,
"name"
,
"小李"
);
jedis.hset(
"student"
,
"class"
,
"小学生"
);
jedis.hset(
"student"
,
"age"
,
"10"
);
jedis.hset(
"student"
,
"skill"
,
"keng"
);
//2.获取指定值 获取名字
String
name
= jedis.hget(
"student"
,
"name"
);
System.
out
.println(
name
);
System.
out
.println(
"----------------------"
);
//3.获取存储的map
Map<string, string=
""
>
all
= jedis.hgetAll(
"student"
);
Set
<entry<string,string>>
set
=
all
.entrySet();
Iterator<entry<string, string=
""
>> iterator =
set
.iterator();
while (iterator.hasNext()) {
Entry<string, string=
""
> entry = iterator.
next
();
String
key
= entry.getKey();
String value = entry.getValue();
System.
out
.println(
key
+
":"
+value);
}
System.
out
.println(
"------------------------------"
);
//4.获取map中全部
key
Set
<string> keySet = jedis.hkeys(
"student"
);
for
(String
key
: keySet) {
System.
out
.println(
key
);
}
System.
out
.println(
"-------------------------------"
);
//5.获取map中全部
values
List<string> list = jedis.hvals(
"student"
);
for
(String value : list) {
System.
out
.println(value);
}
System.
out
.println(
"------------------"
);
//6.删除指定的值 删除map中class,
name
两对键值对
Long long1 = jedis.hdel(
"student"
,
"class"
,
"name"
);
Set
<string> set2 = jedis.hkeys(
"student"
);
for
(String
key
: set2) {
System.
out
.println(
key
);
}
System.
out
.println(
"-------------------"
);
//7.判断map是否存在
Boolean e = jedis.hexists(
"student"
,
"class"
);
System.
out
.println(
"class是否存在:"
+e);
System.
out
.println(
"-----------------"
);
//8.自增自减,可以指定增加减少的数值
jedis.hincrBy(
"student"
,
"age"
, 2);
System.
out
.println(jedis.hget(
"student"
,
"age"
));
}
}</string></string></string></string,></entry<string,></entry<string,string></string,>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
输出结果:
小李
----------------------
skill:keng
name
:小李
class:小学生
age:10
------------------------------
name
class
age
skill
-------------------------------
10
keng
小李
小学生
------------------
age
skill
-------------------
class是否存在:
false
-----------------
2年后年龄为:12
|
String跟Hash是最常见的类型,在实习这段时间也都只用到这两种,所以我觉得不管怎样这两种至少应该掌握。
3、List类型操作
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
|
public
class ListTest {
Jedis jedis;
@Before
public
void before(){
jedis = new Jedis(
"192.168.25.128"
,6379);
}
@
After
public
void
after
(){
jedis.
close
();
}
@Test
public
void test(){
//1.存储值(左边开始)。当成栈(子弹匣),先进先出,入栈
jedis.lpush(
"scores"
,
"100"
,
"90"
,
"80"
,
"70"
,
"60"
);
//右边开始存
jedis.rpush(
"scores"
,
"50"
,
"40"
,
"30"
,
"20"
,
"10"
);
//2.取值(左边开始),可以说是同时移除了该值,出栈
String lv = jedis.lpop(
"scores"
);
//右边开始取
String rv= jedis.rpop(
"scores"
);
System.
out
.println(
"左边取为:"
+lv);
System.
out
.println(
"右边取为:"
+rv);
System.
out
.println(
"--------------------"
);
//3.取所有值(只有左边开始取)0 -1表示取所有位置,位置是[start,
end
]
//redis命令行操作的时候已经演示过
List<string> list = jedis.lrange(
"scores"
, 0, -1);
for
(String value : list) {
System.
out
.println(value);
}
System.
out
.println(
"-----------"
);
//4.插队,44插入到100后面,注:没有什么rinsert()方法
jedis.linsert(
"scores"
,BinaryClient.LIST_POSITION .
AFTER
,
"100"
,
"44"
);
List<string> list2 = jedis.lrange(
"scores"
, 0, -1);
for
(String value : list2) {
System.
out
.println(value);
}
List<string> list3 = jedis.lrange(
"scores"
, 0, 3);
for
(String value : list3) {
System.
out
.println(value);
}
}
}</string></string></string>
|
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
|
输出结果:
左边取为:60
右边取为:10
--------------------
70
80
90
100
50
40
30
20
-----------
70
80
90
100
44
50
40
30
20
---------------
70
80
90
100
|
4、Set类型操作【去重、排序、集合运算】
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
|
public
class JedisTest {
Jedis jedis;
@Before
public
void before(){
jedis = new Jedis(
"192.168.25.128"
,6379);
}
@
After
public
void
after
(){
jedis.
close
();
}
/*
* 测试添加,取值,移除,判断
*/
@Test
public
void test1(){
//1.存储
jedis.sadd(
"names"
,
"Tom"
,
"Jack"
,
"Harry"
,
"Lucy"
,
"laowang"
);
//2.获取
set
中全部记录,取出来的跟存储的顺序不一样,进行自然排序了
Set
<string> members = jedis.smembers(
"names"
);
for
(String member : members) {
System.
out
.println(member);
}
//3.移除指定数据
jedis.srem(
"names"
,
"Tome"
,
"Jack"
);
//4.判断某值是否为
set
中成员
Boolean tom = jedis.sismember(
"names"
,
"Tome"
);
System.
out
.println(
"Tom在名单里吗:"
+tom);
}</string>
|
1
2
3
4
5
6
7
|
输出结果:
Jack
Harry
Lucy
Tom
laowang
Tom在名单里吗:
false
|
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
|
/*
* 差集,交集,并集
*/
@Test
public
void test2(){
jedis.sadd(
"set1"
,
"a"
,
"b"
,
"c"
,
"d"
);
jedis.sadd(
"set2"
,
"b"
,
"c"
,
"d"
,
"e"
);
//1.差集 set1中有set2中没有的
Set
<string> sdiff = jedis.sdiff(
"set1"
,
"set2"
);
for
(String d : sdiff) {
System.
out
.println(d);
}
System.
out
.println(
"----------------"
);
//2.交集
Set
<string> sinter = jedis.sinter(
"set1"
,
"set2"
);
for
(String
in
: sinter) {
System.
out
.println(
in
);
}
System.
out
.println(
"----------------"
);
//3.并集
Set
<string> sunion = jedis.sunion(
"set1"
,
"set2"
);
for
(String un : sunion) {
System.
out
.println(un);
}
}</string></string></string>
|
1
2
3
4
5
6
7
8
9
10
11
12
|
输出结果:
a
----------------
d
c
b
----------------
d
e
a
c
b
|
5、Zset(有序Set)类型操作【开销比较大】
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
|
public
class SortedSetTest {
Jedis jedis;
@Before
public
void before(){
jedis = new Jedis(
"192.168.25.128"
,6379);
}
@
After
public
void
after
(){
jedis.
close
();
}
@Test
public
void test1(){
//1.添加
jedis.zadd(
"table"
, 1,
"a"
);
jedis.zadd(
"table"
, 3,
"b"
);
jedis.zadd(
"table"
, 2,
"c"
);
jedis.zadd(
"table"
, 5,
"d"
);
jedis.zadd(
"table"
, 4,
"e"
);
//2.取值 0 -1表示取所有,可以自己指定开始结束位置,跟list一样
//默认根据分数由低到高排
Set
<string>
table
= jedis.zrange(
"table"
, 0, -1);
for
(String t :
table
) {
System.
out
.println(t);
}
System.
out
.println(
"-----------------"
);
//3.排序,由高到低排
Set
<string> table2 = jedis.zrevrange(
"table"
, 0, -1);
for
(String t : table2) {
System.
out
.println(t);
}
System.
out
.println(
"-----------------"
);
//4.修改某个值的分数
jedis.zincrby(
"table"
, 7,
"a"
);
Set
<string> table3 = jedis.zrevrange(
"table"
, 0, -1);
for
(String t : table3) {
System.
out
.println(t);
}
}
}</string></string></string>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
输出结果:
a
c
b
e
d
-----------------
d
e
b
c
a
-----------------
a
d
e
b
c
|