NoSQL概述-Redis安装-常用五大数据类型-概述Bitmaps,HyperLogLog,Geospatial和redis.conf 基本解析

NoSQL 概述

  • NoSQL(NoSQL=Not Only SQL),意即“不仅仅是SQL”,泛指 非关系型的数据库
  • NoSQL不依赖业务逻辑方式存储,而以简单的key-value模式存储。因此大大的增加了数据库的扩展能力。
  • 不遵循SQL标准。
  • 不支持ACID.
  • 远超于SQL的性能。
  1. NoSQL 适用场景
  • 对数据高并发的读写
  • 海量数据的读写
  • 对数据高可扩展性的

Redis 安装概述

  • Redis是一个 开源 的 key-value 存储系统。
  • 和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set-有序集合)和hash(哈希类型)。
  • 这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,
    而且这些操作都是 原子性 的。
  • 在此基础上,Redis支持各种不同方式的排序
  • Rdis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件。
  • 并且在此基础上实现了master-slave(主从)同步。
  1. 安装

    1. 安装Redis的机器需要gcc环境

    2. 在官网下载安装包: https://redis.io/download/#redis-downloads

    3. 解压下载好的redis.*.*.tar.gz包(tar -zxvf)

    4. 在解压好的redis.*.*文件夹内执行 make 进行编译

      1. 如果没有准备好C语言编译环境,make会报错(Jemalloc./jemalloc.h:没有那个文件)
      2. 解决方案:运行: make distclean
      3. 在解压好的redis.*.*文件夹内执行 make 进行编译
    5. 执行:make install (进行安装)

    6. 默认安装目录:/usr/local/bin

  2. 查看 默认的安装路径(/usr/local/bin)

    • redis-benchmark: 性能测试工具,可以在自己本子运行,看看自己本子性能如何
    • redis-check-aof: 修复有问题的AOF文件:
    • redis-check-dump: 修复有问题的dump.rdb文件,
    • redis-sentinel: Redis集群使用
    • redis-server: Redis服务器启动命令
    • redis-cli:客户端,操作入口
  3. 启动

    1. 后台启动

      1. 拷贝( cp ) 一份redis.conf到其他文件夹

      2. 在拷贝后的 redis.conf文件里修改

        • ################################# GENERAL #####################################
          
          # By default Redis does not run as a daemon. Use 'yes' if you need it.
          # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
          # When Redis is supervised by upstart or systemd, this parameter has no impact.
          daemonize yes
          #daemonize no 改为 daemonize yes
          #/daem--查询
          
      3. 在到(/usr/local/bin)下执行:redis-server [修改后的redis.conf文件路径]

      4. [root@iZuf619z87y4phm0888ytzZ bin]# redis-server /zzyyuse/redis-6.2.7/Redis-con/redis.conf #启动redis
        [root@iZuf619z87y4phm0888ytzZ bin]# ps -ef | grep redis
        root     16750     1  0 23:10 ?        00:00:00 redis-server 127.0.0.1:6379
        root     16757 16713  0 23:12 pts/2    00:00:00 grep --color=auto redis
        [root@iZuf619z87y4phm0888ytzZ bin]# redis-cli   		#redis-cli:客户端,操作入口
        127.0.0.1:6379> ping
        PONG
        127.0.0.1:6379> SHUTDOWN      #1.关闭
        not connected> 
        not connected> exit  
        [root@iZuf619z87y4phm0888ytzZ bin]# ps -ef | grep redis		
        root     16760 16713  0 23:14 pts/2    00:00:00 grep --color=auto redis
        [root@iZuf619z87y4phm0888ytzZ bin]# redis-server /zzyyuse/redis-6.2.7/Redis-con/redis.conf #启动
        [root@iZuf619z87y4phm0888ytzZ bin]# ps -ef | grep redis   
        root     16762     1  0 23:14 ?        00:00:00 redis-server 127.0.0.1:6379
        root     16768 16713  0 23:14 pts/2    00:00:00 grep --color=auto redis
        [root@iZuf619z87y4phm0888ytzZ bin]# kill -9 16762			#2.关闭
        [root@iZuf619z87y4phm0888ytzZ bin]# ps -ef | grep redis
        root     16770 16713  0 23:14 pts/2    00:00:00 grep --color=auto redis
        [root@iZuf619z87y4phm0888ytzZ bin]#
        
    2. 前台启动:在(/usr/local/bin)里执行–> redis-server

      • [root@iZuf619z87y4phm0888ytzZ bin]# redis-server
      • 不建议,这样这个启动页面已关闭,服务也会跟着停止

