Redis简单的数据操作(增删改查):
字符串类型 string
1. 存储: set key value
127.0.0.1:6379> set username zhangsan
OK
2. 获取: get key
127.0.0.1:6379> get username
"zhangsan"
以下同理:
127.0.0.1:6379> set age 23
OK
127.0.0.1:6379> get age
"23"
127.0.0.1:6379> set age 33
OK
127.0.0.1:6379> get age
"33"
3. 删除: del key
127.0.0.1:6379> del age
(integer) 1
127.0.0.1:6379> get age
(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. 获取:
127.0.0.1:6379> hget myhash username
"lisi"
127.0.0.1:6379> hgetall myhash
1) "username"
2) "lisi"
3) "password"
4) "123"
3. 删除: hdel key field
127.0.0.1:6379> hdel myhash username
(integer) 1
127.0.0.1:6379> hget myhash username
(nil)
列表类型 list:
可以添加一个元素到列表的头部(左边)或者尾部(右边)
底层实现原理自我判断为使用栈的数据结构进行构建。
1. 添加:
1. 左插入
127.0.0.1:6379> lpush mylist a
(integer) 1
127.0.0.1:6379> lpush mylist b
(integer) 2
2. 右插入
127.0.0.1:6379> rpush mylist c
(integer) 3
2. 获取:
127.0.0.1:6379> lrange mylist 0 -1
1) "b"
2) "a"
3) "c"
3. 删除(删除后会有返回值):
1. 从左侧删除
127.0.0.1:6379> lpop mylist
"b"
127.0.0.1:6379> lrange mylist 0 -1
1) "a"
2) "c"
2. 从右侧删除
127.0.0.1:6379> rpop mylist
"c"
127.0.0.1:6379> lrange mylist 0 -1
1) "a"
4. 可以插入重复值
127.0.0.1:6379> lpush mylist a
(integer) 2
127.0.0.1:6379> lrange mylist 0 -1
1) "a"
2) "a"
del mylist 删除该键的所有信息
集合类型 set
不允许重复元素
1. 存储:sadd key value
127.0.0.1:6379> sadd myset a
(integer) 1
127.0.0.1:6379> sadd myset a
(integer) 0
第二行存入又一个a,但是改变行为0,代表没有插入成功
2. 获取:smembers key:获取set集合中所有元素
127.0.0.1:6379> smembers myset
1) "a"
一次性插入多条数据
127.0.0.1:6379> sadd myset b c d
(integer) 3
127.0.0.1:6379> smembers myset
1) "d"
2) "b"
3) "a"
4) "c"
3. 删除:srem key value:删除set集合中的某个元素
删除的同时会有返回值
127.0.0.1:6379> srem myset a
(integer) 1
127.0.0.1:6379> smembers myset
1) "d"
2) "b"
3) "c"
有序集合类型 sortedset:
不允许重复元素,且元素有顺序.每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。
1. 存储:zadd key score value
127.0.0.1:6379> zadd mysort 60 zhangsan
(integer) 1
127.0.0.1:6379> zadd mysort 50 zan
(integer) 1
127.0.0.1:6379> zadd mysort 80 wangwuan
(integer) 1
2. 获取:zrange key start end [withscores]
127.0.0.1:6379> zrange mysort
(error) ERR wrong number of arguments for 'zrange' command
127.0.0.1:6379> zrange mysort 0 -1
1) "zan"
2) "zhangsan"
3) "wangwuan"
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "zan"
2) "50"
3) "zhangsan"
4) "60"
5) "wangwuan"
6) "80"
覆盖数据后重新排序:
127.0.0.1:6379> zadd mysort 509 zan
(integer) 0
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "zhangsan"
2) "60"
3) "wangwuan"
4) "80"
5) "zan"
6) "509"
3. 删除:zrem key value
127.0.0.1:6379> zrem mysort zan
(integer) 1
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "zhangsan"
2) "60"
3) "wangwuan"
4) "80"
通用命令
1. keys * : 查询所有的键
2. type key : 获取键对应的value的类型
3. del key:删除指定的key value
127.0.0.1:6379> keys *
1) "mysort"
2) "myhash"
3) "mylist"
4) "username"
5) "myset"
127.0.0.1:6379> get username
"zhangsan"
127.0.0.1:6379> type username
string
127.0.0.1:6379> type mylist
list
127.0.0.1:6379>
redis报错信息:
[6644] 02 Apr 23:11:58.976 # Creating Server TCP listening socket *:6379: bind: No such file or directory
出现这类语句的时候,一般是出现了没有关闭redis的服务器或者客户端造成的,故解决方法一般为将所有redis的服务器和客户端都给关闭了,然后重新打开即可