CAP定理
一致性 可用性 分区容错性
redis持久化机制
RDB (redis database)
原理:
利用时间点快照技术 point-in-time snapshot 保存了redis某个时间点上的数据;
在N秒内数据集至少有M个改动 这一条件就自动保存数据集;
怎么使用:
手动执行命令执行RDB 进入客户端save 或者bgsave 新的文件会覆盖原有的文件
使用bgsava命令时执行的是COW机制(copy on write), 异步 非阻塞
原理(fork COW):
fork 创建一个子进程 这个子进程共享父类中的数据(有时刻性) 在这个时刻后这两个进程都互不影响 主进程挂了 子线程也跟着挂了 子线程用于持久化操作,主进程提供对外合作
COW save 同步 阻塞
AOF (append only file)
记录日志的方式 把操作指令持续写到一个类似日志文件内 appendonly.aof 重启后通过重新执行这些命令来还原数据操作。
1.实时数据备份,安全 2..AOF文件体积大 3.执行速度慢 4.持久化速度比RDB慢 手动bgrewriteao 生成aof文件
[note]AOF和RDB同时开启时AOF的优先级更高
常用命令
自己百度去
redis应用
redis主从复制 集群构建 读写分离--业务区分 哨兵模式 --监听 redis 安全机制 redis事物 multi exec redis集群搭建 缓存穿透 缓存一般都是通过key去查找value,如果不存在对应的value,就要去数据库中查找,如果这个key对应的value在数据库中不存在, 并且访问这个key数据并发量也很大,就会对数据库产生压力。这就是缓存穿透 解决方案 布隆过滤器 布隆过滤器 庞大的数据中是否含有某个元素 缓存雪崩 缓存重启或者缓存在某一段失效,造成数据库瞬间的访问量增大,造成缓存雪崩 加锁或者队列控制数据库缓存的进程量 设置不同的过期时间,缓存点均匀分布 二级缓存 redis值的生存时间
redis底层原理分析
hash槽 存储方式
redis核心数据结构剖析
待看
Redis应用场景
1.String -- 单值缓存 -- 对象缓存 -- 分布式锁 -- 计数器 -- Web集群session共享 -- 分布式系统全局序列号 2.List -- 微信公共号 信息流 3.set -- 抽奖小程序 -- 关注模型 4.Zset -- 点击计数 -- 排行榜 5.Hash -- 对象缓存 -- 电商购物车
Redis是单线程吗?
Redis 的单线程主要是指 Redis 的网络 IO 和键值对读写是由一个线程来完成的,这也是 Redis 对外提供键值存储服务的主要流程。但 Redis 的其他功能,比如持久化、异步删除、集群数据同步等,其实是由额外的线程执行的。
Redis 单线程为什么还能这么快?
因为它所有的数据都在内存中,所有的运算都是内存级别的运算,而且单线程避免了多线程的切换性能损耗问题。正因为 Redis 是单线程,所以要小心使用 Redis 指令,对于那些耗时的指令(比如keys),一定要谨慎使用,一不小心就可能会导致 Redis 卡顿。
Redis 单线程如何处理那么多的并发客户端连接?
Redis的IO多路复用:redis利用epoll来实现IO多路复用,将连接信息和事件放到队列中,依次放到文件事件分派器,事件分派器将事件分发给事件处理器。
Redis五种存储类型
百度廖志伟博客 图解很好理解
快链 https://blog.csdn.net/java_wxid/category_8600390.html
代码敲敲乐
package com.springbootweb.webapp.springDateRedis; /** * @author Liyongzhe * @date 2021/3/18_10:21 */ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Set; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml") public class testSet { @Autowired private RedisTemplate<String, String> redisTemplate; /** * 存入值 */ @Test public void setValue(){ redisTemplate.boundSetOps("nameset").add("曹操"); redisTemplate.boundSetOps("nameset").add("刘备"); redisTemplate.boundSetOps("nameset").add("孙权"); } /** * 提取值 */ @Test public void getValue(){ Set<String> members = redisTemplate.boundSetOps("nameset").members(); System.out.println(members); } /** * 删除集合中的某一个值 */ @Test public void deleteValue(){ redisTemplate.boundSetOps("nameset").remove("孙权"); } /** * 删除整个集合 */ @Test public void deleteAllValue(){ redisTemplate.delete("nameset"); } }
package com.springbootweb.webapp.springDateRedis; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author Liyongzhe * @date 2021/3/17_16:50 */ //让测试运行于Spring测试环 境,以便在测试开始的时候自动创建Spring的应用上下文 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml") public class TestValue { @Autowired private RedisTemplate redisTemplate; //添加值 @Test public void setValue(){ redisTemplate.boundValueOps("String_key").set("String_values"); } //取值 @Test public void getValue(){ String str = (String) redisTemplate.boundValueOps("String_key").get(); System.out.println("String_key: "+str); } //删除值 @Test public void deleteValue(){ redisTemplate.delete("String_key"); } }
redisConfig.properties
#主机连接地址 redis.host=127.0.0.1 #连接端口 redis.port=6379 redis.pass= redis.database=0 redis.maxIdle=300 redis.maxWait=3000 redis.testOnBorrow=true
applicationContext-redis.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath*:properties/*.properties" /> <!-- 配置redis相关信息连接池 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> <property name="maxWaitMillis" value="${redis.maxWait}" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:poolConfig-ref="poolConfig"></bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> </bean> </beans>