redis 相关知识

  1. 默认端口号:6379
  2. 默认16个数据库,类似数组下标从0开始,初始默认使用0号库(0-15)
    1. select [几号库] 到几号库,切换库
    2. dbsize 查看当前数据库的key的数量
    3. flushdb清空当前库
    4. flushall通杀全部库
  3. 同一管理,所有库 密码一样
  4. Redis 是单线程+多路IO复用
    • 多路复用是指使用一个线程来检查多个文件描述符(Socket)的就绪状态,比如调用select和poll函数,传入多个文件描述符,如果有一个文件描述符就绪,则返回,否则阻塞直到超时。得到就绪状态后进行真正的操作可以在同一个线程里执行,也可以启动线程执行(比如使用线程池)
    • 想一下,你和你的室友都去找黄牛买票。黄牛一个人到车站去买你们所有人的票,你们不用买票该干嘛干嘛,黄牛买到了谁的票就会找谁

Redis 键(key)

参考文档:http://redis.cn/commands.html

  • keys * 查看当前库所有key(匹配:keys*1)
  • set [key] [value] 添加键值对
  • exists key 判断某个key是否存在,
  • type key 查看你的key是什么类型
  • del key 删除指定的key数据。(直接删)
  • unlink key 根据value选择非阻塞删除(仅将keys从keyspace元数据中删除,真正的删除会在后续异步操作。)
  • expire [key] 10 表示给key 设置为10 秒过期,以秒为单位
  • ttl [key] 查看key 还有多少秒过期,-1表示永不过期,-2表示已经过期
  • select 切换到几号库
  • dbsize 查看当前库的 key 数量
  • flushdb 清空当前库
[root@iZuf619z87y4phm0888ytzZ bin]#  redis-c
redis-check-aof  redis-check-rdb  redis-cli        
[root@iZuf619z87y4phm0888ytzZ bin]#  redis-cli
127.0.0.1:6379> set k1 kang				#添加key-value
OK
127.0.0.1:6379> set k2 jk
OK
127.0.0.1:6379> keys *					#查看库的 key
1) "k1"
2) "k2"
127.0.0.1:6379> exists k2				#判断某个key是否存在,1表示存在,0表示不存在
(integer) 1
127.0.0.1:6379> exists k5
(integer) 0
127.0.0.1:6379> expire k2 20			#表示给k2(key)设置为 20秒过期,以秒为单位
(integer) 1
127.0.0.1:6379> ttl k2					# 查看k2(key) 还有多少秒过期,-1表示永不过期,-2表示已经过期
(integer) 15
127.0.0.1:6379> ttl k2
(integer) 13
127.0.0.1:6379> ttl k2
(integer) 13
127.0.0.1:6379> ttl k2
(integer) 9
127.0.0.1:6379> ttl k2
(integer) -2
127.0.0.1:6379> ttl k1
(integer) -1
127.0.0.1:6379> keys *
1) "k1"
127.0.0.1:6379> SELECT 5		#select 切换到5号库,当前默认0号库
OK
127.0.0.1:6379[5]> SELECT 0
OK
127.0.0.1:6379> DBSIZE				#dbsize 查看当前库的 key 数量
(integer) 1
127.0.0.1:6379> FLUSHDB 				# flushdb 清空当前库
OK
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> DBSIZE
(integer) 0
127.0.0.1:6379> 


