redis学习 - 猿码设计师

redis学习 - 猿码设计师

 

https://redis.io/

1. 安装redis

> wget http://download.redis.io/releases/redis-5.0.5.tar.gz
> tar -xzvf
redis-5.0.5.tar.gz
> cd redis-5.0.5
> make
> make test

2. 运行redis-server

> cd src && ./redis-server

22501:C 05 Aug 2019 13:37:12.267 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
22501:C 05 Aug 2019 13:37:12.267 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=22501, just started
22501:C 05 Aug 2019 13:37:12.267 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
22501:M 05 Aug 2019 13:37:12.268 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 22501
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

22501:M 05 Aug 2019 13:37:12.270 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
22501:M 05 Aug 2019 13:37:12.271 # Server initialized
22501:M 05 Aug 2019 13:37:12.271 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
22501:M 05 Aug 2019 13:37:12.271 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
22501:M 05 Aug 2019 13:37:12.271 * Ready to accept connections

3. 测试客户端程序redis-cli

> cd src && ./redis-cli -h host -p port -a password --raw
127.0.0.1:6379> set mykey myvalue
OK
127.0.0.1:6379> get mykey
"myvalue"
127.0.0.1:6379> 

--raw 避免中文乱码

4. Redis CONFIG命令格式

127.0.0.1:6379> CONFIG GET CONFIG_SETTING_NAME
127.0.0.1:6379> config get loglevel
1) "loglevel"
2) "notice"
127.0.0.1:6379> config get *
  1) "dbfilename"
  2) "dump.rdb"
  3) "requirepass"
  4) ""
  5) "masterauth"
  6) ""
  7) "cluster-announce-ip"
  8) ""
  .....

  207) "unixsocketperm"
  208) "0"
  209) "slaveof"
  210) ""
  211) "notify-keyspace-events"
  212) ""
  213) "bind"
  214) "127.0.0.1"

5. Edit configuration

127.0.0.1:6379> CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE
127.0.0.1:6379> CONFIG SET loglevel "notice"
OK
redis 127.0.0.1:6379> CONFIG GET loglevel

1) "loglevel"
2) "notice"

6. Redis数据类型

Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。

string最大存储512MB,是最基本的类型

Hash: HMSET and HGET设置键值对

List:字符串列表,按照插入顺序排序,lpush/lrange

Set:string类型的无序集合,复杂度是O(1), sdd/smemebers

zset: string类型的有序集合,zdd/zrangebyscore

7. 基本key命令

127.0.0.1:6379> COMMAND KEY_NAME

127.0.0.1:6379> SET runoobkey redis
OK
127.0.0.1:6379> DEL runoobkey
(integer) 1

DEL key

DUMP key

EXISTS key

EXPIRE key seconds

EXPIREAT key timestamp

PEXPIRE key milliseconds

PEXPIREAT key milliseconds-timestamp

KEYS pattern

MOVE key db

PERSIST key

PTTL key

TTL key

RANDOMKEY

RENAME key newkey

RENAMENX key newkey

TYPE key

 

8. Redis string

https://www.runoob.com/redis/redis-strings.html

9. Redis hash

https://www.runoob.com/redis/redis-hashes.html

10. Redis list

https://www.runoob.com/redis/redis-lists.html

11. Redis set

https://www.runoob.com/redis/redis-sets.html

12. Redis sorted set

https://www.runoob.com/redis/redis-sorted-sets.html

13. Redis HyperLogLog

https://www.runoob.com/redis/redis-hyperloglog.html

 

14. Redis pub/sub messages

Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。

Redis 客户端可以订阅任意数量的频道。

下图展示了频道 channel1 , 以及订阅这个频道的三个客户端 —— client2 、 client5 和 client1 之间的关系:

当有新消息通过 PUBLISH 命令发送给频道 channel1 时, 这个消息就会被发送给订阅它的三个客户端:

以下实例演示了发布订阅是如何工作的。在我们实例中我们创建了订阅频道名为 redisChat:

redis 127.0.0.1:6379> SUBSCRIBE redisChat

Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "redisChat"
3) (integer) 1
现在,我们先重新开启个 redis 客户端,然后在同一个频道 redisChat 发布两次消息,订阅者就能接收到消息。

redis 127.0.0.1:6379> PUBLISH redisChat "Redis is a great caching technique"

(integer) 1

redis 127.0.0.1:6379> PUBLISH redisChat "Learn redis by runoob.com"

(integer) 1

