Redis-技术专题-Redis知识体系
{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"1.基本文件说明"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c8/c8df417d1216165831d65a00031e2cd7.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"2.基础命令"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/44/44e02da4e1f6074ee9e842548728d63b.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"3.字符串命令"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e8/e862e54c858061df98afe66101a90f4b.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"4.哈希(Hash)命令"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a8/a86c8f4b7abe2c84ddb5e7d7746992f0.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"编码: field value 值由 ziplist 及 hashtable 两种编码格式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"字段较少的时候采用"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ziplist"},{"type":"text","text":",字段较多的时候会变成"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"hashtable"},{"type":"text","text":"编码"}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"5.列表(List)命令"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 一个列表最多可以包含 "},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"232 - 1 "},{"type":"text","text":"个元素 ("},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"4294967295"},{"type":"text","text":", 每个列表超过"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"40"},{"type":"text","text":"亿个元素)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"容量 -> 集合,有序集合也是如此。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6e/6e7776adb9af2e046cff64e4cd5d4052.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"6.集合(Set)命令"}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":" Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/23/23cea99d44b943c05fa77fe4df5411cd.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"7.有序集合(sorted set)命令"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 不同的是每个元素都会关联一个"},{"type":"text","marks":[{"type":"strong"}],"text":"double"},{"type":"text","text":"类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。"}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"有序集合的成员是唯一的,但分数("},{"type":"text","text":"score"},{"type":"text","marks":[{"type":"strong"}],"text":")却可以重复。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/94/944d58a6a9019a80b271a6c60327367d.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"8.发布订阅"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"开启两个客户端"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"A客户端订阅频道:subscribe redisChat (频道名字为 redisChat)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"B客户端发布内容:publish redisChat \"Hello, this is my wor\" (内容是 hello....)"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"A客户端即为自动收到内容, 原理图如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/dd/dddfbfb67c61d912da8ad918a837771b.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/00/0062974a89fc02b192bd3ac5fb2247f5.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/57/572ad5a6ea461175bb3c807a73e07902.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"9.Redis 事务"}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Redis 事务可以一次执行多个命令, 并且带有以下三个重要的保证:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"批量操作在发送 EXEC 命令前被放入队列缓存"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"收到 EXEC 命令后进入事务执行,事务中任意命令执行失败,其余的命令依然被执行"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"在事务执行过程,其他客户端提交的命令请求不会插入到事务执行命令序列中"}]}]}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 一个事务从开始到执行会经历以下三个阶段:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"开始事务"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"命令入队"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"执行事务"}]}]}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"注意:redis事务和数据库事务不同,redis事务出错后最大的特点是,一剩下的命令会继续执行,二出错的数据不会回滚"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/af/af63ec8d056d9cdcadb3562016a9ab22.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"10.Redis 服务器命令"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/16/168c17614429783926a971f902e79b12.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"11.Redis 数据备份与恢复"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":" Redis "},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"SAVE"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":" 命令用于创建当前数据库的备份"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 如果需要恢复数据,只需将备份文件 ("},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"dump.rdb"},{"type":"text","text":") 移动到 redis 安装目录并启动服务即可。获取 redis 目录可以使用 "},{"type":"text","marks":[{"type":"strong"}],"text":"CONFIG"},{"type":"text","text":" 命令"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"12.Redis 性能测试"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"redis 性能测试的基本命令如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"redis目录执行:redis-benchmark [option] [option value]\n// 会返回各种操作的性能报告(100连接,10000请求)\nredis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 10000\n// 100个字节作为value值进行压测\nredis-benchmark -h 127.0.0.1 -p 6379 -q -d 100"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Jedis"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"\n\n redis.clients \n jedis \n 2.8.2 \n "}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Jedis配置"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"############# redis Config #############\n# Redis数据库索引(默认为0)\nspring.redis.database=0\n# Redis服务器地址\nspring.redis.host=120.79.88.17\n# Redis服务器连接端口\nspring.redis.port=6379\n# Redis服务器连接密码(默认为空)\nspring.redis.password=123456\n# 连接池中的最大空闲连接\nspring.redis.jedis.pool.max-idle=8\n# 连接池中的最小空闲连接\nspring.redis.jedis.pool.min-idle=0"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"JedisConfig"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"@Configuration\npublic class JedisConfig extends CachingConfigurerSupport {\n\n @Value(\"${spring.redis.host}\")\n private String host;\n\n @Value(\"${spring.redis.port}\")\n private int port;\n\n @Value(\"${spring.redis.password}\")\n private String password;\n\n @Value(\"${spring.redis.max-idle}\")\n private Integer maxIdle;\n\n @Value(\"${spring.redis.min-idle}\")\n private Integer minIdle;\n\n @Bean\n public JedisPool redisPoolFactory(){\n JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();\n jedisPoolConfig.setMaxIdle(maxIdle);\n jedisPoolConfig.setMinIdle(minIdle);\n jedisPoolConfig.setMaxWaitMillis(3000L);\n int timeOut = 3;\n return new JedisPool(jedisPoolConfig, host, port, timeOut, password);\n }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"基础使用"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"@RunWith(SpringRunner.class)\n@SpringBootTest(classes = KerwinBootsApplication.class)\npublic class ApplicationTests {\n @Resource\n JedisPool jedisPool;\n @Test\n public void testJedis () {\n Jedis jedis = jedisPool.getResource();\n jedis.set(\"year\", String.valueOf(24));\n }\n}"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"SpringBoot RedisTemplate"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"\n\n org.springframework.boot \n spring-boot-starter-data-redis \n \n\n\n org.apache.commons \n commons-pool2 \n \n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"############# redis Config #############\n# Redis数据库索引(默认为0)\nspring.redis.database=0\n# Redis服务器地址\nspring.redis.host=120.79.88.17\n# Redis服务器连接端口\nspring.redis.port=6379\n# Redis服务器连接密码(默认为空)\nspring.redis.password=123456\n# 连接池最大连接数(使用负值表示没有限制)\nspring.redis.jedis.pool.max-active=200\n# 连接池最大阻塞等待时间(使用负值表示没有限制)\nspring.redis.jedis.pool.max-wait=1000ms\n# 连接池中的最大空闲连接\nspring.redis.jedis.pool.max-idle=8\n# 连接池中的最小空闲连接\nspring.redis.jedis.pool.min-idle=0\n# 连接超时时间(毫秒)\nspring.redis.timeout=1000ms"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"// Cache注解配置类\n@Configuration\npublic class RedisCacheConfig {\n\n @Bean\n public KeyGenerator simpleKeyGenerator() {\n return (o, method, objects) -> {\n StringBuilder stringBuilder = new StringBuilder();\n stringBuilder.append(o.getClass().getSimpleName());\n stringBuilder.append(\".\");\n stringBuilder.append(method.getName());\n stringBuilder.append(\"[\");\n for (Object obj : objects) {\n stringBuilder.append(obj.toString());\n }\n stringBuilder.append(\"]\");\n return stringBuilder.toString();\n };\n }\n \n @Bean\n public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {\n return new RedisCacheManager(\n RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),\n // 默认策略,未配置的 key 会使用这个\n this.getRedisCacheConfigurationWithTtl(15),\n // 指定 key 策略\n this.getRedisCacheConfigurationMap()\n );\n }\n \n private Map getRedisCacheConfigurationMap() {\n Map redisCacheConfigurationMap = new HashMap<>(16);\n redisCacheConfigurationMap.put(\"redisTest\", this.getRedisCacheConfigurationWithTtl(15));\n return redisCacheConfigurationMap;\n }\n \n private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {\n Jackson2JsonRedisSerializer
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.