Redis 字符串(String)

  • String是Redis最基本的类型,一个key对应一个value。

  • String类型是二进制安全的。意味着Redis的string可以包含任何数据。比如jpg图片或者序列化的对象。

  • String类型是Redis最基本的数据类型,一个Redis中字符串value最多可以是512M

  1. 相关命令

    1. set [key] [value] 添加键值对

    2. get [key] 查询对应 key 的 value

    3. append [key] [value] 将指定定的[value]追加到原值的末尾(追加),

    4. strlen [key] 获得值的长度,

    5. setnx [key] [value] 只有在key不存在时,设置key的值。


    6. mset [key] [value] [key] [value] … 可以同时设置多个键值对

    7. mget [key1] [key2] [key3] … 可以同时获取多个value值

    8. msetnx [key] [value] [key] [value] … 同时设置一个或多个key-value对,仅当所有给定key都不存在(原子性,有一个设置的key 存在则所有设置都失败)。

    9. getrange [key] <起始位置> <结束位置> 获得值的范围

    10. setrange [key] <起始位置> [value] 用覆写所储存的字符串值,从<起始位置>开始(索引从0开始)

    11. setex [key] <过期时间> [value] 设置键和值 的同时 设置过期时间 单位为

    12. getset [key] [value] 以新换旧,设置了新值同时获得引旧值

    13. [root@iZuf619z87y4phm0888ytzZ bin]# redis-cli
      127.0.0.1:6379> keys *
      (empty array)
      127.0.0.1:6379> mset K1 Kang K2 jiek k3 mrya
      OK
      127.0.0.1:6379> keys *
      1) "k3"
      2) "K1"
      3) "K2"
      127.0.0.1:6379> mget K1 K3			#这里 K3  应为小写k3(注意区分大小写)
      1) "Kang"
      2) (nil)
      127.0.0.1:6379> mget K1 k3
      1) "Kang"
      2) "mrya"
      127.0.0.1:6379> getrange K1 0 1				#获得值的范围
      "Ka"
      127.0.0.1:6379> setrange K1 1 He			#用<value>覆写<key>所储存的字符串值(11)
      (integer) 4
      127.0.0.1:6379> getrange K1 0 1
      "KH"
      127.0.0.1:6379> get K1
      "KHeg"
      127.0.0.1:6379> setex K4 15 123456				# 设置键和值 的同时 设置过期时间 单位为 `秒`(12)
      OK
      127.0.0.1:6379> ttl K4					#查看K4(key) 还有多少秒过期,-1表示永不过期,-2表示已经过期
      (integer) 10
      127.0.0.1:6379> ttl K4
      (integer) 8
      127.0.0.1:6379> ttl K4
      (integer) 6
      127.0.0.1:6379> get K4
      (nil)
      127.0.0.1:6379> ttl K4
      (integer) -2
      127.0.0.1:6379> 
      
      
      
  2. 原子操作

    1. 所谓原子操作是指不会被线程调度机制打断的操作,这种操作一旦开始,就一直运行到结束,中间不会有任何context switch(切换到另个线程)。(Redis 是单线程+多路IO复用)
    2. 在单线程中(Redis),能够在单条指令中完成的操作都可以认为是"原子操作”,因为中断只能发生于指令之间。
    3. 在多线程中,不能被其它进程(线程)打断的操作就叫原子操作,Redis单命令的原子性主要得益于Redis的单线程。
  3. 数据结构

    1. String的数据结构为简单动态字符串(Simple Dynamic String,缩写SDS)。是可以修改的字符串,内部结构实现上类似于Java的ArrayList,采用预分配冗余空间的方式来减少内存的频繁分配。

    2. 在这里插入图片描述

      ​ 如图中所示,内部为当前字符串实际分配的空间capacity一般要高于实际字符串长度1en。当字符串长度小于1M时,扩容都是加倍现有的空间,如果超过1M,扩容时一次只会多扩1M的空间。需要注意的是字符串最大长度为512M

