1. string(字符串)
Redis的字符串就是由字节组成的序列,和其他语言基本没什么区别,在Redis字符串可以存储4种类型的值:
- 字符串
- 字节串
- 整数
- 浮点数
对于整数和浮点数,可以执行自增和自减操作,Redis还可以将整数转换成浮点数。整数的取值范围和系统的长整型(long integer)的取值范围相同,而浮点数的取值范围和精度则与IEEE754标准的双精度浮点数(double)一致。
自增自减命令
命令 | 描述 |
---|---|
incr key-name | 将键上的值加1 |
decr key-name | 将键上的值减1 |
incrby key-name amount | 将键上的值增加整数amount |
decrby key-name amount | 将键上的值减少整数amount |
incrbyfloat key-name amount | 将键上的值增加浮点数amount |
对于自增自减命令,只能对存在的值可以被解释成十进制整数或者浮点数,如果对一个不存在的键或者键上存的值为空执行自增自减操作,会将键上的值当成0,如果键上存的值不能被解释为整数或者浮点数,就会报错。
Code(java)
Long incr = conn.incr("key");
System.out.println(key + " + 1 = " + incr);
Long incr15 = conn.incrBy("key", 15);
System.out.println(incr + " + 15 = " + incr15);
Long decr1 = conn.decr("key");
System.out.println(incr15 + " - 1 = " + decr1);
Long decr4 = conn.decrBy("key", 4);
System.out.println(decr1 + " - 4 = " + decr4);
Double incrByFloat = conn.incrByFloat("key", 22.03D);
System.out.println(decr4 + " + 22.03 = " + incrByFloat);
除了对数值类型的值有以上操作,还有对字符串和二进制位的一些操作命令。
对字符串和二进制位的命令
命令 | 描述 |
---|---|
append key-name value | 字符串拼接 |
getrange key-name start end | 返回偏移量从start到end的子串,包括start和end位 |
setrange key-name offset | 将offset位置(包括)之后的值一对一替换 |
getbit key-name offset | 将键上的值看作二进制位串(string bit),并返回位于偏移量offset位的二进制位的值设置位value |
bitcount key-name [start end] | 统计二进制位串里1的个数,如果给定偏移量,那么只对给定偏移量范围内的二进制位串的进行统计 |
bittop operation dest-key key-name [key-name ...] | 对一个或者多个二进制位串执行包括并(and)、或(or)、异或(XOR)、非(not)在内的任何一种按位运算操作,并将计算的结果存储到dest-key键里面 |
对于偏移量超过字符串长度的使用null,二进制用0代替。
Code
conn.del("new-string-key");
conn.append("new-string-key", "hello");
conn.append("new-string-key", " world!");
String hellworld = conn.get("new-string-key");
System.out.println("new-string-key:" + hellworld);
String getrange05 = conn.getrange("new-string-key", 0, 5);
System.out.println("helloworld(0,5):" + getrange05);
conn.setrange("new-string-key", 5, " xdq!");
assert conn.get("new-string-key").equals("hello xdq!");
System.out.println(conn.get("new-string-key"));
conn.del("bit-string-key");
conn.set("bit-string-key", "1001");
conn.setbit("bit-string-key", 1, "1");
System.out.println("1001 : " + conn.get("bit-string-key"));
2. list(列表)
Redis的列表可以从两端插入或者弹出元素,一个键存储一个或多个字符串序列。
常用命令
命令 | 描述 |
---|---|
rpush/lpush key-name value [value ...] | 将一个或多个值从右/左端推入到列表中 |
rpop/lpop key-name | 移除并返回最右/左端的值 |
lindex key-name offset | 返回列表中偏移量为offset的值 |
lrange key-name start end | 返回列表中偏移量从start到end的值,包括(start和end) |
ltrim key-name start end | 对列表进行裁剪,只保留偏移量start到end的值(包括start和end) |
blpop/brpop key-name [key-name ...] timeout | 从第一个非空列表中弹出最左/右的的元素,或者在timeout秒之内阻塞并等待可弹出的元素出现 |
rpoplpush source-key dest-key | 从source-key列表中弹出最右端的元素,然后将这个元素推入到dest-key的最左端,并返回这个元素 |
brpoplpush source-key dest-key timeout | 从source-key列表中弹出最右端的元素,然后将这个元素推入到dest-key的最左端,并返回这个元素;如果source-key为空,那么在timeout之内阻塞并等待可弹出的元素出现 |
Code(java)
System.out.println("=====列表常用命令=====");
conn.del("list-key");
conn.lpush("list-key", "1");
System.out.println(conn.lrange("list-key", 0, -1));
conn.rpush("list-key", "2");
System.out.println(conn.lrange("list-key", 0, -1));
assert Objects.equals("1", conn.lindex("list-key", 0));
conn.lpush("list-key", "0");
conn.rpush("list-key", "3");
assert Objects.equals(Arrays.asList("0", "1", "2", "3"), conn.lrange("list-key", 0, -1));
System.out.println(conn.lrange("list-key", 0, -1));
String rpop = conn.rpop("list-key");
System.out.println("rpop:" + rpop);
conn.lpop("list-key");
assert Objects.equals(Arrays.asList("1", "2"), conn.lrange("list-key", 0, -1));
System.out.println(conn.lrange("list-key", 0, -1));
String ltrim = conn.ltrim("list-key", 0, 0);
System.out.println("ltrim:" + ltrim);
assert Objects.equals(Collections.singletonList("1"), conn.lrange("list-key", 0, -1));
System.out.println(conn.lrange("list-key", 0, -1));
conn.del("new-list-key1");
conn.del("new-list-key2");
conn.rpush("new-list-key1", "a", "b");
conn.rpush("new-list-key2", "1", "2");
List<String> blpop = conn.blpop(1, "new-list-key1", "new-list-key2");
System.out.println("blpop:" + blpop);
List<String> brpop = conn.brpop(1, "new-list-key1", "new-list-key2");
System.out.println("brpop:" + brpop);
conn.del("new-list-key3");
conn.del("new-list-key4");
conn.rpush("new-list-key3", "a", "b", "c");
conn.rpush("new-list-key4", "d", "e", "f");
for (int i = 0; i < 3; i++){
conn.rpoplpush("new-list-key3", "new-list-key4");
}
System.out.println(conn.lrange("new-list-key4", 0,-1));
3. set(集合)
Redis的集合以无序的方式存储多个互不相同的元素,集合有添加元素、移除元素、检查元素是否存在,以及多个集合的交集、并集和差集运算命令。
命令操作
命令 | 描述 |
---|---|
sadd key-name value [value ...] | 向集合添加一个或者多个元素 |
srem key-name value [value ...] | 从集合移除一个或者多个元素 |
sismember key-name value | 检查元素是否存在与集合中 |
smembers key-nam | 返回集合中所有的元素 |
scard key-name | 返回集合中元素的数量 |
srandmember key-name [count] | 从集合中随机返回一个或者count个元素,当count>0时,返回的元素不回重复;当count<0时,返回的元素可能会重复;当count=0时,返回空集合 |
spop key-name | 返回随机移除集合中的元素 |
smove source-key dest-key item | 如果source-key中有item元素,则将item元素移动到dest-key中,成果则返回1,否则返回0 |
sdiff key-name [key-name ...] | 返回存在在第一个集合中而不存在后面的集合中的元素(差集) |
sdiffstore dest-key key-name [key-name ...] | 类似sdiff命令,不同的是此命令将结果存到了dest-key集合中 |
sinter key-name [key-name ...] | 返回同时存在于所有集合中的元素(交集) |
sinterstore dest-key key-name [key-name ...] | 将交集结果存储到 |
sunion key-name [key-name ...] | 返回集合的并集结果 |
sunionstore dest-key key-name [key-name ...] | 将并集结果存储在dest-key集合中 |
Code(Java)
System.out.println("======集合常用命令=====");
conn.del("set-key");
//sadd 向集合里添加元素(一个或者多个),返回插入元素非重复的个数
Long sadd = conn.sadd("set-key", "a", "b", "c");
System.out.println(sadd);
Long d = conn.sadd("set-key", "c");
System.out.println(d);
Long srem = conn.srem("set-key", "a");
assert srem==1;
assert !conn.sismember("set-key", "a");
Long size = conn.scard("set-key");
System.out.println("集合中的数量:" + size);
Set<String> memberList = conn.smembers("set-key");
System.out.println("集合中的成员:" + memberList);
for (int i = 0; i < 10; i++){
System.out.println("count是默认,随机从集合中获取成员:" + conn.srandmember("set-key"));
System.out.println("count是正数,随机从集合中获取成员:" + conn.srandmember("set-key", 4));
System.out.println("count是负数,随机从集合中获取成员:" + conn.srandmember("set-key", -4));
System.out.println("count是0,随机从集合中获取成员:" + conn.srandmember("set-key", 0));
}
String spop = conn.spop("set-key");
System.out.println("spop是随机移除一个元素:" + spop);
conn.sadd("new-set-key", "a");
Long a = conn.smove("new-set-key", "set-set-key", "a");
assert a==1;
System.out.println("=====用于组合和处理多集合的命令=====");
conn.del("new-set-key-1");
conn.del("new-set-key-2");
conn.del("new-set-key-3");
conn.del("new-set-key-4");
conn.del("new-set-key-5");
conn.sadd("new-set-key-1","first", "second", "one", "two", "three");
conn.sadd("new-set-key-2", "one", "two", "second");
Set<String> sdiff12 = conn.sdiff("new-set-key-1", "new-set-key-2");
System.out.println(conn.smembers("new-set-key-1") + " - " + conn.smembers("new-set-key-2") + " = " + sdiff12);
Set<String> sdiff21 = conn.sdiff("new-set-key-2", "new-set-key-1");
System.out.println(conn.smembers("new-set-key-2") + " - " + conn.smembers("new-set-key-1") + " = " + sdiff21);
conn.sdiffstore("new-set-key-3", "new-set-key-1", "new-set-key-2");
assert Objects.equals(new HashSet<>(Arrays.asList("first", "three")), conn.smembers("new-set-key-3"));
System.out.println("new-set-key-3:" + conn.smembers("new-set-key-3"));
Set<String> sinter12 = conn.sinter("new-set-key-1", "new-set-key-2");
System.out.println(conn.smembers("new-set-key-1") + "交集" + conn.smembers("new-set-key-2") + " = " + sinter12);
conn.sinterstore("new-set-key-4", "new-set-key-1", "new-set-key-2");
System.out.println("new-set-key-4:" + conn.smembers("new-set-key-4"));
Set<String> sunion = conn.sunion("new-set-key-1", "new-set-key-2");
System.out.println(conn.smembers("new-set-key-1") + "并集" + conn.smembers("new-set-key-2") + " = " + sunion);
conn.sunionstore("new-set-key-5", "new-set-key-1", "new-set-key-2");
System.out.println("new-set-key-5:" + conn.smembers("new-set-key-5"));
4. hash(散列)
在《Redis的5种数据结构》这篇文章中提到过Redis的散列是将多个健值对存储在一个键中,对应的结构图形可以去上这一篇文章去看看。
常用操作命令
命令 | 描述 |
---|---|
hset key-name field value | 往散列中添加一对健值对 |
hmset key-name field value [field value ...] | 往散列中添加一对或者多对健值对 |
hget key-name field | 从散列中获取对应键的值 |
hmget key-name field [field ...] | 从散列中获取一个或者多个键的值 |
hdel key-name field [field ...] | 删除散列中一个或者多个健值对,并返回删除的数量 |
hlen key-name | 返回散列中健值对的数量 |
hexists key-name field | 检查散列中是否存在此键 |
hkeys key-name | 获取散列中的所有键 |
hvals key-name | 获取散列中的所有值 |
hgetall key-name | 获取散列中的所有健值对 |
hincrby key-name field increment | 将散列中对应键的值增加上整数increment,值必须是数值 |
hincrbyfloat key-name field increment | 将散列中对应键的值增加上浮点数,值必须是数值 |
Code(Java)
conn.del("new-hash-key1");
conn.del("new-hash-key2");
System.out.println("=====散列的API命令=====");
conn.hset("new-hash-key1", "name", "xdq");
conn.hset("new-hash-key1", "age", "11");
Map<String, String> map = new HashMap<>(4);
map.put("name", "xdq");
map.put("age", "11");
conn.hmset("new-hash-key2", map);
List<String> hmget = conn.hmget("new-hash-key1", "name", "age");
List<String> hget = Arrays.asList(conn.hget("new-hash-key2", "name"),
conn.hget("new-hash-key2", "age"));
assert Objects.equals(hget, hmget);
System.out.println(hmget);
Map<String, String> hgetAll = conn.hgetAll("new-hash-key1");
System.out.println(hgetAll);
conn.hdel("new-hash-key1", "name");
assert conn.hlen("new-hash-key1") == 1;
assert conn.hexists("new-hash-key1", "age");
List<String> hkeys = new ArrayList<>(conn.hkeys("new-hash-key2"));
List<String> hvals = conn.hvals("new-hash-key2");
for (int i = 0; i < hvals.size(); i++){
System.out.println("key: " + hkeys.get(i) + ",value: " + hvals.get(i));
}
Map<String, String> keyValues = conn.hgetAll("new-hash-key2");
keyValues.forEach((k,v) -> System.out.println(k + ": " + v));
conn.hincrBy("new-hash-key2", "age", 1);
assert Objects.equals("2", conn.hget("new-hash-key2", "age"));
conn.hincrByFloat("new-hash-key2", "age", 0.0D);
assert Objects.equals("2.0", conn.hget("new-hash-key2", "age"));
5. zset(有序集合)
有序集合和散列存储的健与值的映射类似,有序集合也存储着member(成员)和score(分值,分值是浮点型)的映射,并提供了分值的处理命令,有序集合默认是正序(从小到大)
命令操作
命令 | 描述 |
---|---|
zadd key-name score member [score member ...] | 向有序集合添加一个或者多个成员分值对 |
zrem key-name member [member ...] | 删除有序集合一个或者多个成员 |
zcard key-name | 返回有序集合中的成员数 |
zincrby key-name increment member | 给有序集合中给定成员的分值增加上increment |
zcount key-name min max | 返回有序集合中分值在min和max之间的成员数量 |
zrank key-name member | 返回成员在有序集合中排名,第一名为0值 |
zscore key-name member | 返回成员在有序集合中的分值 |
zrange key-name start stop [withscore] | 返回有序集合中分值排名在start和stop之间的成员,withscore是返回成员和分值 |
zrevrank key-name member | 返回有序集合中member的排名,由大到小排序 |
zrevrange key-name start stop [withscore] | 命令类似zrange,只是有大到小排序的 |
zrangebyscore key-name min max [withscore] [limit offset count] | 返回有序集合中分值介于min和max的所有成员,withscore于上面的一样,offset和count是对之前的结果进行截取,意思就是从offset之后的前count个 |
zrevrangebyscore key-name max min [withscore] [limit offset count] | 于上一条命令一样,只是排序是从大到小 |
zremrangebyrank key-name start stop | 移除有序集合中分值排名介于start和stop之间的所有成员 |
zremrangebyscore key-name min max | 移除有序集合中分值在min和max之间的所有成员 |
zinterscore dest-key key-name [key-name ...] [weight weight [weight ...]] [aggregate sum|min|max] | 交集运算,将结果存储到dest-key有序集合中,sum、min和max是对值的计算,默认是求和 |
zunionstore dest-key key-name [key-name ...] [weight weight [weight ...]] [aggregate sum|min|max] | 并集运算,默认是求和 |
Code(Java)
conn.del("zset-key");
System.out.println("=====有序集合的常用命令=====");
conn.zadd("zset-key", 89, "member01");
conn.zadd("zset-key", 99.5, "member02");
Long zcard = conn.zcard("zset-key");
assert 2 == zcard;
conn.zrem("zset-key", "member01");
conn.zadd("zset-key", 79, "member01");
conn.zadd("zset-key", 85.5, "member03");
conn.zincrby("zset-key", 2, "member03");
Long zcount = conn.zcount("zset-key", 80, 100);
assert 2 == zcount;
Long member01 = conn.zrank("zset-key", "member01");
System.out.println("member01 的排名是: " + member01);
Double member01Score = conn.zscore("zset-key", "member01");
System.out.println("member01 的成绩是: " + member01Score);
Set<String> zrange = conn.zrange("zset-key", 0, -1);
System.out.println(zrange);
Set<Tuple> tuples = conn.zrangeWithScores("zset-key", 0, -1);
tuples.forEach(tuple -> System.out.println(tuple.getElement() + " : " + tuple.getScore()));
System.out.println("=====有序集合的范围型和倒序(由大到小)API=====");
Long member02 = conn.zrevrank("zset-key", "member02");
assert 0 == member02;
Set<String> zrevrange = conn.zrevrange("zset-key", 0, -1);
System.out.println(zrevrange);
Set<String> zrangeByScore = conn.zrangeByScore("zset-key", 87.5, 99.5, 0, 1);
System.out.println("zrangeByScore: " + zrangeByScore);
conn.del("zset-key-1");
conn.del("z-dest-key1");
conn.del("z-dest-key2");
conn.zadd("zset-key-1",33, "student01");
conn.zadd("zset-key-1",77, "member01");
ZParams zParams = new ZParams();
zParams.aggregate(ZParams.Aggregate.MAX);
//zparams 默认是求和
conn.zinterstore("z-dest-key1", zParams, "zset-key", "zset-key-1");
Set<Tuple> destKey1 = conn.zrangeWithScores("z-dest-key1", 0, -1);
destKey1.forEach(tuple -> System.out.println(tuple.getElement() + " : " + tuple.getScore()));
zParams.aggregate(ZParams.Aggregate.MIN);
//zparams 默认是求和
conn.zunionstore("z-dest-key2", zParams,"zset-key", "zset-key-1");
Set<Tuple> destKey2 = conn.zrangeWithScores("z-dest-key2", 0, -1);
destKey2.forEach(tuple -> System.out.println(tuple.getElement() + " : " + tuple.getScore()));
源代码
持续更新中......