一、字符串类型 string
1. 存储: set key value
2. 获取: get key
3. 删除: del key
127.0.0.1:6379> set username lisi
OK
127.0.0.1:6379> get username
"lisi"
127.0.0.1:6379> del username
(integer) 1
127.0.0.1:6379> get username
(nil)
二、哈希类型 hash
1. 存储: hset key field value
127.0.0.1:6379> hset myhash username lisi
(integer) 1
127.0.0.1:6379> hset myhash password 123
(integer) 1
2. 获取:
* hget key field: 获取指定的field对应的值
* hgetall key:获取所有的field和value
3. 删除: hdel key field
127.0.0.1:6379> hset myhash username zhansan
(integer) 1
127.0.0.1:6379> hset myhash age 35
(integer) 1
127.0.0.1:6379> hset myhash gender true
(integer) 1
127.0.0.1:6379> hget myhash username
"zhansan"
127.0.0.1:6379> hgetall myhash
1) "username"
2) "zhansan"
3) "age"
4) "35"
5) "gender"
6) "true"
127.0.0.1:6379> hdel myhash username
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "age"
2) "35"
3) "gender"
4) "true"
>
三、 列表类型 list:可以添加一个元素到列表的头部(左边)或者尾部(右边)
1. 添加:
1. lpush key value: 将元素加入列表左表
2. rpush key value:将元素加入列表右边
2. 获取:
* lrange key start end :范围获取 。如果要查询全部, start设置为0,end设置为-1
3. 删除:
* lpop key: 删除列表最左边的元素,并将元素返回
* rpop key: 删除列表最右边的元素,并将元素返回
127.0.0.1:6379> lpush myList a
(integer) 1
127.0.0.1:6379> lpush myList b
(integer) 2
127.0.0.1:6379> rpush myList b
(integer) 3
127.0.0.1:6379> lrange myList 0 -1
1) "b"
2) "a"
3) "b"
127.0.0.1:6379> rpop myList
"b"
127.0.0.1:6379> rpush myList c
(integer) 3
127.0.0.1:6379> lrange myList 0 -1
1) "b"
2) "a"
3) "c"
127.0.0.1:6379> lpop myList
"b"
127.0.0.1:6379> lrange myList 0 -1
1) "a"
2) "c"
127.0.0.1:6379> rpop myList
"c"
四、 集合类型 set : 不允许重复元素
1. 存储:sadd key value
2. 获取:smembers key:获取set集合中所有元素
3. 删除:srem key value:删除set集合中的某个元素
127.0.0.1:6379> sadd mySet a
(integer) 1
127.0.0.1:6379> sadd mySet b c d e
(integer) 4
127.0.0.1:6379> smembers mySet
1) "c"
2) "b"
3) "d"
4) "a"
5) "e"
127.0.0.1:6379> srem mySet c
(integer) 1
127.0.0.1:6379> smembers mySet
1) "d"
2) "a"
3) "b"
4) "e"
五. 有序集合类型 sortedset:不允许重复元素,且元素有顺序.每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。
1. 存储:zadd key score value
2. 获取:zrange key start end [withscores] ---》 [withscores] 如果写上会把分数score也列出来
3. 删除:zrem key value
127.0.0.1:6379> zadd mySortset 60 zhangshan
(integer) 1
127.0.0.1:6379> zadd mySortset 59.5 lisi
(integer) 1
127.0.0.1:6379> zadd mySortset 62 wangwu
(integer) 1
127.0.0.1:6379> zadd mySortset 61 zhaoliu
(integer) 1
127.0.0.1:6379> zrange mySortset 0 -1
1) "lisi"
2) "zhangshan"
3) "zhaoliu"
4) "wangwu"
127.0.0.1:6379> zrange mySortset 0 -1 withscores
1) "lisi"
2) "59.5"
3) "zhangshan"
4) "60"
5) "zhaoliu"
6) "61"
7) "wangwu"
8) "62"
127.0.0.1:6379> zrem mySortset lisi
(integer) 1
127.0.0.1:6379> zrange mySortset 0 -1 withscores
1) "zhangshan"
2) "60"
3) "zhaoliu"
4) "61"
5) "wangwu"
6) "62"
六. 通用命令
1. keys * : 查询所有的键。(这里的*可以写正则表达式)
2. type key : 获取键对应的value的类型
3. del key:删除指定的key value
127.0.0.1:6379> keys *
1) "mySortset"
2) "myhash"
3) "mySet"
127.0.0.1:6379> type mySortset
zset
127.0.0.1:6379> del mySortset
(integer) 1
127.0.0.1:6379> keys *
1) "myhash"
2) "mySet"
127.0.0.1:6379>