Redis 列表(List)

  • 简介
    • 单键多值
      • Reds列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。
      • 它的底层实际是个双向链表,对两端的操作性能很高,通过索引下标的操作中间的节点性能会较差。
  1. 常用命令

    1. lpush / rpush [value1] [value2] [value3] 从左边 / 右边插入一个或多个值。

    2. Ipop / rpop 从左边 / 右边吐出一个值。值在键在,值光键亡

    3. lrange [开始] [结束] 按照下标获取元素

    4. rpoplpush 从列表右边 吐出一个值,插到列表左边

    5. lindex [index] 按照索下标获得元素(从左到右)

    6. Ilen 获得列表长度

    7. linsert before [插入的值] 在的后面插入 [插入值的值]

    8. lrem [n] 从左边删除 n 个value(从左到右)

    9. lset [index] 将列表key下标为[index]的值替换成

    10. 127.0.0.1:6379> LPUSH k1 1 2 3 4
      (integer) 4
      127.0.0.1:6379> LRANGE k1 0 -1
      1) "4"
      2) "3"
      3) "2"
      4) "1"
      127.0.0.1:6379> rPUSH kr 1 2 3 4
      (integer) 4
      127.0.0.1:6379> LRANGE kr 0 -1			#0 -1 代表全部
      1) "1"
      2) "2"
      3) "3"
      4) "4"
      127.0.0.1:6379> LRANGE kr 0 2
      1) "1"
      2) "2"
      3) "3"
      127.0.0.1:6379> RPOPLPUSH k1 kr			# 从 k1 `列表右边` 吐出一个值,插到 kr `列表左边`
      "1"
      127.0.0.1:6379> LRANGE kr 0 -1
      1) "1"
      2) "1"
      3) "2"
      4) "3"
      5) "4"
      127.0.0.1:6379> linsert kr before 2 hh			#在<value>=2 的后面插入 `hh`  (7)
      (integer) 6
      127.0.0.1:6379> LRANGE kr 0 -1
      1) "1"
      2) "1"
      3) "hh"
      4) "2"
      5) "3"
      6) "4"
      127.0.0.1:6379> LRANGE k1 0 -1
      1) "4"
      2) "3"
      3) "2"
      127.0.0.1:6379> linsert k1 before 2 hh
      (integer) 4
      127.0.0.1:6379> LRANGE k1 0 -1
      1) "4"
      2) "3"
      3) "hh"
      4) "2"
      127.0.0.1:6379> lset kr hh kang 
      (error) ERR value is not an integer or out of range
      127.0.0.1:6379> lset k1 2 kang 				#将列表key下标为 `2` 的值替换成 `kang`  (9)
      OK
      127.0.0.1:6379> LRANGE k1 0 -1
      1) "4"
      2) "3"
      3) "kang"
      4) "2"
      127.0.0.1:6379> 
      
      

  1. 数据结构

    1. List的数据结构为快速链表quickList
    • 首先在列表元素较少的情况下会使用一块连续的内存存储,这个结构是ziplist,也即是压缩列表。
    • 它将所有的元素紧挨着一起存储,分配的是一块连续的内存。
    • 当数据量比较多的时候才会改成quicklist.
    • 因为普通的链表需要的附加指针空间太大,会比较浪费空间。比如这个列表里存的只是int类型的数据,结构上还需要两个额外的指针prev和next
ziplist<---->ziplist<---->ziplist<---->ziplist<---->ziplist

Redis将链表ziplist结合起来组成了quicklist。也就是将多个ziplist使用双向指针(<—>)串起来使用。这样既满足了快速的插入删除性能,又不会出现太大的空间冗余。


Redis 集合(set)

  • Redis set 对外提供的功能与list类似是一个列表的功能,特殊之处在于set是可以自动排重的,当你需要存储一个列表数据,又不希望出现重复数据时,set是一个很好的选择,并且set 提供了判断某个成员是否在一个set集合内的重要接口,这个也是list所不能提供的。
  • Redis的Setstring类型的无序集合。它底层其实是一个value为null的hash表,所以添加,删除,查找的复杂度都是==0(1)==找一次
  • 一个算法,随着数据的增加,执行时间的长短,如果是O(1),数据增加,查找数据的时间不变
  1. 常用命令

    1. sadd … 将一个或多个元素加入到集合中,已经存在的元素将被忽略。

    2. smembers 取出该集合的所有值。

    3. sismember 判断集合是否为含有该值,有1,没有0

    4. scard 返回该集合的元素个数。

    5. srem … 删除集合中某个元素

    6. spop 随机从该集合中吐出一个值。

    7. srandmember [n] 随机从该集合中取出 n 个值。不会从集合中删除。

    8. smove 把集合中一个值从一个集合移动到另一个集合

    9. sinter 返回两个集合的交集元素。

    10. sunion 返回两个集合的并集元素。

    11. sdif 返回两个集合的差集元素(key1中的,不包含key2中的)

    12. 127.0.0.1:6379> sadd k1 1 2 3
      (integer) 3
      127.0.0.1:6379> sadd k2 6 5 3
      (integer) 3
      127.0.0.1:6379> SMEMBERS k1
      1) "1"
      2) "2"
      3) "3"
      127.0.0.1:6379> SMEMBERS k2
      1) "3"
      2) "5"
      3) "6"
      127.0.0.1:6379> SINTER k1 k2		#返回两个集合的交集元素
      1) "3"
      127.0.0.1:6379> SMOVE k1 k2 3		#把集合k1中一个值移动到另一个集合k2  应为与k2 中值重复,则不添加
      (integer) 1
      127.0.0.1:6379> SMEMBERS k2
      1) "3"
      2) "5"
      3) "6"
      127.0.0.1:6379> SMEMBERS k1
      1) "1"
      2) "2"
      127.0.0.1:6379> 
      
      
  2. 数据结构:Set 数据结构是dict 字典,字典是用哈希表实现的


