读了很多二次加工的知识,还是homepage最好
Redis 是什么?
offical doc
Redis is an open source (BSD licensed), in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
You can run atomic operations on these types, like appending to a string; incrementing the value in a hash; pushing an element to a list; computing set intersection, union and difference; or getting the member with highest ranking in a sorted set.
To achieve top performance, Redis works with an in-memory dataset. Depending on your use case, Redis can persist your data either by periodically dumping the dataset to disk or by appending each command to a disk-based log. You can also disable persistence if you just need a feature-rich, networked, in-memory cache.
Redis supports asynchronous replication, with fast non-blocking synchronization and auto-reconnection with partial resynchronization on net split.
Redis also includes:
- Transactions
- Pub/Sub
- Lua scripting
- Keys with a limited time-to-live
- LRU eviction of keys
- Automatic failover
You can use Redis from most programming languages.
Redis is written in ANSI C and works on most POSIX systems like Linux, *BSD, and Mac OS X, without external dependencies. Linux and OS X are the two operating systems where Redis is developed and tested the most, and we recommend using Linux for deployment. Redis may work in Solaris-derived systems like SmartOS, but support is best effort. There is no official support for Windows builds.
part1:总体介绍与使用场景
Redis是一个开源的,基于内存的,结构化的存储服务,可用于作为数据库,缓存,消息中间件,流引擎使用
part2:支持丰富的数据结构
Redis提供了多种数据结构,例如string,hash,list,set,sorted set,以及bitmaps,hyperloglogs,geospatial indexes and streams,可以对这些数据类型执行原子操作 比如说append to a string, increment the value in a hash,push a element to a list,集合的差集,并集计算,或者得到sorted set中评分最高的元素
part3:高性能和持久化
Redis使用内存数据集来达到高性能,(读写数量级为10^6次/s),根据用户的使用场景,Redis可以通过周期性的dump 数据集到磁盘,或者append command to log文件来持久化数据,当然如果仅需要内存数据库时,也可以关闭持久化
part4:内部组成
Redis内置复制机制保证高可用,Redis支持异步复制,具有快速非阻塞同步,和当网络出现分区时自动重连并部分重新同步 支持LUA scripting,LRU淘汰策略,事务,以及不同等级的硬盘持久化策略,同时借助Redis 哨兵机制和Redis Cluster自动分区提供了高可用功能
为什么要使用Redis?
- 读写性能优秀
从业务场景出发,当出现了性能和并发瓶颈时需要使用Redis
2.数据类型丰富
支持多种数据结构支持二进制安全的Strings
3.原子性 redis中所有操作都是原子性的
4.丰富的特性 支持pub/sub.通知,key过期等特性
5.持久化
6.支持分布式 redis cluster
Redis的使用场景
热点数据的缓存
缓存是REdis最常见的应用场景 首先因为redis读写性能优异 且支持事务,保证数据的一致性
两种缓存方案
1.读取前,先去读redis,如果没有数据,读取数据库,将数据拉入redis
注意: 缓存击穿:数据库就没有要命中的数据,导致redis层被击穿 数据实时性无法保证
2.插入数据时,同时写入redis 数据实时性强,但数据量不宜过大
限时任务
利用redis expire命令设置key的过期时间,可以运用在限时/手机验证码等场景
计数器
redis的INCR家族命令可原子性计数
分布式锁
//todo
排行榜
利用redis sortedSet实现排行榜功能
好友关系
set的交并差集操作实现共同好友功能
简单队列
redis的双端,双向list可以作为链表,队列,栈使用