3.3 在Java中操作Redis
3.3.1 准备工作
引入jedis依赖
redis.clients
jedis
4.3.2
1
2
3
4
5
创建Redis连接,并测试连接
public class Main {
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");//redis连接池
try (Jedis jedis = jedisPool.getResource()){//获取数据源
String ping = jedis.ping();
System.out.println(ping);
}
}
}
1
2
3
4
5
6
7
8
9
返回pong,说明连接成功.
3.3.2 关于String的操作
get,set.
public class Main {
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");//redis连接池
try (Jedis jedis = jedisPool.getResource()){//获取数据源
stringTest(jedis);
}
}
private static void stringTest(Jedis jedis){
jedis.set("key1","value1");//set一次只可以设置一个键值对
jedis.set("key2","value2");
jedis.set("key3","value3");
String key1 = jedis.get("key1");
String key2 = jedis.get("key2");
String key3 = jedis.get("key100");
System.out.println(key1);
System.out.println(key2);
System.out.println(key3);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
mset,mget
private static void stringTest2(Jedis jedis){
jedis.flushAll();
jedis.mset("key1","value1","key2","value2","key3","value3");//key和value交替设置
List list = jedis.mget("key1","key2","key3");//由于value的值可以重复,所以使用的是list来接收
System.out.println(list);
}
1
2
3
4
5
6
在mset的时候,key和value是交替设置的.在mget接收value的时候,由于value的值是可以重复的.所以我们使用List来接收.
exists,del
private static void stringTest3(Jedis jedis){
jedis.flushAll();
jedis.set("key1","value1");
Boolean ret = jedis.exists("key1");//返回Boolean,即该key是否存在
System.out.println(ret);
Long ret1 = jedis.del("key1");//返回的是Long类型,即删除成功的个数
System.out.println(ret1);
}
1
2
3
4
5
6
7
8
exists返回的是Boolean,即该key是否存在,del返回的是Long类型,即删除成功的个数.
keys
private static void stringTest4(Jedis jedis){
jedis.flushAll();
jedis.set("key1","value1");
jedis.set("key2","value2");
jedis.set("key3","value3");
jedis.set("key4","value4");
jedis.set("key100","value100");
Set keys = jedis.keys("");//由于key不可以重复,所以我们使用set来接收
System.out.println(keys);
Set keys1 = jedis.keys("key?");//?只匹配一个字符
System.out.println(keys1);
Set keys2 = jedis.keys("key");//*可以匹配多个字符
System.out.println(keys2);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
expire,ttl
private static void stringTest5(Jedis jedis) throws InterruptedException {
jedis.flushAll();
jedis.set("key1","value1");
jedis.set("key2","value2");
jedis.set("key3","value3");
jedis.set("key4","value4");
jedis.expire("key1",5);
Long key1 = jedis.ttl("key1");
System.out.println(key1);
Thread.sleep(6000);//阻塞6秒
boolean ret = jedis.exists("key1");
System.out.println(ret);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
append
private static void stringTest6(Jedis jedis){
jedis.flushAll();
jedis.set("key1","value1");
jedis.append("key1", "aaa");
System.out.println(jedis.get("key1"));
}
1
2
3
4
5
6
getrange,setrange
getrange获取指定字符串区间的字符,setrange设置指定字符串区间的字符.
private static void stringTest7(Jedis jedis){
jedis.flushAll();
jedis.set("key1","value1aaaa");
System.out.println(jedis.getrange("key1", 0, 4));
jedis.setrange("key1",0,"bbbbb");
System.out.println(jedis.getrange("key1",0,4));
}
1
2
3
4
5
6
7
setnx
如果存在key,则不设置,返回0,不存在,则设置
private static void stringTest8(Jedis jedis){
jedis.flushAll();
jedis.set("key1","value1");
long setnx = jedis.setnx("key1", "value1");
System.out.println(setnx);
}
1
2
3
4
5
6
setex
设置键值对,并设置过期时间
private static void stringTest9(Jedis jedis) throws InterruptedException {
jedis.flushAll();
jedis.setex("key1",2,"value1");
Thread.sleep(3000);
String s = jedis.get("key1");
System.out.println(s);
}
1
2
3
4
5
6
7
incr,decr
给指定的key对应的value+1或-1
private static void stringTest10(Jedis jedis){
jedis.flushAll();
jedis.set("key1","1");
jedis.incr("key1");
System.out.println(jedis.get("key1"));
jedis.decr("key1");
System.out.println(jedis.get("key1"));
}
1
2
3
4
5
6
7
8
incrby,decrby
private static void stringTest11(Jedis jedis){
jedis.flushAll();
jedis.set("key1","1");
jedis.incrBy("key1",2);
System.out.println(jedis.get("key1"));
jedis.decrBy("key1",2);
System.out.println(jedis.get("key1"));
}
1
2
3
4
5
6
7
8
3.3.3 关于List的操作
lpush,lpop
private static void listTest1(Jedis jedis){
jedis.flushAll();
jedis.lpush("list1","v1","v2","v3");
System.out.println(jedis.lpop("list1"));
System.out.println(jedis.lpop("list1"));
System.out.println(jedis.lpop("list1"));
}
1
2
3
4
5
6
7
rpush,rpop
private static void listTest2(Jedis jedis){
jedis.flushAll();
jedis.rpush("key1","v1","v2","v3");
System.out.println(jedis.rpop("key1"));
System.out.println(jedis.rpop("key1"));
System.out.println(jedis.rpop("key1"));
}
1
2
3
4
5
6
7
lrange
查看指定下标区间的元素
private static void listTest3(Jedis jedis){
jedis.flushAll();
jedis.rpush("key1","v1","v2","v3");
System.out.println(jedis.lrange("key1",0,1));//左闭右闭
}
1
2
3
4
5
blpop
private static void listTest4(Jedis jedis){
jedis.flushAll();
jedis.lpush("key1","v1");
System.out.println(jedis.blpop(0, "key1"));
System.out.println(jedis.blpop(5, "key1"));
}
1
2
3
4
5
6
brpop和blpop同理,这里不再赘述.
lindex
获取指定下标的元素
private static void listTest5(Jedis jedis){
jedis.flushAll();
jedis.lpush("key1","v1","v2","v3");
System.out.println(jedis.lindex("key1", 2));
}
1
2
3
4
5
linsert
在指定的元素之前或者之后插入指定元素.
private static void listTest6(Jedis jedis){
jedis.flushAll();
jedis.lpush("key1","v1","v2");
jedis.linsert("key1", ListPosition.BEFORE,"v2","100");
System.out.println(jedis.lrange("key1",0,-1));
}
1
2
3
4
5
6
llen
private static void listTest7(Jedis jedis){
jedis.flushAll();
jedis.rpush("key1","v1","v2","v3");
System.out.println(jedis.llen("key1"));
}
1
2
3
4
5
3.3.4 关于Hash的操作
hset,hget
private static void hashTest1(Jedis jedis){
jedis.flushAll();
jedis.hset("key1","field1","value1");
jedis.hset("key1","field2","value2");
jedis.hset("key1","field3","value3");
System.out.println(jedis.hget("key1", "field1"));
System.out.println(jedis.hget("key1", "field2"));
System.out.println(jedis.hget("key1", "field3"));
}
1
2
3
4
5
6
7
8
9
hexists,hdel
private static void hashTest2(Jedis jedis){
jedis.flushAll();
jedis.hset("key1","field1","value1");
jedis.hset("key1","field2","value2");
jedis.hset("key1","field3","value3");
System.out.println(jedis.hexists("key1", "field1"));
System.out.println(jedis.hexists("key1", "field100"));
jedis.hdel("key1","field1");
System.out.println(jedis.hgetAll("key1"));
}
1
2
3
4
5
6
7
8
9
10
hkeys,hvals
private static void hashTest3(Jedis jedis){
jedis.flushAll();
jedis.hset("key1","field1","value1");
jedis.hset("key1","field2","value2");
jedis.hset("key1","field3","value3");
System.out.println(jedis.hkeys("key1"));//获取所有的key
System.out.println(jedis.hvals("key1"));//获取所有的val
}
1
2
3
4
5
6
7
8
hmget
private static void hashTest4(Jedis jedis){
jedis.flushAll();
jedis.hset("key1","field1","value1");
jedis.hset("key1","field2","value2");
jedis.hset("key1","field3","value3");
System.out.println(jedis.hmget("key1","field1","field2"));
}
1
2
3
4
5
6
7
hlen
private static void hashTest5(Jedis jedis){
jedis.flushAll();
jedis.hset("key1","field1","value1");
jedis.hset("key1","field2","value2");
jedis.hset("key1","field3","value3");
System.out.println(jedis.hlen("key1"));
}
1
2
3
4
5
6
7
hincrby,hincrbyfloat
private static void hashTest6(Jedis jedis){
jedis.flushAll();
jedis.hset("key1","field1","2");
jedis.hincrBy("key1","field1",1);
System.out.println(jedis.hget("key1","field1"));
jedis.hincrByFloat("key1","field1",1.5);
System.out.println(jedis.hget("key1","field1"));
}
1
2
3
4
5
6
7
8
3.3.5 关于set的操作
sadd,smember
private static void setTest1(Jedis jedis){
jedis.flushAll();
jedis.sadd("key1","m1","m2","m3");
jedis.sadd("key1","m1");
System.out.println(jedis.smembers("key1"));
}
1
2
3
4
5
6
srem,sismember
private static void setTest2(Jedis jedis){
jedis.flushAll();
jedis.sadd("key1","m1","m2","m3");
System.out.println(jedis.smembers("key1"));
jedis.srem("key1","m1");
System.out.println(jedis.smembers("key1"));
System.out.println(jedis.sismember("key1", "m1"));
System.out.println(jedis.sismember("key1", "m2"));
}
1
2
3
4
5
6
7
8
9
scard
private static void setTest3(Jedis jedis){
jedis.flushAll();
jedis.sadd("key1","m1","m2","m3");
System.out.println(jedis.scard("key1"));
}
1
2
3
4
5
sinter
private static void setTest4(Jedis jedis){
jedis.flushAll();
jedis.sadd("key1","m1","m2","m3");
jedis.sadd("key2","m2","m3","m4");
System.out.println(jedis.sinter("key1","key2"));
}
1
2
3
4
5
6
sunion和sdiff是同样的道理,这里不再赘述.
3.3.6 关于zset的操作
zadd,zrange,zrangeWithScores
private static void zsetTest1(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
System.out.println(jedis.zrange("key1",0,-1));
System.out.println(jedis.zrangeWithScores("key1",0,-1));//返回的是带有分数的二元组
}
1
2
3
4
5
6
7
8
zrem,zcard
private static void zsetTest2(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
jedis.zrem("key1","guanyu");
System.out.println(jedis.zcard("key1"));
System.out.println(jedis.zrangeWithScores("key1",0,-1));
}
1
2
3
4
5
6
7
8
9
zcount
返回指定分数区间的所有key
private static void zsetTest3(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
System.out.println(jedis.zcount("key1",90,91));
}
1
2
3
4
5
6
7
zpopmax,zpopmin
private static void zsetTest4(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
System.out.println(jedis.zpopmax("key1"));
System.out.println(jedis.zpopmin("key1"));
}
1
2
3
4
5
6
7
8
zrank
按升序的顺序返回指定元素的排名
private static void zsetTest5(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
System.out.println(jedis.zrank("key1","guanyu"));
}
1
2
3
4
5
6
7
zscore
获取指定元素的分数.
private static void zsetTest6(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
System.out.println(jedis.zscore("key1","zhaoyun"));
}
1
2
3
4
5
6
7
zincrby
给指定的元素加上指定的分数
private static void zsetTest7(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
jedis.zincrby("key1",10,"zhaoyun");
System.out.println(jedis.zscore("key1","zhaoyun"));
}
1
2
3
4
5
6
7
8
zinterstore
private static void zsetTest8(Jedis jedis){
jedis.flushAll();
jedis.zadd("key1",90,"zhangfei");
jedis.zadd("key1",91,"zhaoyun");
jedis.zadd("key1",92,"guanyu");
jedis.zadd("key2",90,"zhangfei");
jedis.zadd("key2",91,"zhaoyun");
jedis.zadd("key2",93,"zhugeliang");
jedis.zinterstore("key3","key1","key2");
System.out.println(jedis.zrangeWithScores("key3",0, -1));
}
1
2
3
4
5
6
7
8
9
10
11
union和diff是同样的道理,这里不再赘述.
3.4 在Spring中操作Redis
3.4.1 准备工作