Redis h哈希(Hash)

Redis hash 是一个键值对集合

Redis hash 是一个string类型的fieldvalue的映射表,hash 特别适合用于存储对象

在这里插入图片描述

  • 通过key(用户ID) + field(属性标签) 就可以操作对应属性数据了,既不需要重复存储数据,也不会带来序列化和并发修改控制的问题
  1. 常用命令
    1. hset 给集合中的 [field] 键赋值
    2. hget [field] 从集合[field] 取出
    3. hmset [field1] [field2] … 批量设置hash的值,
    4. hexists [field] 查看哈希表key中,给定域field是否存在。
    5. hkeys 列出该hash集合的所有field
    6. hvals 列出该hash集合的所有value
    7. hincrby [field] 为哈希表key中的域field的值加上增量1 -1
    8. hsetnx [field] 将哈希表key中的域field的值设置为value,当且仅当域field不存在
[root@iZuf619z87y4phm0888ytzZ bin]# redis-cli 
127.0.0.1:6379> hset user:1 name kang 
(integer) 1
127.0.0.1:6379> hset user:1 id 1
(integer) 1
127.0.0.1:6379> hget user:1 name
"kang"
127.0.0.1:6379> hmset user:2 id 2 name kang2
OK
127.0.0.1:6379> HEXISTS user:2 id				#查看哈希表 user:2 中,给定域 id 是否存在
(integer) 1
127.0.0.1:6379> HEXISTS user:1 id
(integer) 1
127.0.0.1:6379> hkeys user:1
1) "name"
2) "id"
127.0.0.1:6379> HVALS user:1
1) "kang"
2) "1"
127.0.0.1:6379> HVALS user:2
1) "2"
2) "kang2"
127.0.0.1:6379> HINCRBY user:1 id 2			#为哈希表user:1中的域`id`的值加上增量2(负数代表减量)
(integer) 3
127.0.0.1:6379> hget user:1 id 
"3"
127.0.0.1:6379> HSETNX user:1 id 5			#将哈希表user:1 中的域field的值设置为value,当且仅当域field不存在
(integer) 0
127.0.0.1:6379> hget user:1 id 
"3"
127.0.0.1:6379> HSETNX user:1 age 5
(integer) 1
127.0.0.1:6379> hget user:1 age
"5"
127.0.0.1:6379> HINCRBY user:1 age -4
(integer) 1
127.0.0.1:6379> hget user:1 age
"1"
127.0.0.1:6379> 
  1. Hash类型对应的数据结构是两种:ziplist(压缩列表),hashtable(哈希表)。当field-value长度较短且个数较少时,使用ziplist,否则(数量多)使用hashtable。


Redis 有序集合Zset(sorted set)

  • Redis有序集合zset普通集合set 非常相似,是一个没有重复元素的字符串集合。
  • 不同之处是有序集合的每个成员都关联了一个评分(score),这个评分(score)被用来按照从最低分到最高分的方式排序集合中的成员。集合的成员是唯一的,但是评分可以是重复的
  1. 常用命令
    1. zadd [score1] [score2] … 将一个或多个元素及其score(评分)值加入到有序集key当中。
    2. zrange [WITHSCORES] 返回有序集key中,下标在 之间的元素(0 -1 代表返回全部) 带 WITHSCORES ,可以让分数一起和值返回到结果集。
    3. zrangebyscore [min] [max] [withscores] [limit offset count] 返回有序集key中,所有score值介于min和max之间(包括等于min或max)的成员,有序集成员按score值递增(从小到大)次序排列。
    4. zrevrangebyscore [max] [min] [withscores][limit offset count] 同上,改为从大到小排列。
    5. zincrby 为元素的score(评分)加上增量,
    6. zrem 删除该集合下,指定值的元素
    7. zcount [min] [max] 统计该集合,分数[min] [max] 区间内的元素个数
    8. zrank 返回该值在集合中的排名,从0开始。
