Redis学习笔记——初级
1、Redis是什么、特点、优势
Redis是一个开源的使用C语言编写、开源、支持网络、可基于内存亦可持久化的日志型、高性能的Key-Value数据库,并提供多语言的API。
它通常被称为数据结构服务器,因为值(value)可以是字符串(String)、哈希(Map)、列表(list)、集合(sets)和有序集合(sorted sets)等类型。
Redis与其他key-value缓存产品有以下三个特点:
(a)、Redis支持数据的持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用。
(b)、Redis不见金支持简单的key-vallue类型的数据,同时还提供list、set、zset、hash等数据结构的存储。
(c)、Redis支持数据的备份,即master-slave模式的数据备份。
Redis的优势:
(a)、性能极高-Redis能读的速度是110000次/s,写的速度是81000次/s。
(b)、丰富的数据类型-Redis支持二进制案例的String、Lists、Hashes、Sets以及Sorted Sets数据类型操作。
(c)、丰富的特性-Redis还支持publish/subscribe,通知,key过期等等特性。
2、Redis安装(Linux)、启动、退出、设置密码、远程连接
2.1、安装redis
下载redis安装包(如:redis-4.0.11.tar.gz)
[root@localhost src]# wget http://download.redis.io/releases/redis-4.0.11.tar.gz [root@localhost src]# tar -zxvf redis-4.0.11.tar.gz [root@localhost src]# cd redis-4.0.11 [root@localhost redis-4.0.11]# make && make install
2.2、后台启动服务端
[root@localhost redis-4.0.11]# redis-server & [1] 5397 [root@localhost redis-4.0.11]# 5397:C 09 Oct 23:28:11.744 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5397:C 09 Oct 23:28:11.745 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=5397, just started 5397:C 09 Oct 23:28:11.745 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 5397:M 09 Oct 23:28:11.746 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 5397 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 5397:M 09 Oct 23:28:11.749 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 5397:M 09 Oct 23:28:11.749 # Server initialized 5397:M 09 Oct 23:28:11.749 # 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. 5397:M 09 Oct 23:28:11.750 # 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. 5397:M 09 Oct 23:28:11.750 * DB loaded from disk: 0.000 seconds 5397:M 09 Oct 23:28:11.750 * Ready to accept connections
注:redis-server默认启动端口时6379,没有密码
如果不使用默认配置文件,启动时可以加上配置文件
[root@localhost redis-4.0.11]# redis-server /usr/local/src/redis-4.0.11/redis.enabled.conf & [1] 9317 [root@localhost redis-4.0.11]# 9317:C 09 Oct 23:38:54.877 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 9317:C 09 Oct 23:38:54.877 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=9317, just started 9317:C 09 Oct 23:38:54.877 # Configuration loaded 9317:M 09 Oct 23:38:54.879 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 9317 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 9317:M 09 Oct 23:38:54.881 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 9317:M 09 Oct 23:38:54.881 # Server initialized 9317:M 09 Oct 23:38:54.881 # 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. 9317:M 09 Oct 23:38:54.881 # 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. 9317:M 09 Oct 23:38:54.881 * Ready to accept connections
2.3、启动客户端、验证
[root@localhost ~]# redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379> set var "hello world" OK 127.0.0.1:6379> get var "hello world" 127.0.0.1:6379>
2.4、退出
关闭redis-cli
[root@localhost ~]# ps aux|grep redis root 9317 0.1 0.8 145312 7984 pts/0 Sl 23:38 0:00 redis-server 127.0.0.1:6379 root 9746 0.0 0.0 112704 972 pts/1 R+ 23:46 0:00 grep --color=auto redis [root@localhost ~]# redis-cli shutdown [root@localhost ~]# ps aux|grep redis root 10016 0.0 0.0 112704 968 pts/1 R+ 23:52 0:00 grep --color=auto redis [root@localhost ~]#
注:如果设置上密码后,单纯的redis-cli是关不掉的,必须加上ip、port、password
[root@localhost redis-4.0.11]# redis-cli -h 127.0.0.1 -p 6379 -a 123456 shutdown Warning: Using a password with '-a' option on the command line interface may not be safe. 10475:M 10 Oct 00:02:57.875 # User requested shutdown... 10475:M 10 Oct 00:02:57.875 * Saving the final RDB snapshot before exiting. 10475:M 10 Oct 00:02:57.880 * DB saved on disk 10475:M 10 Oct 00:02:57.880 * Removing the pid file. 10475:M 10 Oct 00:02:57.880 # Redis is now ready to exit, bye bye... [1]+ Done redis-server /usr/local/src/redis-4.0.11/redis.enabled.conf [root@localhost redis-4.0.11]#
退出客户端
[root@localhost redis-4.0.11]# redis-cli 127.0.0.1:6379> quit [root@localhost redis-4.0.11]#
2.5、设置密码
打开redis.enabled.conf找到requirepass,去掉默认,修改
requirepass 123456
验证密码的正确性
[root@localhost redis-4.0.11]# redis-cli 127.0.0.1:6379> auth 123456 OK 127.0.0.1:6379>
2.6、远程连接
[root@localhost redis-4.0.11]# redis-cli -h 127.0.0.1 -p 6379 -a 123456 Warning: Using a password with '-a' option on the command line interface may not be safe. 127.0.0.1:6379>
2.7、查看redis-server统计信息
127.0.0.1:6379> INFO
3、Redis Key
Redis是key-value的数据库,Redis的键用于管理Redis的键,基本语法是:
COMMAND KEY_NAME
例子:
127.0.0.1:6379> set var redis OK 127.0.0.1:6379> get var "redis" 127.0.0.1:6379> del var (integer) 1 127.0.0.1:6379> get var (nil) 127.0.0.1:6379>
注:redis命令不区分大小写,所以get var和GET var是等价的。
4、Redis数据类型
Redis支持五种数据类型:string(字符串)、hash(哈希)、list(列表)、set(集合)以及zset(sorted set:有序集合)。
4.1、String(字符串)
(a)、是Redis最基本的数据类型,可以理解成与Memcached一模一样的类型,一个key对应一个value。
(b)、二进制安全的。意思是redis的string可以包含任何数据。比如jpg图片或者有序列化的对象。
(c)、一个键最大能存储512MB。
例子:
127.0.0.1:6379> set var "String type" OK 127.0.0.1:6379> get var "String type" 127.0.0.1:6379>
说明:利用set给变量var赋值“String type”;利用get获得变量var的值。
4.2、Hash(哈希)
(a)、是一个键值对集合
(b)、是一个string类型的field和value的映射表,hash特别适合用于存储对象
例子:
127.0.0.1:6379> hset set1 name zhouguowei (integer) 1 127.0.0.1:6379> hset set1 score 90 (integer) 1 127.0.0.1:6379> hget set1 name "zhouguowei" 127.0.0.1:6379> hget set1 score "90" 127.0.0.1:6379>
说明:hset和hget一次只能往哈希结构里面插入一个键值对,如果插入多个可以用hmset和hmget。
例子:
127.0.0.1:6379> hmset var1 name zhouguowei school xiaoxue OK 127.0.0.1:6379> hmget var1 name school 1) "zhouguowei" 2) "xiaoxue" 127.0.0.1:6379>
127.0.0.1:6379> hgetall var1 1) "name" 2) "zhouguowei" 3) "school" 4) "xiaoxue" 127.0.0.1:6379>
说明:var1是键值,每个hash可以存储232 - 1键值对(40多亿)。hmset用于建立hash对象,hgetall用于获取hash对象。
4.3、LIST(列表)
例子:
127.0.0.1:6379> lpush var2 1 (integer) 1 127.0.0.1:6379> lpush var2 a (integer) 2 127.0.0.1:6379> lpush var2 ab (integer) 3 127.0.0.1:6379> lrange var2 0 1 1) "ab" 2) "a" 127.0.0.1:6379> lrange var2 0 10 1) "ab" 2) "a" 3) "1" 127.0.0.1:6379>
说明:lpush往列表的前边插入;lrange后面的数字是范围(闭区间),列表最多可存储232 - 1 元素(每个列表可存储40多亿)。
4.4、Set(集合)
Redis的Set是String类型的无序集合,集合是通过哈希表实现的,所以添加、删除、查找的复杂度都是O(1)。
例子:
127.0.0.1:6379> sadd var3 redis (integer) 1 127.0.0.1:6379> sadd var3 mongodb (integer) 1 127.0.0.1:6379> sadd var3 rabbitmq (integer) 1 127.0.0.1:6379> smembers var3 1) "rabbitmq" 2) "mongodb" 3) "redis" 127.0.0.1:6379>
说明:sadd往集合中插入元素,smembers列举出集合中的元素,成功插入返回1;插入错误返回0。
4.5、zset(有序集合)
zset和set一样都是String类型的集合,且不允许元素重复。
zset和set不同的地方在于zset关联一个double类型的分数,redis通过分数对集合中的元素排序。
zset的元素是唯一的,但是分数是可以重复的。
例子:
127.0.0.1:6379> zadd var4 1 redis (integer) 1 127.0.0.1:6379> zadd var4 1 redis (integer) 0 127.0.0.1:6379> zadd var4 2 redis (integer) 0 127.0.0.1:6379> zadd var4 2 mongodb (integer) 1 127.0.0.1:6379> zadd var4 0 rabbitmq (integer) 1 127.0.0.1:6379> zadd var4 -2 celery (integer) 1 127.0.0.1:6379> ZRANGEBYSCORE var4 0 1000 1) "rabbitmq" 2) "mongodb" 3) "redis" 127.0.0.1:6379> ZRANGEBYSCORE var4 -3 1000 1) "celery" 2) "rabbitmq" 3) "mongodb" 4) "redis" 127.0.0.1:6379>
说明:成功插入返回1,否则返回0.插入已存在的元素失败返回0,分数为float(可正、负、0)。