1. 类型 string
**存储**:set key value
127.0.0.1:6379> set username lihua
**获取**:get key
127.0.0.1:6379> get username
"lihua"
**删除**:del key
127.0.0.1:6379> del age
(integer) 1
2. 哈希类型 hash
**存储**: hset key filed value
127.0.0.1:6379> hset myhash username wangwu
(integer) 1
127.0.0.1:6379> hset myhash password 456
(integer) 1
**获取**:
hget key field(获取指定的field对应的值)
127.0.0.1:6379> hget myhash username
"wangwu"
hgetall key(获取所有的key和value)
127.0.0.1:6379> hgetall myhash
-
"username"
-
"wangwu"
-
"password"
-
"456"
**删除**: hdel key field
127.0.0.1:6379> hdel myhash username
(integer) 1
3. 列表类型 list
**添加**:
-
lpush key value: 将元素加入列表左表
-
rpush key value:将元素加入列表右边
127.0.0.1:6379> lpush myList x
(integer) 1
127.0.0.1:6379> lpush myList y
(integer) 2
127.0.0.1:6379> rpush myList z
(integer) 3
**获取**: lrange key stard end(范围获取)
127.0.0.1:6379> lrange myList 0 -1
-
"y"
-
"x"
-
"z"
**删除**:
lpop key: 删除列表最左边的元素,并将元素返回
rpop key: 删除列表最右边的元素,并将元素返回
127.0.0.1:6379> lpop myList
"y"
127.0.0.1:6379> rpop myList
"z"
4. 集合类型 set:不允许重复元素
**存储**: sadd key value
127.0.0.1:6379> sadd myset x
(integer) 1
127.0.0.1:6379> sadd myset x
(integer) 0
**获取**: smenbers key(获取set集合中的所有元素)
127.0.0.1:6379> smembers myset
- "x"
**删除**: srem key value(删除set集合中的某个元素)
127.0.0.1:6379> srem myset x
(integer) 1
5. 有序集合类型 sortedset:
不允许重复元素,且元素有顺序.每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。
**存储**: zadd mysort score value
127.0.0.1:6379> zadd myscores 65 lihua
(integer) 1
127.0.0.1:6379> zadd myscores 70 wangwu
(integer) 1
127.0.0.1:6379> zadd myscores 85 zhangsan
(integer) 1
```
**获取**: zrange key start end [withscores]
127.0.0.1:6379> zrange myscores 0 -1 withscores
-
"lihua"
-
"65"
-
"wangwu"
-
"70"
-
"zhangsan"
-
"85"
**删除**: zrem key value
127.0.0.1:6379> zrem myscores wangwu
(integer) 1
通用命令
**查询所有的键**:
127.0.0.1:6379> keys *
**获取键对应的value的类型**:
127.0.0.1:6379> type myhash
**删除指定的key value**:
127.0.0.1:6379> del myhash
(integer) 1