127.0.0.1:6379> zadd top1 100 c 90 j 70 py 70 sh
(integer) 4
127.0.0.1:6379> zrange top1
(error) ERR wrong number of arguments for 'zrange' command
127.0.0.1:6379> zrange top1 0 -1			#返回有序集top1中,下标在0 -1之间的元素(0 -1 代表返回全部)
1) "py"
2) "sh"
3) "j"
4) "c"
127.0.0.1:6379>  zrangebyscore top1 70 91 withscores		#返回有序集top1中,所有score值介于70和91之间(包括等于70或91)的成员,从小到大
1) "py"
2) "70"
3) "sh"
4) "70"
5) "j"
6) "90"
127.0.0.1:6379> zrevrangebyscore top1 70 91 withscores
(empty array)
127.0.0.1:6379> zrevrangebyscore top1 91 70 withscores			#同上,改为从大到小排列
1) "j"
2) "90"
3) "sh"
4) "70"
5) "py"
6) "70"
127.0.0.1:6379> ZINCRBY top1 100 c			#为元素的`score(评分)`加上增量
"200"
127.0.0.1:6379> ZREM top1 j			#删除top1 中的  j
(integer) 1
127.0.0.1:6379> zrange top1 0 -1
1) "py"
2) "sh"
3) "c"
127.0.0.1:6379> zcount top1 0 100
(integer) 2
127.0.0.1:6379> zrank top1 sh
(integer) 1
127.0.0.1:6379> zrank top1 c
(integer) 2
127.0.0.1:6379> 


  1. 数据结构
  • hash, hash的作用就是关联元素value权重score,保障元素value的唯一性,可以通过元素value找到相应的score值。
  • 跳跃表,跳跃表的目的在于给元素value排序,根据score的范围获取元素列表。

redis.conf 基本解析

# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# units are case insensitive so 1GB 1Gb 1gB are all the same.
配置大小单位,开头定义了一些基本的度量单位,只支持byts,不支持bit,大小写不敏感


################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all available network interfaces on the host machine.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
# Each address can be prefixed by "-", which means that redis will not fail to
# start if the address is not available. Being not available only refers to
# addresses that does not correspond to any network interfece. Addresses that
# are already in use will always fail, and unsupported protocols will always BE
# silently skipped.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1     # listens on two specific IPv4 addresses
# bind 127.0.0.1 ::1              # listens on loopback IPv4 and IPv6
# bind * -::*                     # like the default, all available interfaces
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only on the
# IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
# will only be able to accept client connections from the same host that it is
# running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 -::1

