经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Redis » 查看文章
redis学习
来源:cnblogs  作者:ppju  时间:2019/8/7 8:43:50  对本文有异议

https://redis.io/

1. 安装redis

  1. > 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

  1. > cd src && ./redis-server
  2. 22501:C 05 Aug 2019 13:37:12.267 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  3. 22501:C 05 Aug 2019 13:37:12.267 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=22501, just started
  4. 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
  5. 22501:M 05 Aug 2019 13:37:12.268 * Increased maximum number of open files to 10032 (it was originally set to 1024).
  6. _._
  7. _.-``__ ''-._
  8. _.-`` `. `_. ''-._ Redis 5.0.5 (00000000/0) 64 bit
  9. .-`` .-```. ```\/ _.,_ ''-._
  10. ( ' , .-` | `, ) Running in standalone mode
  11. |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
  12. | `-._ `._ / _.-' | PID: 22501
  13. `-._ `-._ `-./ _.-' _.-'
  14. |`-._`-._ `-.__.-' _.-'_.-'|
  15. | `-._`-._ _.-'_.-' | http://redis.io
  16. `-._ `-._`-.__.-'_.-' _.-'
  17. |`-._`-._ `-.__.-' _.-'_.-'|
  18. | `-._`-._ _.-'_.-' |
  19. `-._ `-._`-.__.-'_.-' _.-'
  20. `-._ `-.__.-' _.-'
  21. `-._ _.-'
  22. `-.__.-'
  23.  
  24. 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.
  25. 22501:M 05 Aug 2019 13:37:12.271 # Server initialized
  26. 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.
  27. 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.
  28. 22501:M 05 Aug 2019 13:37:12.271 * Ready to accept connections

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

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

    --raw 避免中文乱码

4. Redis CONFIG命令格式

  1. 127.0.0.1:6379> CONFIG GET CONFIG_SETTING_NAME
  1. 127.0.0.1:6379> config get loglevel
  2. 1) "loglevel"
  3. 2) "notice"
  1. 127.0.0.1:6379> config get *
  2. 1) "dbfilename"
  3. 2) "dump.rdb"
  4. 3) "requirepass"
  5. 4) ""
  6. 5) "masterauth"
  7. 6) ""
  8. 7) "cluster-announce-ip"
  9. 8) ""
  10. .....
  11. 207) "unixsocketperm"
  12. 208) "0"
  13. 209) "slaveof"
  14. 210) ""
  15. 211) "notify-keyspace-events"
  16. 212) ""
  17. 213) "bind"
  18. 214) "127.0.0.1"

5. Edit configuration

  1. 127.0.0.1:6379> CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE
  1. 127.0.0.1:6379> CONFIG SET loglevel "notice"
  2. OK
  3. redis 127.0.0.1:6379> CONFIG GET loglevel
  4. 1) "loglevel"
  5. 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命令

  1. 127.0.0.1:6379> COMMAND KEY_NAME
  2. 127.0.0.1:6379> SET runoobkey redis
  3. OK
  4. 127.0.0.1:6379> DEL runoobkey
  5. (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 时, 这个消息就会被发送给订阅它的三个客户端:

  1. 以下实例演示了发布订阅是如何工作的。在我们实例中我们创建了订阅频道名为 redisChat:
  2. redis 127.0.0.1:6379> SUBSCRIBE redisChat
  3. Reading messages... (press Ctrl-C to quit)
  4. 1) "subscribe"
  5. 2) "redisChat"
  6. 3) (integer) 1
  7. 现在,我们先重新开启个 redis 客户端,然后在同一个频道 redisChat 发布两次消息,订阅者就能接收到消息。
  8. redis 127.0.0.1:6379> PUBLISH redisChat "Redis is a great caching technique"
  9. (integer) 1
  10. redis 127.0.0.1:6379> PUBLISH redisChat "Learn redis by runoob.com"
  11. (integer) 1
  12. # 订阅者的客户端会显示如下消息
  13. 1) "message"
  14. 2) "redisChat"
  15. 3) "Redis is a great caching technique"
  16. 1) "message"
  17. 2) "redisChat"
  18. 3) "Learn redis by runoob.com"

 

15. Redis transaction

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

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

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

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

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

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

  1. redis 127.0.0.1:6379> MULTI
  2. OK
  3. redis 127.0.0.1:6379> SET book-name "Mastering C++ in 21 days"
  4. QUEUED
  5. redis 127.0.0.1:6379> GET book-name
  6. QUEUED
  7. redis 127.0.0.1:6379> SADD tag "C++" "Programming" "Mastering Series"
  8. QUEUED
  9. redis 127.0.0.1:6379> SMEMBERS tag
  10. QUEUED
  11. redis 127.0.0.1:6379> EXEC
  12. 1) OK
  13. 2) "Mastering C++ in 21 days"
  14. 3) (integer) 3
  15. 4) 1) "Mastering Series"
  16. 2) "C++"
  17. 3) "Programming"

 

16. Redis script file脚本

  1. Eval 命令的基本语法如下:
  2. redis 127.0.0.1:6379> EVAL script numkeys key [key ...] arg [arg ...]

17. Redis 连接

  1. 以下实例演示了客户端如何通过密码验证连接到 redis 服务,并检测服务是否在运行:
  2. redis 127.0.0.1:6379> AUTH "password"
  3. OK
  4. redis 127.0.0.1:6379> PING
  5. PONG

18. Redis数据备份与恢复

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

  1. redis 127.0.0.1:6379> SAVE

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

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

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

  1. 127.0.0.1:6379> BGSAVE
  2. Background saving started

 

19. Redis 安全

  1. 127.0.0.1:6379> CONFIG set requirepass "runoob"
  2. OK
  3. 127.0.0.1:6379> CONFIG get requirepass
  4. 1) "requirepass"
  5. 2) "runoob"

 

20. Redis性能

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

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

  1. $ redis-benchmark -n 10000 -q
  2. PING_INLINE: 141043.72 requests per second
  3. PING_BULK: 142857.14 requests per second
  4. SET: 141442.72 requests per second
  5. GET: 145348.83 requests per second
  6. INCR: 137362.64 requests per second
  7. LPUSH: 145348.83 requests per second
  8. LPOP: 146198.83 requests per second
  9. SADD: 146198.83 requests per second
  10. SPOP: 149253.73 requests per second
  11. LPUSH (needed to benchmark LRANGE): 148588.42 requests per second
  12. LRANGE_100 (first 100 elements): 58411.21 requests per second
  13. LRANGE_300 (first 300 elements): 21195.42 requests per second
  14. LRANGE_500 (first 450 elements): 14539.11 requests per second
  15. LRANGE_600 (first 600 elements): 10504.20 requests per second
  16. 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

 

原文链接:http://www.cnblogs.com/ppju/p/11303883.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号