五大基础数据类型
String(字符串)
127.0.0.1:6379> set k1 v1 #设置k-v键值对
OK
127.0.0.1:6379> append k1 "hello world" #追加“helloworld”到k1对应的v1后面(若key不存在,就相当于set命令)
(integer) 13
127.0.0.1:6379> get k1
"v1hello world"
127.0.0.1:6379> strlen k1 #过得k1对应的v1的长度
(integer) 13
##############################################
127.0.0.1:6379>set views 0
OK
127.0.0.1:6379> incr views #自增1
(integer) 1
127.0.0.1:6379> decr views #自减1
(integer) 0
127.0.0.1:6379> incrby views 10 #自增10
(integer) 10
127.0.0.1:6379> decrby views 10 #自减10
(integer) 0
#字符串范围
127.0.0.1:6379> set k1 "hello,world"
OK
127.0.0.1:6379> getrange k1 0 4 #截取0-4的字符
"hello"
127.0.0.1:6379> getrange k1 0 -1 #获取所有字符
"hello,world"
127.0.0.1:6379>
#字符串替换
127.0.0.1:6379> set k2 abc
OK
127.0.0.1:6379> setrange k2 2 hh #替换索引为2的字符
(integer) 4
127.0.0.1:6379> get k2
"abhh"
127.0.0.1:6379> setex k3 30 hello #设置k3的值为hello,30秒后过期
127.0.0.1:6379> setnx mykey "yes" #若mykey不存在,则创建mykey 值为yes;若存在,则创建失败
(integer) 1
127.0.0.1:6379> keys *
1) "mykey"
2) "views"
3) "k2"
4) "k1"
127.0.0.1:6379> setnx mykey "no"
(integer) 0
127.0.0.1:6379> get mykey
"yes"
#批量处理
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
127.0.0.1:6379> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> msetnx k1 v1 k4 v4 #是一个原子性的操作,要么都成功,要么都失败
(integer) 0
127.0.0.1:6379>
#对象
127.0.0.1:6379> set user:1 {name:jyh,age:21}
OK
127.0.0.1:6379> get user:1
"{name:jyh,age:21}"
127.0.0.1:6379> mset user:2:name zhangsan user:2:age 30
OK
127.0.0.1:6379> mget user:2:name user:2:age
1) "zhangsan"
2) "30"
#getset key value(若不存在key,则设置值,否则,保持值)
127.0.0.1:6379> getset k1 yes
(nil)
127.0.0.1:6379> get k1
"yes"
127.0.0.1:6379> getset k1 no
"yes" #get的值
127.0.0.1:6379> get k1
"no" #set后的值
List(列表)
#将一个值或者多个值,插入到列表头部(左)
127.0.0.1:6379> lpush list one
(integer) 1
127.0.0.1:6379> lpush list two
(integer) 2
127.0.0.1:6379> lpush list three
(integer) 3
127.0.0.1:6379> keys *
1) "list"
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> lpop list #移除第一个元素
#将一个值或者多个值,插入到列表尾部(右)
127.0.0.1:6379> rpush list one
(integer) 1
127.0.0.1:6379> rpush list two
(integer) 2
127.0.0.1:6379> rpush list three
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> rpop list #移除最后一个元素
"three"
#通过下标获取值
127.0.0.1:6379> lindex list 0
"one"
#获得列表长度
127.0.0.1:6379> llen list
(integer) 2
#移除某个值
lrem list 1 one #移除一个list中的one
#截取指定长度
127.0.0.1:6379> rpush mylist one
(integer) 1
127.0.0.1:6379> rpush mylist two
(integer) 2
127.0.0.1:6379> rpush mylist three
(integer) 3
127.0.0.1:6379> rpush mylist four
(integer) 4
127.0.0.1:6379> ltrim mylist 1 2
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "two"
2) "three"
#将列表元素移到新的列表中
rpoplpush
127.0.0.1:6379> lrange mylist 0 -1
1) "two"
2) "three"
127.0.0.1:6379> rpoplpush mylist list
"three"
127.0.0.1:6379> rpoplpush mylist list
"two"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "three"
#lset只能更新不能添加
127.0.0.1:6379> lset k1 v1
(error) ERR wrong number of arguments for 'lset' command
127.0.0.1:6379> lpush k1 v1
(integer) 1
127.0.0.1:6379> lset k1 0 value
OK
127.0.0.1:6379> lrange k1 0 -1
1) "value"
#插入
127.0.0.1:6379> rpush list hello
(integer) 1
127.0.0.1:6379> rpush list redis
(integer) 2
127.0.0.1:6379> linsert list before redis ya
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "hello"
2) "ya"
3) "redis"
Hash
相当于key-map(key-value)
127.0.0.1:6379> hset myhash field1 value1#设置一个key-value
(integer) 1
127.0.0.1:6379> hget myhash field1
"value1"
127.0.0.1:6379> hmset myhash field1 hello field2 world #设置多个key-value
OK
127.0.0.1:6379> hmget myhash field1 field2
1) "hello"
2) "world"
#获取所有key-value
127.0.0.1:6379> hgetall myhash
1) "field1"
2) "hello"
3) "field2"
4) "world"
#删除指定hash的key
127.0.0.1:6379> hdel myhash field1
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "field2"
2) "world"
#获取hash长度
127.0.0.1:6379> hlen myhash
(integer) 1
#判断是否存在
127.0.0.1:6379> hexists myhash field2
(integer) 1
#获取所有字段
127.0.0.1:6379> hkeys myhash
1) "field2"
#获取所有值
127.0.0.1:6379> hvals myhash
1) "world"
#自增(无自减操作)
127.0.0.1:6379> hset myhash field1 0
(integer) 1
127.0.0.1:6379> hincrby myhash field 2
(integer) 2
127.0.0.1:6379> hget myhash field
"2"
#set if not exists
127.0.0.1:6379> hsetnx myhash field1 yes
(integer) 0
127.0.0.1:6379> hsetnx myhash field2 yes
(integer) 1
hash适合存储对象
Set(集合)
无序不重复集合
#添加元素
127.0.0.1:6379> sadd myset 1 2 3
(integer) 3
#查看元素
127.0.0.1:6379> smembers myset
1) "1"
2) "2"
3) "3"
# 判断元素是否存在
127.0.0.1:6379> sismember myset 2
(integer) 1
127.0.0.1:6379> sismember myset 4
(integer) 0
#获取set集合中的元素个数
127.0.0.1:6379> scard myset
(integer) 3
#移除元素
127.0.0.1:6379> srem myset 1
(integer) 1
127.0.0.1:6379> smembers myset
1) "2"
2) "3"
#随机抽取元素 srandmember key count
127.0.0.1:6379> sadd myset zhangsan lisi wangwu
(integer) 3
127.0.0.1:6379> srandmember myset 1
1) "zhangsan"
127.0.0.1:6379> srandmember myset 1
1) "wangwu"
#随机删除元素 spop key
127.0.0.1:6379> spop myset
"lisi"
127.0.0.1:6379> smembers myset
1) "zhangsan"
2) "wangwu"
127.0.0.1:6379> spop myset
"wangwu"
127.0.0.1:6379> smembers myset
1) "zhangsan"
#移除集合指定元素到新的集合
127.0.0.1:6379> sadd myset hello
(integer) 1
127.0.0.1:6379> sadd myset world
(integer) 1
127.0.0.1:6379> sadd myset redis
(integer) 1
127.0.0.1:6379> smove myset set2 redis
(integer) 1
127.0.0.1:6379> smembers myset
1) "hello"
2) "world"
127.0.0.1:6379> smembers set2
1) "redis"
#有关数学的集合
127.0.0.1:6379> sadd set1 a b c
(integer) 3
127.0.0.1:6379> sadd set2 c d e
(integer) 3
127.0.0.1:6379> sdiff set1 set2 #差集
1) "a"
2) "b"
127.0.0.1:6379> sinter set1 set2 #交集
1) "c"
127.0.0.1:6379> sunion set1 set2 #并集
1) "a"
2) "c"
3) "b"
4) "d"
5) "e"
Zset(有序集合)
相当于在key-number-value
127.0.0.1:6379> zadd myzset 1 one
(integer) 1
127.0.0.1:6379> zadd myzset 2 two
(integer) 1
127.0.0.1:6379> zrange myzset 0 -1
1) "one"
2) "two"
127.0.0.1:6379> zadd salary 2000 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 5000 lisi
(integer) 1
127.0.0.1:6379> zadd salary 3500 wangwu
(integer) 1
#从大到小排序
127.0.0.1:6379> zrangebyscore salary -inf +inf
1) "zhangsan"
2) "wangwu"
3) "lisi"
127.0.0.1:6379> zrangebyscore salary -inf +inf withscores
1) "zhangsan"
2) "2000"
3) "wangwu"
4) "3500"
5) "lisi"
6) "5000"
#从大到小
127.0.0.1:6379> zrevrange salary 0 -1
1) "lisi"
2) "wangwu"
3) "zhangsan"
127.0.0.1:6379> zrevrange salary 0 -1 withscores
1) "lisi"
2) "5000"
3) "wangwu"
4) "3500"
5) "zhangsan"
6) "2000"
#移除元素
127.0.0.1:6379> zrem salary zhangsan
#获得集合大小
127.0.0.1:6379> zcard salary
(integer) 2
#获取指定区间的元素个数
127.0.0.1:6379> zcount myzset 1 2
(integer) 2