新手村:最適合新手的 Redis 基礎

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1. 什麼是Redis"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們先看看"},{"type":"link","attrs":{"href":"https://redis.io/","title":null},"content":[{"type":"text","text":"Redis官網"}],"marks":[{"type":"color","attrs":{"color":"#1e6bb8","name":"user"}},{"type":"strong"}]},{"type":"text","text":"給的介紹:"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#6a737d","name":"user"}}],"text":"Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries 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."}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"簡而言之,Redis是一個開源的使用ANSI C語言編寫、遵守BSD協議、支持網絡、可基於內存亦可持久化的日誌型、Key-Value數據庫,並提供多種數據類型,功能挺強。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2. Redis之父"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Redis之父是Salvatore Sanfilippo,一名來自意大利西西里島的程序員,大家更習慣稱呼他Antirez。如果你對他感興趣,可以去他的"},{"type":"link","attrs":{"href":"http://antirez.com","title":null},"content":[{"type":"text","text":"博客"}],"marks":[{"type":"color","attrs":{"color":"#1e6bb8","name":"user"}},{"type":"strong"}]},{"type":"text","text":"逛逛,或者去關注他的"},{"type":"link","attrs":{"href":"http://github.com/antirez","title":null},"content":[{"type":"text","text":"github"}],"marks":[{"type":"color","attrs":{"color":"#1e6bb8","name":"user"}},{"type":"strong"}]},{"type":"text","text":"。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3. Redis有什麼優勢"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"速度快"},{"type":"text","text":":Redis使用內存來存儲數據集,且支持Pipelining命令,可以一次發送多條命令。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"持久化"},{"type":"text","text":":可以把內存中的數據保存在磁盤中,在重啓時可重新加載使用。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"原子性"},{"type":"text","text":":所有操作都是原子性的,同時支持事務。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"數據結構豐富"},{"type":"text","text":":支持字符串、列表、哈希、集合及有序集合,滿足大多數使用需求。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"支持多種語言"},{"type":"text","text":":Redis支持許多語言,如C、C++、C#、Go、Java、JavaScript、PHP等等。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"多種特性"},{"type":"text","text":":Redis還支持publish/subscribe, 通知, key 過期等特性。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4. Redis能做什麼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲Redis交換數據速度快,所以常在服務器中被用於存儲一些需要頻繁調取的數據,相比於直接讀取磁盤來獲得數據,使用Redis可以節省大量時間,提升效率。舉個例子:"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#6a737d","name":"user"}}],"text":"某視頻網站每天有100萬人訪問首頁推薦視頻專欄,如果都從數據庫查詢讀取,那麼每天都至少要多出100萬次數據庫查詢請求。若使用了Redis,將常被調取的數據存到內存中,每次節約0.1秒,100萬次就有10萬秒,極大程度提高了速度與開銷。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"總之,Redis的應用場景十分廣泛,且價值極高,現在就開始學學基礎。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"5. 安裝Redis"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"工欲善其事必先利其器,學習Redis的第一步肯定就是安裝Redis。由於我的環境是Windows系統,就只演示Windows下安裝Redis。如果你不想安裝Redis,但想體驗一番,可以去官網提供的"},{"type":"link","attrs":{"href":"http://try.redis.io/","title":null},"content":[{"type":"text","text":"在線測試網站"}],"marks":[{"type":"color","attrs":{"color":"#1e6bb8","name":"user"}},{"type":"strong"}]},{"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":"strong"}],"text":"Windows下安裝Redis下載地址"},{"type":"text","text":":"},{"type":"link","attrs":{"href":"https://github.com/microsoftarchive/redis/releases/tag/win-3.2.100","title":null},"content":[{"type":"text","text":"https://github.com/microsoftarchive/redis/releases/tag/win-3.2.100"}],"marks":[{"type":"color","attrs":{"color":"#1e6bb8","name":"user"}},{"type":"strong"}]},{"type":"text","text":"這裏我們下載"},{"type":"text","marks":[{"type":"strong"}],"text":"Redis-x64-3.2.100.zip"},{"type":"text","text":"壓縮包文件(如果有更新版本,可以下載最近更新的穩定版本)。"}]}]}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2b/2ba077686cf8b06e1f16ad9063da49ca.png","alt":"download","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},"content":[{"type":"text","text":"下載好之後解壓到自己的文件夾中。例如我解壓到D:\\redis中。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/82/82f8d7ac142c486ed97cf1b7be6443c3.png","alt":"unzip","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},"content":[{"type":"text","text":"打開一個"},{"type":"text","marks":[{"type":"strong"}],"text":"cmd"},{"type":"text","text":"窗口,使用cd命令切換目錄到解壓後的文件夾路徑(例如我切換目錄到D:\\redis),然後運行命令:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"redis-server.exe redis.windows.conf。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"輸入之後會顯示如下界面:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/cb/cb114137337d9921045760ddc75d9f9f.jpeg","alt":"redis-server","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},"content":[{"type":"text","text":"這樣我們就打開了一個redis服務端,從圖中我們可以看到一些信息,比如端口號爲6379。如果要使用redis服務端,我們需要"},{"type":"text","marks":[{"type":"strong"}],"text":"再打開一個cmd窗口,原來的不要關閉,不然就不能訪問服務端了"},{"type":"text","text":"。同樣將路徑切換到redis目錄下,然後運行:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"redis-cli.exe -h 127.0.0.1 -p 6379"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"存入一個鍵值對:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"set firstkey \"hello redis\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"取出鍵值對:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"get firstkey"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"關閉連接:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"quit"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ef/ef4949269553492ccc5599fb705739b8.jpeg","alt":"first_use","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},"content":[{"type":"text","text":"這樣我們就完成了Redis在Windows下的安裝並初次體驗了Redis的key-value存儲方式。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"6. Redis數據結構"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"6. Redis數據結構"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Redis支持五種數據結構:"},{"type":"text","marks":[{"type":"strong"}],"text":"String(字符串)、Hash(哈希)、List(列表)、Set(集合)以及SortedSet(有序集合)"},{"type":"text","text":"。下面我們簡單介紹下每一種數據結構以及他們的基本命令。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"String"},{"type":"text","text":" String是Redis最基本的數據結構,也是任何存儲系統都必備的數據類型。String類型是二進制安全的,簡單來說,基本上你什麼都能用字符串存儲,你可以把圖片文件的內容或者序列化的對象作爲字符串來存儲。String類型的值最大可以存512MB,確實基本什麼都夠存了。"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"set key value:設置指定鍵的值"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"> set mykey \"hello world\"\nOK"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"get key:獲取指定鍵的值"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"> gey mykey\n\"hello world\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"getrange key start end:返回key中字符串的子串"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"> getrange mykey 6 10\n\"world\"\n> getrange mykey 0 20\n\"hello world\"\n> getrange mykey 0 -1\n\"hello world\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Tips"},{"type":"text","text":":從上面幾個實例,我們不難看出字符串起始從0開始;若end大於字符串長度時返回完整字符串;而當end爲-1時,end爲字符串最後一個字符。"}]}]},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"getset key value:設置指定鍵的新值,並返回舊值"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"> getset database \"mysql\"\n(nil)\n> get database\n\"mysql\"\n> getset database \"redis\"\n\"mysql\"\n> get database\n\"redis\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Tips"},{"type":"text","text":": 當鍵未設置時,會返回(nil)。"}]}]},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"strlen key:返回鍵所存儲的字符串值的長度"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"> strlen mykey\n(integer) 11"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"append key value:如果可以已存在且是一個字符串,則將指定value添加到原值末尾,會返回操作後字符串長度"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"> append mykey \", hello redis\"\n24\n>get mykey\n\"hello world, hello redis\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"incr key:整數值+1"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"> set incr_num 10\nOK\n>get incr_num\n\"10\"\n>incr incr_num\n(integer) 11"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"incrby key increment:整數值增加給定整數值(increment)"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"> incrby incr_num 4\n(intrger) 15"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"incrbyfloat key increment:數字值增加給定浮點值(increment)"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"> incrbyfloat incr_num 0.5\n15.5"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Tips"},{"type":"text","text":":整數值會顯示爲integer,當變爲浮點型後並不會提示爲float"}]}]},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"decr key:整數值-1"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > set decr_num 10\n OK\n > get decr_num\n \"10\"\n > decr decr_num\n (integer) 9"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"decrby key decrement:整數值減少給定整數值(decrement)"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > decrby decr_num 4\n (integer) 5"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Tips"},{"type":"text","text":":redis並沒有數字值減少給定浮點值的命令,如果我們想要decr_num減少2.5,可以用"},{"type":"text","marks":[{"type":"strong"}],"text":"incrbyfloat"},{"type":"text","text":"命令執行"},{"type":"text","marks":[{"type":"strong"}],"text":"incrbyfloat decr_num -2.5"},{"type":"text","text":"。"}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"> incrbyfloat decr_num -2.5\n2.5"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Hash"},{"type":"text","text":" Hash存儲的是field和value的映射關係,適合用戶存儲對象。比如要存儲一個用戶的姓名、年齡、地址等,就可以使用Hash。每個Hash可以存儲232>-1個field-value對(4294967295,40多億)。"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"hset key field value:設置哈希表中key中field的值爲value"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"hget key field:獲取哈希表key中field對應的value"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > hset myhash name \"test\"\n (integer) 1\n > hget myhash name\n \"NPC\"\n > hset myhash name \"NPC\"\n (integer) 0"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Tips"},{"type":"text","text":":使用hset命令,新建字段並設置值成功後返回1,如果修改已有字段的值則返回0。"}]}]},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"hmset key field1 value1 [field2 value2]:同時設置哈希表key中的多個field-value對。"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > hmset myhash age \"20\" country \"China\"\n OK"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"hexists key field:查看field是否存在於哈希表key中"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > hexists myhash name\n (integer) 1\n > hexists myhash phone\n (integer) 0"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Tips"},{"type":"text","text":":哈希表key中含有字段field返回1,不含有或對應key不存在返回0。"}]}]},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"hmget key field1 [field2]:獲取哈希表key中所有給定field的value"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > hmget myhash name age phone\n 1) \"NPC\"\n 2) \"20\"\n 3) (nil)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"hgetall key:獲取哈希表key中所有field-value對"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > hgetall myhash\n 1) \"name\"\n 2) \"NPC\"\n 3) \"age\"\n 4) \"20\"\n 5) \"country\"\n 6) \"China\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"hkeys key:獲取哈希表key中所有field"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > hkeys myhash\n 1) \"name\"\n 2) \"age\"\n 3) \"country\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"hvals key:獲取哈希表key中所有value"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > hvals myhash\n 1) \"NPC\"\n 2) \"20\"\n 3) \"China\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"hlen key:獲取哈希表key中字段的數量"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > hlen myhash\n 3"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"hdel key field1:刪除哈希表key中一個field"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > hdel myhash age\n (integer) 1"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"List"},{"type":"text","text":" Redis的List類型是簡單的字符串列表,在底層實現上相當於一個鏈表,我們可以在列表的頭部(左邊)或尾部(右邊)添加值。列表最多可以存儲232>-1個元素(4294967295,40多億)。"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"lpush key value1 [value2]:將一個或多個值插入到列表頭部(左邊)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"rpush key value1 [value2]:將一個或多個值插入到列表尾部(右邊)"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > lpush mylist \"a\" \"b\"\n (integer) 2\n > rpush mylist \"c\" \"d\"\n (integer) 4"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Tips"},{"type":"text","text":":執行lpush和rpush命令後返回列表的長度。"}]}]},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"llen key:獲取列表長度"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > llen mylist\n (integer) 4"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"lrange key start end:獲取列表指定範圍內的值"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > lrange mylist 0 -1\n 1) \"b\"\n 2) \"a\"\n 3) \"c\"\n 4) \"d\"\n > lrange mylist 1 -2\n 1) \"a\"\n 2) \"c\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Tips"},{"type":"text","text":":由上述例子我們不難看出lrange命令中的start和end參數都是索引值,其中0代表第一個元素,-1表示最後一個元素。"}]}]},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"lindex key index:通過索引獲取列表中元素"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > lindex mylist 0\n \"b\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"lpop key:移除並獲取列表頭部的值"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"rpop key:移除並獲取列表尾部的值"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > lpop mylist\n \"b\"\n > rpop mylist\n \"d\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"lrem key count value:根據count的值,移除列表中與參數value相等的元素"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > rpush rem \"hello\" \"hello\" \"redis\" \"hello\"\n (integer) 4\n > lrange rem 0 -1\n 1) \"hello\"\n 2) \"hello\"\n 3) \"redis\"\n 4) \"hello\"\n > lrem rem -2 \"hello\"\n (integer) 2\n >lrange rem 0 -1\n 1) \"hello\"\n 2) \"redis\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"count>0:從左往右搜索列表,移除與value相等的元素,數量爲count。 count<0:從右往左搜索列表,移除與value相等的元素,數量爲count的絕對值。 count=0:移除列表中所有與value相等的元素。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Set"},{"type":"text","text":" Set(集合)存儲string類型的值,集合不允許重複元素,但集合裏面的元素沒有先後順序。集合中最大的成員數爲232>-1(4294967295,40多億)。"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"sadd key member1 [member2]:向集合添加一個或多個成員"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > sadd myset1 \"hello\" \"redis\"\n (integer) 2\n > sadd myset1 \"hello\"\n (integer) 0"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Tips"},{"type":"text","text":":當向集合添加重複成員時,返回0"}]}]},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"scard key:獲取集合成員數量"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > scard myset1\n 2"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"smembers key:返回集合中的所有成員"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > smembers myset1\n 1) \"hello\"\n 2) \"redis\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"sdiff key1 [key2]:返回所有給定集合的差集"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > sadd myset2 \"hello\" \"world\"\n (integer) 2\n > sdiff myset1 myset2\n 1) \"redis\"\n > sdiff myset2 myset1\n 1) \"world\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"sinter key1 [key2]:返回所有給定集合的交集"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > sinter myset1 myset2\n 1) \"hello\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"sunion key1 [key2]:返回所有給定集合的並集"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > sunion myset1 myset2\n 1) \"hello\"\n 2) \"redis\"\n 3) \"world\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"spop key:移除並返回集合中的一個隨機元素"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > sadd myset1 \"NPC\"\n (integer) 1\n >spop myset1\n \"redis\"\n >smembers myset1\n 1) \"NPC\"\n 2) \"hello\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"SortedSet"},{"type":"text","text":" 除了無序集合(Set),Redis還提供了有序集合(SortedSet),有序集合不允許重複的成員,且每個不同的成員都關聯一個double類型的分數,redis通過這些分數對成員進行從小到大排序。有序集合有時也被稱爲ZSet,因爲其命令都是以字母Z開頭的。"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"zadd key score1 member1 [score2 member2]:向有序集合中添加一個或多個成員,或者更新已有成員分數"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > zadd myzset 10 \"one\" 20 \"two\" 30 \"three\"\n (integer) 3"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"zcard key:獲取有序集合的成員數量"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > zcard myzset\n 3"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"zscore key member:返回指定成員的分數"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > zscore myzset \"one\"\n 10.0"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"zrange key start end [withscores]:通過索引start和end從小到大"},{"type":"text","text":"返回成員"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"zrevrange key start end [withscores]:通過索引start和end從大到小返回成員"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > zrange myzset 0 -1\n 1) \"one\"\n 2) \"two\"\n 3) \"three\"\n > zrange myzset 0 -1 withscores\n 1) \"one\"\n 2) 10.0\n 3) \"two\"\n 4) 20.0\n 5) \"three\"\n 6) 30.0\n > zrevrange myzset 0 -1 withscores\n 1) \"three\"\n 2) 30.0\n 3) \"two\"\n 4) 20.0\n 5) \"one\"\n 6) 10.0"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"zrank key member:返回指定成員的排名,從小到大排序"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"zrevrank key member:返回指定成員的排名,從大到小排序"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > zrank myzset \"one\"\n 0\n >zrank myzset \"three\"\n 2\n > zrevrank myzset \"one\"\n 2\n > zrevrank myzset \"three\"\n 0"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"zcount key min max:返回分數在min和max之間的成員數量"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > zcount myzset 15 40\n 2"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"zincrby key increment member:將指定成員的分數增加increment"}]}]}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" > zrange myzset 0 -1 withscores\n 1) \"one\"\n 2) 10.0\n 3) \"two\"\n 4) 20.0\n 5) \"three\"\n 6) 30.0\n > zincrby myzset 40 \"one\"\n 50.0\n > zrange myzset 0 -1 withscores\n 1) \"two\"\n 2) 20.0\n 3) \"three\"\n 4) 30.0\n 5) \"one\"\n 6) 50.0"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章