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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.