NoSQL 概念

0.关系型数据库的原理
ACID
原子性
一致性
隔离性
持久性

1.CAP定理
一致性(Consistency)所有节点在同一时间拥有相同的数据
可用性(Availability)保证每个请求不管成功或失败都有响应
分区容忍(Partition tolerance)(系统中任意信息的丢失或失败都不会影响系统的正常运作)

CAP理论的核心是:一个分布式系统不可能同时满足C A P三个特性 最多只能同时较好的满足两个

CA - 单点集群,满足一致性,可用性的系统。可扩展性不是很高
CP - 满足一致性和分区容忍的系统 通常性能不是特别高
AP - 满足可用性,分区容忍性的系统,通常可能对一致性要求低一些(弱一致性)(大多分布式系统为AP)

2.NoSQL BASE

basically Availble 基本可用
Soft-state 软状态、柔性事务。
Eventual consistency 最终一致性 也是ACID的最终目的

Redis 入门

Remote Dictionary Server 远程字典服务

支持数据的备份,即master-slave模式的数据备份
单进程的 默认16个数据库(计数从0开始)

默认端口6379

基本命令

  1. set key1 "hello world" #设置key1的值为hello
  2. get key1 #获得key1的值
  3. exit #断开连接
  4. shutdown #关闭当前服务 然后断开连接
  5. keys [pattern] #搜索key
  6. select 1 #切换到一号数据库
  7. flushdb #删库
  8. flushall #删除所有数据库
  9. clear #清屏

key-value操作

  1. exists [key] #判断键值对是否存在
  2. type [key] #查看value类型
  3. expire [key] [seconds] #设置过期时间
  4. pexpire [key] [ms] #设置毫秒值
  5. ttl [key] #查看过期时间 -1 未设置有效时间 -2 key已不存在
  6. pttl [key] #过期时间 毫秒值
  7. persist [key] #设置不过期
  8. del [key] # 删除key
  9. rename [key] [newkey] #重命名key
  10. randomkey # 随机返回key值
  11. move [key] [dbid] #移动键值对到另一个数据库

缓存有效期和过期策略 有效期叫做TTL(Time to live) 设置有效期的作用:节省空间和数据的弱一致性—有效期失效后保证数据的一致性

过期或淘汰策略 FIFO (First In First Out) LRU(Least Recently Used) LFU (least Frequently Used)

Redis数据结构

五大基本数据类型

String 字符串
List 列表 (有序可重复)
Hash 字典
Set 集合 (无序无重复)
ZSet 有序集合 可扩展使用GEO
Bitmap(位图) HyperLogLog(基数统计)Stream(流)

其中list hash set zset为容器型结构,共享两个规则,不存在就创建,没有元素就删除

String(字符串)

  1. strlen #查看字符串长度
  2. append #添加长度
  3. #运算
  4. incr #自增
  5. decr #自建
  6. incrby [num] #+运算
  7. decrby [num] #-运算
  8. #范围操作
  9. getrange [key] [startindex] [endindex] #截取字符串
  10. setrange [key] [startindex] [endindex] [str] #替换对应位置为str
  11. #组合操作
  12. setex [key] [seconds] #设置值 同时设置过期时间
  13. setnx [key] [value] #如果key不存在 不设置0 存在设置返回1
  14. getset [key][newvlaue] #设置新值 返回旧值
  15. #批量操作
  16. mget [key...] #接收多个key 返回多个value
  17. mset [key...] [value...] #设置多个key
  18. msetnt [key...] [value...]#设置多个key if not exists

当value的值为整数时 可以直接进行运算

String扩容 <1MB时翻倍扩容 ,>1MB一次扩容1MB

List(列表)一个key对应一个List

  1. lpush [key] [value...] #压栈 头插
  2. rpush [key] [value] #入队 尾插
  3. lrange [key] [startindex] [endindex] #截取值
  4. lpop [key] #出队或者出栈
  5. rpop [key] #出栈
  6. llen [key] #查看长度
  7. lindex [index] #获取某一个字段的值
  8. lrem [key] [startindex] [endindex] #删除n个value
  9. ltrim [key] [startindex] [endindex] 截取某个范围的值重新复制给list
  10. linsert [key] [localtion] [index] [value] #插入数据到某个位置的前后
  11. lpushx [key][value] #头插插入
  12. rpushx [key][value] #尾插插入

