Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库。 redis不仅支持key-value类型的数据,还支持其他复杂的数据类型。 Redis 提供了两种持久化方式:RDB (Redis DataBase)(默认)和 AOF (Append Only File)。 在指定的时间间隔内,执行指定次数的写操作,则会将内存中的数据写入到磁盘中。即在指定目录下生成一个dump.rdb文件。redis 重启会通过加载dump.rdb文件恢复数据。 附: Redis 默认不开启。是已文件形式存储 附: redis事务是 一组命令集合,1.开启事务可以一次执行多条命令,一条事务中的所有命令会被序列化。 注:不分大小写 附:事务的基本操作: 架构分析:有图可以帮你更好的理解 参考文档:Redis基础知识整理
redis基础知识点
什么是redis及特点作用
redis支持的数据类型
redis主要支持的数据类型有5种:
String字符串,Hash哈希 ,List列表,Set集合,zset有序集合(sorted set)。
string 类型是二进制安全的。 string 可以包含任何数据。比如jpg图片或者序列化的对象。
string 类型是 redis 最基本的数据类型,string 类型的值最大能存储 512MB。#set 设置值 set key value #get 取值 get value
hash 是一个键值(key=>value)对集合。
hash是一个string类型的field和value的映射表,hash特别适合用于存储对象。# 如果key 被用过,需删除 del key # hash 设置值 hmset key key1 value1 key2 value2 # hash 取值 hget key key1/key2
list 是简单的字符串列表,按照插入顺序排序。添加一个元素到列表的头部(左边)或者尾部(右边)。# 如果有其他类型key,需删除,否则报错 del key # list 设置值 lpush key value1 //lpush头部添加,可以多次value设置 # list 取值 lrange key 0 10 //0 ,10 是范围
set是string类型的无序集合。
集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。# 如果有其他类型key,需删除,否则报错 del key # set 设置值 sadd key value1 //可以多次value设置 # set 取值 smembers key
zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。
不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。
zset的成员是唯一的,但分数(score)却可以重复。# 如果有其他类型key,需删除,否则报错 del key # zset 设置值 zadd key 0 value1 //可以多次value设置; 0 是分数(次数),可重复 # zset 取值 zrangebyscore key 0 10 //通过分数排序去 0~10 之间的值
附:redis指令测试练习工具
redis持久化
ps : 持久化就是把内存的数据写到磁盘中去,防止服务宕机了内存数据丢失。RDB
redis.conf 配置文件中RDB持久化配置信息:# RDB 持久化,快照保存频率 # 900秒内,如果超过1个key被修改,则发起快照保存 # 300秒内,如果超过10个key被修改,则发起快照保存 # 60秒内,如果1万个key被修改,则发起快照保存 save 900 1 save 300 10 save 60 10000 # 启用rdb文件压缩手段,默认为yes。 rdbcompression yes
AOF
它的出现是为了弥补RDB的不足(数据的不一致性),所以它采用日志的形式来记录每个写操作,并追加到文件中。
redis 重启 会根据日志文件的内容将 写指令从前到后执行一次以完成数据的恢复工作。
redis.conf 配置文件中AOF持久化配置信息:# 开启 AOF 持久化 appendonly yes
RDB 持久化适合大规模的数据恢复但它的数据一致性和完整性较差。
AOF 的数据完整性比RDB高,但记录内容多了,会影响数据恢复的效率。
需要手动开启AOF持久化方式,默认是每秒将写操作日志追加到AOF文件中。
Redis 做缓存,可以关闭持久化。redis事务
什么是redis事务
2.其他客户端提交的命令请求不会插入到事务执行命令序列中。redis事务过程
redis事务命令
# 1、开启一个事务 MULTI # 2、执行事务 EXEC # 3、放弃事务:客户端取消事务队列,退出事务 DISCARD # 4、监控 一个或多个key,若其中一个key被修改,则事务不执行。 # 监控一直持续到EXEC命令结束 WATCH key [key ...]
redis事务注意示例
redis架构模式
特点:操作简单,易理解。
不足:1、内存容量有限 2、处理能力有限 3、无法高可用。
特点:服务器分为两类角色:master(主服务器,一般负责写);slave(从服务器,一般负责读)。可实现 读写分离。
不足:1、无法保证高可用;2、没有解决 master 写的压力
特点:sentinel 分布式监控:master和slave;出现问题时:1.提醒通知,2.自动故障迁移.
不足:1、主从模式,切换需要时间丢数据;2、没有解决 master 写的压力
1、Sentinel 高可用集群,主从复制,故障转移
2、Cluster 高可用集群,配置简单,支持故障转移,动态增减节点,水平横向扩展(推荐)
Redis系统架构详解
架构具体实现:
Redis 系列文章导读运用场景(后期再优化)
在会话中保存用户等信息,好处可以提供持久化。
关系型数据库在排行榜方面查询速度普遍偏慢,利用redis这个非关系数据库SortedSet进行热点数据的排序很快。
redis一般做缓存数据库,可以加快访问数据的速度,但也可以在分布式应用中用来实现分布式锁。效果相当于zookeeper,但是大多数的过程需要手动操作。
redis菜鸟教程
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算