一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Redis学习入门之常用的数据类型

时间:2022-06-29 10:47:17 编辑:袖梨 来源:一聚教程网

简单的使用:
set插入字符串,get查看

[root@yum-down ~]# redis-cli -h 10.10.0.250 -p 6379
10.10.0.250:6379> set keyname hello
OK
10.10.0.250:6379> get keyname
"hello"
10.10.0.250:6379>
过滤:
显示所有

10.10.0.250:6379> keys *
1) "keya"
2) "keyna"
3) "keyname1"
4) "keyname"
匹配keyname开头

10.10.0.250:6379> keys keyname?
1) "keyname2"
2) "keyname1"
10.10.0.250:6379>
判断key是否存在,0不存在,非0则存在,表示数量

10.10.0.250:6379> EXISTS keya
(integer) 1
10.10.0.250:6379> EXISTS keya1
(integer) 0
10.10.0.250:6379>
删除:0不存在,非0则存在,表示数量

10.10.0.250:6379> DEL keya
(integer) 1
10.10.0.250:6379> DEL keya
(integer) 0
10.10.0.250:6379>
info:

Keyspace
db0:keys=2,expires=0,avg_ttl=0
db0:实例
如下:SELECT 1和2两个实例,分别插入数据

10.10.0.250:6379[2]> SELECT 1
OK
10.10.0.250:6379[1]> SELECT 2
OK
10.10.0.250:6379[2]> SET 1 1
OK
10.10.0.250:6379[2]> SELECT 1
OK
10.10.0.250:6379[1]> SET 1 1
OK
在使用info查看就有了1和2的实例

Keyspace
db0:keys=2,expires=0,avg_ttl=0
db1:keys=1,expires=0,avg_ttl=0
db2:keys=1,expires=0,avg_ttl=0
flushall可清空所有实例
10.10.0.250:63791> flushall

keys:key数量
expires:过期数量
avg_ttl:ttl数量

追加:append
通常使用set也可以追加

127.0.0.1:6379> set 1 hello
OK
127.0.0.1:6379> get 1
"hello"
set追加
127.0.0.1:6379> set 1 hello,word
OK
127.0.0.1:6379> get 1
"hello,word"
append追加
127.0.0.1:6379> append 1 w
(integer) 11
127.0.0.1:6379> append 1 word
(integer) 15
127.0.0.1:6379> get 1
"hello,wordwword"
127.0.0.1:6379>
单个数据追加:INCR

127.0.0.1:6379> incr new
(integer) 1
127.0.0.1:6379> incr new
(integer) 2
127.0.0.1:6379> incr new
(integer) 3
127.0.0.1:6379> incr new
(integer) 4
127.0.0.1:6379> incr new
(integer) 5
127.0.0.1:6379> get new
"5"
127.0.0.1:6379>
追加多个:INCRBY

127.0.0.1:6379> get new
"5"
127.0.0.1:6379> incrby new 100
(integer) 105
127.0.0.1:6379> get new
"105"
127.0.0.1:6379>
减少:DECR

127.0.0.1:6379> get new
"105"
127.0.0.1:6379> DECR new
(integer) 104
127.0.0.1:6379> DECR new
(integer) 103
127.0.0.1:6379> DECR new
(integer) 102
127.0.0.1:6379> DECR new
(integer) 101
127.0.0.1:6379> DECR new
(integer) 100
127.0.0.1:6379> get new
"100"
127.0.0.1:6379>
浮点:INCRBYELOAT

127.0.0.1:6379> INCRBYFLOAT key 0.1
"0.2"
127.0.0.1:6379> INCRBYFLOAT key 0.1
"0.3"
127.0.0.1:6379> INCRBYFLOAT key 0.1
"0.4"
127.0.0.1:6379> INCRBYFLOAT key 0.1
"0.5"
127.0.0.1:6379> INCRBYFLOAT key 0.1
"0.6"
127.0.0.1:6379> INCRBYFLOAT key 0.1
"0.7"
127.0.0.1:6379>
MSET使用:

127.0.0.1:6379> mset key 1 key2 2 key3 3
OK
127.0.0.1:6379> get key
"1"
127.0.0.1:6379> get key2
"2"
127.0.0.1:6379> get key3
"3"
127.0.0.1:6379> mget key key2 key3
1) "1"
2) "2"
3) "3"
127.0.0.1:6379>
根据UTF-8编码计算key值:

127.0.0.1:6379> set key "linuxEA博客"
OK
127.0.0.1:6379> get key
"linuxEAxe5x8dx9axe5xaexa2"
127.0.0.1:6379> strlen key
(integer) 13
127.0.0.1:6379>
===================散列类型=====================
散列类型:

HSET
127.0.0.1:6379> HSET xuhao name 1
(integer) 1
127.0.0.1:6379> HSET xuhao co red
(integer) 1
127.0.0.1:6379> HSET xuhao price  3
(integer) 1
HGETALL查看所有

127.0.0.1:6379> HGETALL xuhao
1) "name"
2) "1"
3) "co"
4) "red"
5) "price"
6) "3"
HGET单个查看