Hash(本质HashMap,数组+链表)

  1. hset [key] [value]
  2. hget [key] [value]
  3. hkeys [key] #遍历map的keys
  4. hvals [key] #遍历map的values
  5. # 批量操作
  6. hmset [key] [key][value]...
  7. hmget [key] [key]...
  8. #
  9. hdel [key] [key]
  10. hlen [key]
  11. hgetall [key]
  12. #set if not exists
  13. hsetnx [key] [key] [value]

Set(集合 去重,无序)

  1. sadd [key] [value]
  2. #len 总数
  3. scard [key]
  4. #遍历
  5. smembers [key]
  6. # 删除
  7. srem [key] [key...]
  8. # 随机元素
  9. srandmember [key] [number]
  10. # 随机删除
  11. spop [key] [number]
  12. #set交互操作
  13. smove key [key1] [key2] [vlaue]
  14. # 交集
  15. sinter [key1] [key2]
  16. # 差集
  17. sdiff [key1] [key2]
  18. # 并集
  19. sunion [key1] [key2]

Sorted Set (Zset 有序集合 升序)

原理:
SortedSet 和 HashMap的结合,内部是一种叫”跳跃列表”的数据结构,通过层级制,将元素分层并串联,每隔几个元素选出一个代表,再将代表使用另外一级指针穿起来,以此类推,形成金字塔结构。同一个元素在不同层级之间身兼数职,视为跳跃。新元素插入时,逐层下潜,知道找到合适的位置

  1. #增加元素
  2. zadd [key] [score] [member] ... ...
  3. #查看元素
  4. zrange [key] [start] [end] [[withscores]]
  5. zrangebyscore [key] [minscore] [maxsc] [[limit [start] [end]]]
  6. #删除成员
  7. zrem [key] [member]
  8. # 统计个数
  9. zcard [key]
  10. zcount [key] [minscore] [maxsc]
  11. #查询成员分数
  12. zscore [key] [member]
  13. # 查询索引
  14. zrank [key] [member]
  15. # 逆序索引
  16. zrevrank [key] [member]
  17. # 逆序查找
  18. zrevrange [key] [start] [end]
  19. # 逆序查找分数
  20. zrevrangebyscore [key] [minsc] [maxsc]

GEO(地理位置距离排序 基于zset)

  1. geoadd [key] [经度] [纬度] [name]
  2. # 搜索
  3. zrange
  4. # 测算距离
  5. geolist [key] [member1] [member2] [距离单位m km mi ft]
  6. # 查询成员经纬度
  7. geopos [key] [member]
  8. # hash算法
  9. geohash [key] [member]
  10. # 以当前位置为中心 返回指定半径内的位置 可选参数 返回距离 返回经纬度 返回hash
  11. georadius [key] [member] [radius] [单位] [[withdist withcoord withhash]]

BitMap(位图)
bitmap实际上就是一个byte数组 只能存0、1
零存整取,零存零取,整存零取

  1. # 增 查
  2. setbit [key] [offset] [value]
  3. getbit [key] [offset]
  4. # 统计查找
  5. bitcount [key] [start] [end]
  6. bitpos
  7. #
  8. bitfield

HyperLogLog(基数统计) 基于基数估算的算法 标准误差在0.81%内

  1. pfadd
  2. pfcount

Stream(流)
持久化的消息订阅系统
发布/订阅 pub/sub
进程间的消息通信模式

  1. subscrible channel #订阅频道 会持续接收消息
  2. publish channel massage #在某个频道发布消息
  1. # 生产消息 返回消息ID 时间戳+顺序
  2. xadd key [用户组] msg [x]
  3. #长度
  4. xlen key
  5. # 查找
  6. xrange key - +
  7. # 删除消息
  8. xdel key 消息id
  9. #####################订阅者命令###########
  10. # 阻塞等待消息返回block 返回指定消息数 count
  11. xread [block num] streams key 0-0[count num]

Redis 配置 * 配置优于一切 **
bind 0.0.0.0
只能指定ip地址访问
protected-mode yes
开启保护
port 6379
端口号
timeout seconds
客户端超时 0代表一直保持
tcp-keepalive second
检查客户端是否健康 避免阻塞
tcp-backlog num
队列数量(包括未完成三次握手和已完成三次握手的)

daemonize ture/false
后台运行
pidfile
写入redis的进程id的文件地址
loglevel notice 生产环境默认
logfile “” 日志文件