9> KEYS ?*
1) "hello1"
2) "hello"
3) "hello2"
?
KEY匹配方式:
?匹配单个
?*匹配所有
?
3、判断key是否存在
判断Key是否存在使用:EXISTS ? 他返回的是整形:0不存在,1存在
192.168.0.201:6379> EXISTS hello
(integer) 1
192.168.0.201:6379> EXISTS hehe
(integer) 0
?
4、删除KEY
192.168.0.201:6379> DEL hello
(integer) 1 ? #这里的1是数量
删除多个测试下:
192.168.0.201:6379> DEL hello1 hello2
(integer) 2
?
5、查看类型TYPE
只要用set类型就是字符串。查看类型命令用TYPE
192.168.0.201:6379> TYPE hello
string
?
6、Keyspace
redis是支持多个实例的默认最多16个,可以修改配置文件来支持更多!
使用INFO命令查看!
# Keyspace
db0:keys=1,expires=0,avg_ttl=0
?
db0 :这个可以理解为命名空间。最多支持16个,使用SELECT 去切换
192.168.0.201:6379> SELECT 1
OK
尝试添加一个key-value
SET db1 hehe
然后在使用INFO看下
# Keyspace
db0:keys=1,expires=0,avg_ttl=0
db1:keys=1,expires=0,avg_ttl=0
?Redis数据类型:
?
他用不同的命令来区分你要操作什么数据类型
类型不能嵌套,不能混! 但是有个王炸:set能把所有的类型都改为字符串类型!
?
1、字符串类型:
?
APPEND ?在值的后面追加
set能重新设置但是要追加的话使用APPEND最好比如
192.168.0.201:6379> SET hehe hello
OK
192.168.0.201:6379> GET hehe
"hello"
192.168.0.201:6379> APPEND hehe ,world
(integer) 11
192.168.0.201:6379> GET hehe
"hello,world"
?
可以同时设置多个值和查询值用MSET 和MSET
192.168.0.201:6379> MSET key1 v1 key2 v2 key3 v3
OK
192.168.0.201:6379> MGET key1 key2 key3
1) "v1"
2) "v2"
3) "v3"
?
获取字符串长度
192.168.0.201:6379> STRLEN hehe
(integer) 11
如果字符串是中文的他会按照UTF-8格式的来输出1个字等3个字符串来算的 ?)
192.168.0.201:6379> SET key "呵呵"
OK
192.168.0.201:6379> GET key
"\xe5\x91\xb5\xe5\x91\xb5"
?
2、自增类型
?
比如说投票点下+1 ,如果说用set每点一次修改set下那就不太现实。所有redis有个自增类型:INCR
192.168.0.201:6379> INCR num ? ? ? ? ? #默认如果没有这个值的话,INCR就会自动创建一个值默认为零,当你没执行一次他就会+1
(integer) 1
192.168.0.201:6379> INCR num
(integer) 2
192.168.0.201:6379> INCR num
(integer) 3
192.168.0.201:6379> INCR num
(integer) 4
?
?
如果想加多个呢:INCRBY
192.168.0.201:6379> INCRBY num 10
(integer) 57
192.168.0.201:6379> INCRBY num 10
(integer) 67
192.168.0.201:6379> INCRBY num 10
(integer) 77
?
减呢? DECR
192.168.0.201:6379> DECR num
(integer) 106
192.168.0.201:6379> DECR num
(integer) 105
192.168.0.201:6379> DECR num
(integer) 104
?
如果要是减多个呢:DECRBY
192.168.0.201:6379> DECRBY num 5
(integer) 97
192.168.0.201:6379> DECRBY num 5
(integer) 92
192.168.0.201:6379> DECRBY num 5
(integer) 87
?
想支持小数点:
INCRBYFLOAT key 0.1
192.168.0.201:6379> INCRBYFLOAT key 0.1
"0.1"
192.168.0.201:6379> INCRBYFLOAT key 0.1
"0.2"
192.168.0.201:6379> INCRBYFLOAT key 0.1
"0.3"
192.168.0.201:6379> INCRBYFLOAT key 0.1
"0.4"
?
3、散列类型(hash)
?
和
数据库存的表似的,表不是的有字段吧,可以给每个字段设置个值
HSET Key field value
HGET Key field
HMSET Key field value [field value....]
HMGET Key field [field ...]
HGETALL Key
HDEL
?
192.168.0.201:6379> HSET shouji name iphone
(integer) 1
192.168.0.201:6379> HSET shouji co red
(integer) 1
192.168.0.201:6379> HSET shouji price 8888
(integer) 1
?
192.168.0.201:6379> HGET shouji name
"iphone"
192.168.0.201:6379> HGET shouji co
"red"
192.168.0.201:6379> HGET shouji price
"8888"
192.168.0.201:6379> HGETALL shouji
1) "name"
2) "iphone"
3) "co"
4) "red"
5) "price"
6) "8888"
?
其实现在看着不是好看的但是他通过一些API调用