bind 表示现在仅支持本地连接,(#bind 127.0.0.1 -::1 --> 把bind注释, 在修改下面(protected-mode yes)就可以支持远程连接)

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes			表示开启保护模式,只能本机访问默认yes
							改为 no  才可以支持远程访问
							
# TCP listen() backlog.
#
# In high requests-per-second environments you need a high backlog in order
# to avoid slow clients connection issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
tcp-backlog 511
设置tcp的backlog,backlog其实是一个连接队列,backlog队列总和 = 未完成三次握手队列+已经完成三次握手队列(默认511)。
在高并发环境下你需要一个高backlog值来避免慢客户端连接问题。


# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
设置连接时间,以秒为单位,(默认值为0:永不超时)

# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.
tcp-keepalive 300
检测心跳:300秒为一个周期,检测是否在操作,如果没有则断开连接

################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize yes
#daemonize no 改为 daemonize yes
守护进程,yes表示后台启动

# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
#
# Note that on modern Linux systems "/run/redis.pid" is more conforming
# and should be used instead.
pidfile /var/run/redis_6379.pid
存放pid文件的位置,每个实例会产生一个不同的pid文件

# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice
日志设置级别 
logfile "" --> 设置日志输出文件路径(默认为空)


# maxclients 10000   --> 设置redis同时可以与多少个客户端进行连接。(默认10000)达到现在Redis 会拒绝连接请求

# maxmemory <bytes>  --> 1. 建议 必须设置 否则,将内存占满,造成服务器宕机
#  2. 设置redis可以使用的内存量。一旦到达内存使用上限,redis将会试图移除内部数据,移除规则可以通过maxmemory-policy来指定。

maxmemory-policy:
volatile-lru: 使用LRU算法移除key,只对设置了`过期时间的键`;(最近最少使用)
allkeys-lru: 在所有集合key中,使用LRU算法移除key
volatile-random: 在过期集合中移除随机的key,只对设置了过期时间的键,
allkeys-random: 在所有集合key中,移除随机的key
volatile-ttl: 移除那些TTL值最小的key,即那些最近要过期的key
noeviction: 不进行移除。针对写操作,只是返回错误信息

#
# The default of 5 produces good enough results. 10 Approximates very closely
# true LRU but costs more CPU. 3 is faster but not very accurate.
#
# maxmemory-samples 5
maxmemory-samples
设置样本数量,LRU算法和最小TTL算法都并非是精确的算法,而是估算值,所以你可以设置样本的大小,redis默认会检查这么多个key并选择其中LRU的那个,一般设置3到7的数值,数值越小样本越不准确单性能消耗小

Bitmaps 概述

合理地使用操作位能够有效地提高内存使用率和开发效率

缺点:在第一次初始化Bitmaps时,假如偏移量非常大,那么整个初始化过程执行会比较慢,可能会造成Redis的阻塞。

Redis提供了Bitmaps这个 "数据类型”可以实现对位的操作:

  1. Bitmaps本身不是一种数据类型,实际上它就是字符串(key-value),但是它可以对字符串的位进行操作。

  2. Bitmaps单独提供了一套命令,所以在Redis中使用Bitmaps和使用字符串的方法不太相同。可以把Bitmaps想象成一个以位为单位的数组数组的每个单元只能存储0和1,数组的下标在Bitmaps中叫做偏移量


  3. 常用命令

    1. setbit [偏移量] 设置Bitmaps中某个偏移量的值(0或1) 偏移量从0开始
    2. getbit [偏移量] 获取Bitmaps中某个偏移量的值:
    3. bitcount [x y] 统计字符串从x字节到y字节比特值为1的数量
    4. bitop and(or/not/xor) [返回值] [key…] bitop是一个复合操作,它可以做多个Bitmaps的and(交集)、or(并集)、not(非)、xor(异或) 操作并将结果保存在 [返回值] 中。
  4. 小总结:Bitmaps 本身并不是一种数据类型,它只是一个字符串 专门进行 操作的,与set相比极大的节约空间,活跃的用户量不是很大时用Bitmaps并不合适,Bitmaps主要还是针对活跃用户量进行存储的方式。

HyperLogLog 概述

  • Redis HyperLogLog是用来做基数统计的算法,HyperLogLog的优点是,在输入元素的数量或者体积非常非常大时,计算基数所需的空间总是固定的、并且是很小的。
  • 因为HyperLogLog只会根据输入元素来计算基数,而不会储存输入元素本身,所以HyperLogLog不能像集合那样,返回输入的各个元素。
  1. 常用命令

    1. pfadd [value] [value1] 添加指定元素(一个或多个,已存在的会添加失败)到HyperLogLog中

    2. pfcount [key] 计算HLL的近似基数

    3. pfmerge [k2] 将 k1和 k2 合并到 K

Geospatial 概述

  • Redis3.2中增加了对GE0类型的支持。GE0,Geographic,地理信息的缩写
  • 该类型,就是元素的2维坐标,在地图上就是经纬度。redis基于该类型,提供了经纬度设置,查询,范围查询,距离查询,经纬度Hash等常见操作。
  1. 常用命令
    1. geoadd [经度(-180到180)] [纬度(-85.051到85.051)] <城市> [经度] [纬度] <城市1>… 可以添加一个或多个
    2. geopos [城市] 获取城市经纬度
    3. geodist <城市1> <城市2> [m米|km千米|ft英里|mi英尺(默认单位是 m米)] 获取两个位置之间的直线距离
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值