# 订阅者的客户端会显示如下消息
1) "message"
2) "redisChat"
3) "Redis is a great caching technique"
1) "message"
2) "redisChat"
3) "Learn redis by runoob.com"

 

15. Redis transaction

Redis 事务可以一次执行多个命令, 并且带有以下三个重要的保证:

  • 批量操作在发送 EXEC 命令前被放入队列缓存。
  • 收到 EXEC 命令后进入事务执行,事务中任意命令执行失败,其余的命令依然被执行。
  • 在事务执行过程,其他客户端提交的命令请求不会插入到事务执行命令序列中。

一个事务从开始到执行会经历以下三个阶段:

  • 开始事务。
  • 命令入队。
  • 执行事务。

单个 Redis 命令的执行是原子性的,但 Redis 没有在事务上增加任何维持原子性的机制,所以 Redis 事务的执行并不是原子性的。

事务可以理解为一个打包的批量执行脚本,但批量指令并非原子化的操作,中间某条指令的失败不会导致前面已做指令的回滚,也不会造成后续的指令不做。

redis 127.0.0.1:6379> MULTI
OK

redis 127.0.0.1:6379> SET book-name "Mastering C++ in 21 days"
QUEUED

redis 127.0.0.1:6379> GET book-name
QUEUED

redis 127.0.0.1:6379> SADD tag "C++" "Programming" "Mastering Series"
QUEUED

redis 127.0.0.1:6379> SMEMBERS tag
QUEUED

redis 127.0.0.1:6379> EXEC
1) OK
2) "Mastering C++ in 21 days"
3) (integer) 3
4) 1) "Mastering Series"
   2) "C++"
   3) "Programming"

 

16. Redis script file脚本

Eval 命令的基本语法如下:

redis 127.0.0.1:6379> EVAL script numkeys key [key ...] arg [arg ...]

17. Redis 连接

以下实例演示了客户端如何通过密码验证连接到 redis 服务,并检测服务是否在运行:

redis 127.0.0.1:6379> AUTH "password"
OK
redis 127.0.0.1:6379> PING
PONG

18. Redis数据备份与恢复

Redis SAVE 命令用于创建当前数据库的备份。

redis 127.0.0.1:6379> SAVE 

如果需要恢复数据,只需将备份文件 (dump.rdb) 移动到 redis 安装目录并启动服务即可。获取 redis 目录可以使用 CONFIG 命令,如下所示:

redis 127.0.0.1:6379> CONFIG GET dir
1) "dir"
2) "/usr/local/redis/bin"

创建 redis 备份文件也可以使用命令 BGSAVE,该命令在后台执行。

127.0.0.1:6379> BGSAVE

Background saving started

 

19. Redis 安全

127.0.0.1:6379> CONFIG set requirepass "runoob"
OK
127.0.0.1:6379> CONFIG get requirepass
1) "requirepass"
2) "runoob"

 

20. Redis性能

Redis 性能测试是通过同时执行多个命令实现的。

以下实例同时执行 10000 个请求来检测性能:

$ redis-benchmark -n 10000  -q

PING_INLINE: 141043.72 requests per second
PING_BULK: 142857.14 requests per second
SET: 141442.72 requests per second
GET: 145348.83 requests per second
INCR: 137362.64 requests per second
LPUSH: 145348.83 requests per second
LPOP: 146198.83 requests per second
SADD: 146198.83 requests per second
SPOP: 149253.73 requests per second
LPUSH (needed to benchmark LRANGE): 148588.42 requests per second
LRANGE_100 (first 100 elements): 58411.21 requests per second
LRANGE_300 (first 300 elements): 21195.42 requests per second
LRANGE_500 (first 450 elements): 14539.11 requests per second
LRANGE_600 (first 600 elements): 10504.20 requests per second
MSET (10 keys): 93283.58 requests per second

Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接,当一个连接建立后,Redis 内部会进行以下一些操作:

  • 首先,客户端 socket 会被设置为非阻塞模式,因为 Redis 在网络事件处理上采用的是非阻塞多路复用模型。
  • 然后为这个 socket 设置 TCP_NODELAY 属性,禁用 Nagle 算法
  • 然后创建一个可读的文件事件用于监听这个客户端 socket 的数据发送

21. Java中使用Redis

jedis-xxx.jar

 

posted @ 2019-08-05 16:38  ppju  阅读(314)  评论(0编辑  收藏  举报

加我,一起讨论技术噢