127.0.0.1:6379> HGET xuhao name
"1"
127.0.0.1:6379> HGET xuhao co
"red"
127.0.0.1:6379> HGET xuhao price
"3"
127.0.0.1:6379>

HMSET:
127.0.0.1:6379> HMSET pc name thinkpad co black price 3000
OK
127.0.0.1:6379> HGETALL pc
1) "name"
2) "thinkpad"
3) "co"
4) "black"
5) "price"
6) "3000"
127.0.0.1:6379> HSET pc mode1 "bu huan jia"
(integer) 1
127.0.0.1:6379> HGET pc mode1
"bu huan jia"
127.0.0.1:6379>
删除:

127.0.0.1:6379> HGET pc mode1
"bu huan jia"
127.0.0.1:6379> HDEL pc mode1
(integer) 1
127.0.0.1:6379> HGETALL mode1
(empty list or set)
判断是否还存在:

127.0.0.1:6379> HEXISTS mode1 "bu huan jia"
(integer) 0
127.0.0.1:6379>
判断key是否存在:

127.0.0.1:6379> EXISTS mode1
(integer) 0
127.0.0.1:6379>
===================列表类型=====================
列表类型:

[root@yum-down ~]# redis-cli
127.0.0.1:6379> del new
(integer) 1
127.0.0.1:6379> LPUSH new 1
(integer) 1
127.0.0.1:6379> LPUSH new 2
(integer) 2
127.0.0.1:6379> LPUSH new 3
(integer) 3
从右边插入

127.0.0.1:6379> RPUSH new 4
(integer) 4
127.0.0.1:6379> LLEN new
(integer) 4
127.0.0.1:6379>
从左边弹出一个:

127.0.0.1:6379> LPOP new
"1"
从右边弹出一个:

127.0.0.1:6379> RPOP new
"4"
127.0.0.1:6379>
弹出两个后查看只剩下2个字符

127.0.0.1:6379> LLEN new
(integer) 2
127.0.0.1:6379>
某一个范围:LRANGE
-1表示从右边的第一个元素

127.0.0.1:6379> LRANGE new 1 2
1) "2"
2) "3"
127.0.0.1:6379> LRANGE new 0 -1
-0则是从左边开始查看

127.0.0.1:6379> LINDEX new 0
"1"
127.0.0.1:6379>
只保留指定的条目:
如:只保留0-4的条目:

127.0.0.1:6379> LTRIM new 1 4
OK
查看

127.0.0.1:6379> LRANGE new 0 -1
1) "4"
2) "LLEN"
3) "new"
4) "LLEN"
只保留0-3的条目

127.0.0.1:6379> LTRIM new 1 3
OK
查看

127.0.0.1:6379> LRANGE new 0 -1
1) "LLEN"
2) "new"
3) "LLEN"
127.0.0.1:6379>
例:只看logs的100条数据:

127.0.0.1:6379> LPUSH logs newlog
(integer) 1
127.0.0.1:6379> LTRIM logs 0 99
OK
127.0.0.1:6379>
===================集合类型====================
集合类型:SADD

127.0.0.1:6379> SADD jihe a b c
(integer) 3
127.0.0.1:6379> SADD jihe2 e f g
(integer) 3
127.0.0.1:6379> SMEMBERS jihe
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> SMEMBERS jihe2
1) "e"
2) "g"
3) "f"
127.0.0.1:6379>
判断是否存在:SISMEMBER

127.0.0.1:6379> SISMEMBER jihe2 a
(integer) 0
127.0.0.1:6379> SISMEMBER jihe2 e
(integer) 1
127.0.0.1:6379>
判断差集:SDIFF

127.0.0.1:6379> SDIFF jihe jihe2
1) "c"
2) "a"
3) "b"
127.0.0.1:6379>
交集运算: SINTER

127.0.0.1:6379> SMEMBERS jihe2
1) "a"
2) "e"
3) "g"
4) "f"
127.0.0.1:6379> SMEMBERS jihe
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> SINTER jihe jihe2
1) "a"
127.0.0.1:6379>
集合:SUNION

127.0.0.1:6379> SUNION jihe jihe2
1) "e"
2) "a"
3) "b"
4) "f"
5) "g"
6) "c"
127.0.0.1:6379>
有序集合:

127.0.0.1:6379> ZADD jihea 80 a
(integer) 1
127.0.0.1:6379> ZADD jihea 81 b
(integer) 1
127.0.0.1:6379> ZADD jihea 82 c
(integer) 1
127.0.0.1:6379> ZADD jihea 83 d
(integer) 1
127.0.0.1:6379> ZADD jihea 84 e
(integer) 1
查看

127.0.0.1:6379> ZSCORE jihea a
"80"
127.0.0.1:6379> ZSCORE jihea b
"81"
127.0.0.1:6379> ZSCORE jihea c
"82"
127.0.0.1:6379> ZSCORE jihea d
"83"
127.0.0.1:6379>
查看所有:

127.0.0.1:6379> ZRANGE jihea 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379>

热门栏目