redis可作非关系型数据库(如新浪就是用mysql存内容,用redis存一些粉丝,排行榜之类的关系),缓存,和消息中间件(作为客户端和elasticsearch的中间件,将客户端的日志先放在redis上,再由redis送给elasticsearch,解决elasticsearch坏掉的问题)
redis 相关设置,redis命令可以用tab键补全
[root@elk_node1 ~]# redis-cli -h 192.168.122.188
192.168.122.188:6379> set mykey abc //定义一个键值对
OK
192.168.122.188:6379> get mykey //获取键值对
"abc"
[root@elk_node1 ~]# redis-cli -h 192.168.122.188 -n 7 // -n指定选取哪个数据库,默认命名类似db6
192.168.122.188:6379[7]> set mykey1 aaa
OK
192.168.122.188:6379[7]> get mykey1
"aaa"
192.168.122.188:6379[7]> select 6 //选择db6数据库
OK
192.168.122.188:6379[6]> help set //redis中可以help一个命令获得一些命令信息
SET key value [EX seconds] [PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
group: string
192.168.122.188:6379[6]> set msg goodbye nx //nx表示当不为空的时候才存入这个键值
OK
192.168.122.188:6379[6]> set msg godfd nx
(nil)
192.168.122.188:6379[6]> get msg
"goodbye"
192.168.122.188:6379[6]> set msg1 huahua ex 10 //ex表示有效期,这个表示10秒后,这个值自动过期
OK
192.168.122.188:6379[6]> get msg1
"huahua"
192.168.122.188:6379[6]> get msg1
"huahua"
192.168.122.188:6379[6]> get msg1
"huahua"
192.168.122.188:6379[6]> get msg1 //10秒后已经为空
(nil)
192.168.122.188:6379[6]> mset a 1 b 2 c 3 //mset 可以同时设置多个键值对
OK
192.168.122.188:6379[6]> mget a b c
1) "1"
2) "2"
3) "3"
192.168.122.188:6379[6]> keys * //显示所有的键值对
1) "c"
2) "b"
3) "a"
4) "msg"
192.168.122.188:6379[6]> MSETNX d 4 a 5
(integer) 0
这是因为//msetnx 当key不存在设置多个值,它具有原子性,因为a这个key被设置过,只要有一个有值就不会成功,返回0
192.168.122.188:6379[6]> set myhobby spo
OK
192.168.122.188:6379[6]> APPEND myhobby rt //append是将两个值连接在一起
(integer) 5
192.168.122.188:6379[6]> get myhobby
"sport"
192.168.122.188:6379[6]> STRLEN myhobby //查看key的值的长度
(integer) 5
192.168.122.188:6379[6]> set msg hello
OK
192.168.122.188:6379[6]> get msg
"hello"
192.168.122.188:6379[6]> SETRANGE msg 2 apply //setrange表示从之前哪个位置更改值,这个表示从第2个字符开始替换。
(integer) 7
192.168.122.188:6379[6]> get msg
"heapply"
192.168.122.188:6379[6]> set msg "hello world"
OK
192.168.122.188:6379[6]> get msg
"hello world"
192.168.122.188:6379[6]> GETRANGE msg 0 4 //getrange获取一定范围内的值,获取0-4范围内的值
"hello"
192.168.122.188:6379[6]> GETRANGE msg -5 -1 -5---1 倒着就是从-1往后取值
"world"