Redis|Redis 基础概念和命令
Redis 是什么
Redis是一种基于键值对(key-value)的NoSQL数据库。
为什么使用Redis 速度快 Redis的时间颗粒度一般是微秒,慢查询的默认值是10 000微秒,即10毫秒。
简单
- API
- 协议:RESP(REdis Serialization Protocol)
- 源码:5万
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是否存在 |
连接指定IP,指定端口的redis服务器
redis-cli -h {host} -p {port} -a {pass}
$./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> PING
(error) NOAUTH Authentication required.
127.0.0.1:6379> AUTH admin.123
OK
127.0.0.1:6379>
./redis-cli -h 127.0.0.1 -p 6379 -a admin.123
key的增删改查
set key value
get key
del key
127.0.0.1:6379> set user_000001 lihairong
OK
127.0.0.1:6379> get user_000001
"lihairong"
127.0.0.1:6379> exists user_000001
(integer) 1
127.0.0.1:6379> del user_000001
(integer) 1
127.0.0.1:6379> get user_000001
(nil)
127.0.0.1:6379> exists user_000001
(integer) 0
127.0.0.1:6379>
查询是否master节点
127.0.0.1:6379> 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
127.0.0.1:6379> info Replication
# Replication
role:slave
master_host:127.0.0.x
master_port:6379
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 REDIS_CONF_SVR_01.conf | grep requirepass
requirepass "admin.123"
client list
127.0.0.1:6379> client list
id=8 addr=127.0.0.1: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=127.0.0.1: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.x.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=127.0.0.1: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=127.0.0.1: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.x.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.x.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.x.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.x.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.x.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.x.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.x.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
127.0.0.1:6379> keys service*
1) "serviceArea_20170411"
2) "serviceArea_serviceArea"
3) "serviceArea_20170515"
127.0.0.1:6379> ttl serviceArea_20170515
(integer) 41559
127.0.0.1:6379> 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
- 【Redis|Redis 基础概念和命令】Write Behind Caching Pattern
Redis 实战
缓存更新的套路
推荐阅读
- 网络编程基本概念:|网络编程基本概念: UDP / TCP
- Docker镜像管理基础
- Python-基础学习-第二轮
- Java 继承
- Vim基础用法,最常用、最实用的命令介绍(保姆级教程)
- java|零基础微信完整版小程序开发之微信表情包小程序前端+后台源码(java)
- Docker精简学习系列(一)(安装、常用命令和基本概念理解)
- web前端面试宝典-基础篇(二)
- 怪物概念设计师|怪物概念设计师 KOUJI TAJIMA | 不是幸运,而是足够努力
- Android中的线程相关