Redis 基础概念和命令
Redis 是什么
Redis是一种基于键值对(key-value)的NoSQL数据库。
为什么使用Redis
速度快
Redis的时间颗粒度一般是微秒,慢查询的默认值是10 000微秒,即10毫秒。
简单
- API
- 协议:RESP(REdis Serialization Protocol)
- 源码:5万
怎么使用Redis
命令总览
redis | mysql | 作用 |
---|---|---|
redis-cli | mysql | 自带客户端 |
set | insert ... on duplicate key update | 新增或者修改 |
get | select | 查询 |
del | delete from | 删除 |
client list | show processlist | 查看连接情况 |
keys pattern | like | 查询对应模式的key列表 |
info replication | - | 是否Master节点 |
config get requirepass | - | 查看密码 |
expire key seconds | - | 设置key的有效期 |
ttl key | - | 查看key的有效期 |
exists key | - | 判断key是否存在 |
命令讲解
redis-cli
连接指定IP,指定端口的redis服务器
redis-cli -h {host} -p {port} -a {pass}
$./redis-cli -h 10.202.24.171 -p 8080
10.202.24.171:8080> PING
(error) NOAUTH Authentication required.
10.202.24.171:8080> AUTH admin.123
OK
10.202.24.171:8080>
./redis-cli -h 10.202.101.4 -p 6379 -a admin.123
key的增删改查
set key value
get key
del key
10.202.24.171:8080> set user_449631 lihairong
OK
10.202.24.171:8080> get user_449631
"lihairong"
10.202.24.171:8080> exists user_449631
(integer) 1
10.202.24.171:8080> del user_449631
(integer) 1
10.202.24.171:8080> get user_449631
(nil)
10.202.24.171:8080> exists user_449631
(integer) 0
10.202.24.171:8080>
查询是否master节点
10.202.24.171:8080> info Replication
# Replication
role:master
connected_slaves:0
master_repl_offset:11545465
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:10496890
repl_backlog_histlen:1048576
10.2.xxx.xx:8080> info Replication
# Replication
role:slave
master_host:10.2.231.94
master_port:8080
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:11661520052
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
查看pass
连接并认证可通过redis命令获取
127.0.0.1:6379> config get requirepass
查看服务器的配置文件,一般存放在conf文件夹
[appdeploy@cnsz22vl1101:/app/redis/conf]$cat SFECP_CNSZ22_REDIS_CACHE_SVR_01.conf | grep requirepass
requirepass "admin.123"
client list
10.202.24.171:8080> client list
id=8 addr=10.202.24.171:39978 fd=11 name=sentinel-ca6ff759-cmd age=11843771 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=publish
id=10 addr=10.202.24.171:30351 fd=13 name=sentinel-2aef8d79-cmd age=11843761 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=info
id=30711 addr=10.202.24.170:56322 fd=5 name=sentinel-0a0deac6-cmd age=7307073 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=publish
id=42956 addr=10.202.24.171:17510 fd=10 name=sentinel-2aef8d79-pubsub age=6629498 idle=0 flags=N db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
id=42958 addr=10.202.24.171:59685 fd=14 name=sentinel-ca6ff759-pubsub age=6629498 idle=0 flags=N db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
id=54850 addr=10.202.24.170:27905 fd=6 name=sentinel-0a0deac6-pubsub age=6109432 idle=0 flags=N db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
id=161052 addr=10.202.91.7:37620 fd=7 name= age=8965 idle=3 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping
id=161055 addr=10.202.91.14:27102 fd=9 name= age=8890 idle=9 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping
id=161056 addr=10.202.91.13:35310 fd=12 name= age=8889 idle=8 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping
id=161147 addr=10.202.91.1:56217 fd=15 name= age=202 idle=5 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping
id=161151 addr=10.202.91.1:56817 fd=8 name= age=5 idle=5 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping
id=161152 addr=10.202.24.13:29523 fd=16 name= age=4 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
key的有效期
ttl key
10.202.24.171:8080> keys service*
1) "serviceArea_20170411"
2) "serviceArea_serviceArea"
3) "serviceArea_20170515"
10.202.24.171:8080> ttl serviceArea_20170515
(integer) 41559
10.202.24.171:8080> quit
[appdeploy@cnsz22vl1101:/app/redis/conf]$bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
41559 / 60 /60
11
^C
(interrupt) Exiting bc.
[appdeploy@cnsz22vl1101:/app/redis/conf]$date
Mon May 15 12:25:34 CST 2017
info
配置很多,请参看扩展阅读。
缓存更新的套路
- Cache Aside Pattern
+--------------------------------------------------+
| |
| Web Server |
| |
+------------------^^--------------------^^--------+
|| || ||
hit || || ||
|| || return ||
|| || ||
+-------vv---------------------+ ||
| | ||
| Cache(Redis) | || return
| | ||
+------------------^^----------+ ||
|| || ||
miss|| || ||
|| || write cache ||
|| || ||
+-------vv------------------------------------------+
| |
| DB (MYSQL) |
| |
+---------------------------------------------------+
-
Read/Write Through Pattern
-
Write Behind Caching Pattern