一、簡介
redis是一個key-value存儲系統。
和Memcached類似,它支持存儲的value類型相對更多,
包括string(字符串)、list(鏈表)、set(集合)和zset(有序集合)。
這些數據類型都支持push/pop、add/remove及取交集並集和差集及更豐富的操作,而且這些操作都是原子性的。
在此基礎上,redis支持各種不同方式的排序。與memcached一樣,爲了保證效率,數據都是緩存在內存中。
區別的是redis會週期性的把更新的數據寫入磁盤或者把修改操作寫入追加的記錄文件,並且在此基礎上實現了master-slave(主從)同步。
Redis 是一個高性能的key-value數據庫。
redis的出現,很大程度補償了memcached這類key/value存儲的不足,在部 分場合可以對關係數據庫起到很好的補充作用。
它提供了Python,Ruby,Erlang,PHP客戶端,使用很方便。
Memcache
l 內存緩存服務,緩存數據保存在內存中,一旦斷電重啓,數據將丟失
mangoDB
l 開源免費的NOSQL 數據庫,提供數據持久化服務,以文檔的形式提供數據組織方式,而不是表
Redis
l 開源免費的 NOSQL數據庫,提供數據持久化服務,即能實現內存緩存服務,也能提供數據結構服務
取代MySQL 自建索引
l 用來彌補關係型數據的不足
二、安裝(安裝前先安裝gcc)
1、默認安裝位置
[root@localhost ~]# wget http://download.redis.io/releases/redis-2.8.6.tar.gz
[root@localhost ~]# tar xzf redis-2.8.6.tar.gz
[root@localhost ~]# cd redis-2.8.6
[root@localhost ~]# make
#不指定安裝位置,則會把redis的可執行文件安裝到 redis-2.8.6/src/目錄下
2、指定安裝位置
[root@localhost ~]# tar xzf redis-2.8.6.tar.gz
[root@localhost ~]# cd redis-2.8.6
[root@localhost ~]# make
[root@localhost ~]# make PREFIX=/usr/local/redis install
#指定安裝位置,如果沒有指定安裝位置PREFIX=/usr/local/redis,則make install會把redis安裝到/usr/local/bin/目錄下
[root@localhost ~]# mkdir /usr/local/redis/etc
[root@localhost ~]# cp /root/redis-2.8.6/redis.conf /usr/local/redis/etc/
3、安裝的可執行文件的作用
redis-server 服務器端
redis-cli 客戶端
redis-benchmark 調試
redis-check-dump 數據導出
redis-check-aof 數據導入
三、啓動與關閉
1、 啓動redis服務
路徑/redis-server 配置文件
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
注意:需要修改配置文件
[root@localhost redis]# vi /usr/local/redis/etc/redis.conf
daemonize no 改爲
daemonize yes #後臺啓動
端口 6379
[root@localhost redis]# /usr/local/redis/bin/redis-cli
#客戶端連接
-h IP : 連接指定的redis服務器
-p 6379: 指定redis服務器的端口
-a 密碼: 使用密碼登錄
-n 數據庫號: 指定連接哪個數據庫
2、 關閉redis
[root@localhost ~]# /usr/local/redis/bin/redis-cli shutdown
或
[root@localhost ~]# pkill -9 redis
四、配置文件
[root@localhost redis]# vi /usr/local/redis/etc/redis.conf
#是否以後臺進程運行,默認爲no,如果需要以後臺進程運行則改爲yes
daemonize no
#如果以後臺進程運行的話,就需要指定pid,你可以在此自定義redis.pid文件的位置。
pidfile /var/run/redis.pid
#接受連接的端口號,如果端口是0則redis將不會監聽TCP socket連接
port 6379
# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for incoming connections.
#
# bind 127.0.0.1
# Specify the path for the unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /tmp/redis.sock
# unixsocketperm 755
#連接超時時間,單位秒。(0 to disable)?
timeout 300000000
#日誌級別,默認是verbose(詳細),各種日誌級別:
#debug:很詳細的信息,適合開發和測試
#verbose:包含許多不太有用的信息,但比debug要清爽一些(many rarely useful info, but not a mess like #the debug level)
#notice:比較適合生產環境
#warning:警告信息
loglevel verbose
#指定log文件的名字,默認是stdout。stdout會讓redis把日誌輸出到標準輸出。但是如果使用stdout而又以後臺進#程的方式運行redis,則日誌會輸出到/dev/null
logfile stdout
#'syslog-enabled'設置爲yes會把日誌輸出到系統日誌,默認是no
# syslog-enabled no
#指定syslog的標示符,如果'syslog-enabled'是no,則這個選項無效。
# syslog-ident redis
#指定syslog 設備(facility), 必須是USER或者LOCAL0到LOCAL7.
# syslog-facility local0
#設置數據庫數目。默認的數據庫是DB 0。可以通過SELECT <dbid>來選擇一個數據庫,dbid是[0,'databases'-1]的數字
databases 16
################## 快照#################################
#
# 硬盤上保存數據:
#
# save <seconds> <changes>
#
# <seconds>和<changes>都滿足時就會觸發數據保存動作。
#
#
# 以下面的例子來說明:
# 過了900秒並且有1個key發生了改變 就會觸發save動作
# 過了300秒並且有10個key發生了改變 就會觸發save動作
# 過了60秒並且至少有10000個key發生了改變 也會觸發save動作
#
# 注意:如果你不想讓redis自動保存數據,那就把下面的配置註釋掉!
save 900 1
save 300 10
save 60 10000
#存儲數據時是否壓縮數據。默認是yes。
rdbcompression yes
# 保存dump數據的文件名
dbfilename dump.rdb
# 工作目錄.
#
# 數據會被持久化到這個目錄下的‘dbfilename’指定的文件中。
#
#
# 注意,這裏指定的必須是目錄而不能是文件。
dir ./
######## REPLICATION(複製,冗餘)#################################
# Master-Slave replication. 使用slaveof把一個 Redis 實例設置成爲另一個Redis server的從庫(熱備). 注意: #配置只對當前slave有效。
# 因此可以把某個slave配置成使用不同的時間間隔來保存數據或者監聽其他端口等等。
#命令格式:
# slaveof <masterip> <masterport>
#如果master有密碼保護,則在slave與master進行數據同步之前需要進行密碼校驗,否則master會拒絕slave的請#求。
#
# masterauth <master-password>
#當slave丟失與master的連接時,或者slave仍然在於master進行數據同步時(還沒有與master保持一致),#slave可以有兩種方式來響應客戶端請求:
#
# 1) 如果 slave-serve-stale-data 設置成 'yes' (the default) slave會仍然響應客戶端請求,此時可能會有問題。
#
# 2) 如果 slave-serve-stale data設置成 'no' slave會返回"SYNC with master in progress"這樣的錯誤信息。 但 INFO 和SLAVEOF命令除外。
#
slave-serve-stale-data yes
############### 安全 ###################################
# 需要客戶端在執行任何命令之前指定 AUTH <PASSWORD>
#
# requirepass foobared
# 命令重命名.
#
#
# 例如:
#
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#
# 同樣可以通過把一個命令重命名爲空串來徹底kill掉這個命令,比如:
#
# rename-command CONFIG ""
#################### 限制 ####################################
# 設置最大連接數. 默認沒有限制, '0' 意味着不限制.
#
# maxclients 128
#最大可使用內存。如果超過,Redis會試圖刪除EXPIRE集合中的keys,具體做法是:Redis會試圖釋放即將過期的#keys,而保護還有很長生命週期的keys。
#
#如果這樣還不行,Redis就會報錯,但像GET之類的查詢請求還是會得到響應。
#
#警告:如果你想把Redis視爲一個真正的DB的話,那不要設置<maxmemory>,只有你只想把Redis作爲cache或者
#有狀態的server('state' server)時才需要設置。
#
# maxmemory <bytes>
#內存清理策略:如果達到了maxmemory,你可以採取如下動作:
#
# volatile-lru -> 使用LRU算法來刪除過期的set
# allkeys-lru -> 刪除任何遵循LRU算法的key
# volatile-random ->隨機地刪除過期set中的key
# allkeys->random -> 隨機地刪除一個key
# volatile-ttl -> 刪除最近即將過期的key(the nearest expire time (minor TTL))
# noeviction -> 根本不過期,寫操作直接報錯
#
#
# 默認策略:
#
# maxmemory-policy volatile-lru
# 對於處理redis內存來說,LRU和minor TTL算法不是精確的,而是近似的(估計的)算法。所以我們會檢查某些樣本#來達到內存檢查的目的。默認的樣本數是3,你可以修改它。
#
# maxmemory-samples 3
################# APPEND ONLY MODE ###############################
#默認情況下,Redis會異步的把數據保存到硬盤。如果你的應用場景允許因爲系統崩潰等極端情況而導致最新數據丟失#的話,那這種做法已經很ok了。否則你應該打開‘append only’模式,開啓這種模式後,Redis會在#appendonly.aof文件中添加每一個寫操作,這個文件會在Redis啓動時被讀取來在內存中重新構建數據集。
#
#注意:如果你需要,你可以同時開啓‘append only’模式和異步dumps模式(你需要註釋掉上面的‘save’表達式來禁#止dumps),這種情況下,Redis重建數據集時會優先使用appendonly.aof而忽略dump.rdb
#
appendonly no
# append only 文件名 (默認: "appendonly.aof")
# appendfilename appendonly.aof
# 調用fsync()函數通知操作系統立刻向硬盤寫數據
#
# Redis支持3中模式:
#
# no:不fsync, 只是通知OS可以flush數據了,具體是否flush取決於OS.性能更好.
# always: 每次寫入append only 日誌文件後都會fsync . 性能差,但很安全.
# everysec: 沒間隔1秒進行一次fsync. 折中.
#
# 默認是 "everysec"
# appendfsync always
appendfsync everysec
# appendfsync no
# 當AOF fsync策略被設置爲always或者everysec並且後臺保存進程(saving process)正在執行大量I/O操作時
# Redis可能會在fsync()調用上阻塞過長時間
#
no-appendfsync-on-rewrite no
# append only 文件的自動重寫
# 當AOF 日誌文件即將增長到指定百分比時,Redis可以通過調用BGREWRITEAOF 來自動重寫append only文件。
#
# 它是這麼幹的:Redis會記住最近一次重寫後的AOF 文件size。然後它會把這個size與當前size進行比較,如果當前# size比指定的百分比大,就會觸發重寫。同樣,你需要指定AOF文件被重寫的最小size,這對避免雖然百分比達到了# 但是實際上文件size還是很小(這種情況沒有必要重寫)卻導致AOF文件重寫的情況很有用。
#
#
# auto-aof-rewrite-percentage 設置爲 0 可以關閉AOF重寫功能
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
################## SLOW LOG ###################################
# Redis slow log用來記錄超過指定執行時間的查詢。
#
# 你可以指定兩個參數:一個是慢查詢的閥值,單位是毫秒;另外一個是slow log的長度,相當於一個隊列。
# 負數則關閉slow log,0則會導致每個命令都被記錄
slowlog-log-slower-than 10000
# 不設置會消耗過多內存,所以還是要設置一下。可以使用SLOWLOG RESET命令來回收slow log使用的內存
slowlog-max-len 1024
################ 虛擬內存 ###############################
#使用redis 就別用虛擬內存了,絕對不是一個好主意,加個機器吧,所以這裏不翻譯啦!!
### WARNING! Virtual Memory is deprecated in Redis 2.4
### The use of Virtual Memory is strongly discouraged.
# Virtual Memory allows Redis to work with datasets bigger than the actual
# amount of RAM needed to hold the whole dataset in memory.
# In order to do so very used keys are taken in memory while the other keys
# are swapped into a swap file, similarly to what operating systems do
# with memory pages.
#
# To enable VM just set 'vm-enabled' to yes, and set the following three
# VM parameters accordingly to your needs.
vm-enabled no
# vm-enabled yes
# This is the path of the Redis swap file. As you can guess, swap files
# can't be shared by different Redis instances, so make sure to use a swap
# file for every redis process you are running. Redis will complain if the
# swap file is already in use.
#
# The best kind of storage for the Redis swap file (that's accessed at random)
# is a Solid State Disk (SSD).
#
# *** WARNING *** if you are using a shared hosting the default of putting
# the swap file under /tmp is not secure. Create a dir with access granted
# only to Redis user and configure Redis to create the swap file there.
vm-swap-file /tmp/redis.swap
# vm-max-memory configures the VM to use at max the specified amount of
# RAM. Everything that deos not fit will be swapped on disk *if* possible, that
# is, if there is still enough contiguous space in the swap file.
#
# With vm-max-memory 0 the system will swap everything it can. Not a good
# default, just specify the max amount of RAM you can in bytes, but it's
# better to leave some margin. For instance specify an amount of RAM
# that's more or less between 60 and 80% of your free RAM.
vm-max-memory 0
# Redis swap files is split into pages. An object can be saved using multiple
# contiguous pages, but pages can't be shared between different objects.
# So if your page is too big, small objects swapped out on disk will waste
# a lot of space. If you page is too small, there is less space in the swap
# file (assuming you configured the same number of total swap file pages).
#
# If you use a lot of small objects, use a page size of 64 or 32 bytes.
# If you use a lot of big objects, use a bigger page size.
# If unsure, use the default :)
vm-page-size 32
# Number of total memory pages in the swap file.
# Given that the page table (a bitmap of free/used pages) is taken in memory,
# every 8 pages on disk will consume 1 byte of RAM.
#
# The total swap size is vm-page-size * vm-pages
#
# With the default of 32-bytes memory pages and 134217728 pages Redis will
# use a 4 GB swap file, that will use 16 MB of RAM for the page table.
#
# It's better to use the smallest acceptable value for your application,
# but the default is large in order to work in most conditions.
vm-pages 134217728
# Max number of VM I/O threads running at the same time.
# This threads are used to read/write data from/to swap file, since they
# also encode and decode objects from disk to memory or the reverse, a bigger
# number of threads can help with big objects even if they can't help with
# I/O itself as the physical device may not be able to couple with many
# reads/writes operations at the same time.
#
# The special value of 0 turn off threaded I/O and enables the blocking
# Virtual Memory implementation.
vm-max-threads 4
################高級配置###############################
# Hashes are encoded in a special way (much more memory efficient) when they
# have at max a given numer of elements, and the biggest element does not
# exceed a given threshold. You can configure this limits with the following
# configuration directives.
hash-max-zipmap-entries 512
hash-max-zipmap-value 64
# Similarly to hashes, small lists are also encoded in a special way in order
# to save a lot of space. The special representation is only used when
# you are under the following limits:
list-max-ziplist-entries 512
list-max-ziplist-value 64
# Sets have a special encoding in just one case: when a set is composed
# of just strings that happens to be integers in radix 10 in the range
# of 64 bit signed integers.
# The following configuration setting sets the limit in the size of the
# set in order to use this special memory saving encoding.
set-max-intset-entries 512
# Similarly to hashes and lists, sorted sets are also specially encoded in
# order to save a lot of space. This encoding is only used when the length and
# elements of a sorted set are below the following limits:
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in
# order to help rehashing the main Redis hash table (the one mapping top-level
# keys to values). The hash table implementation redis uses (see dict.c)
# performs a lazy rehashing: the more operation you run into an hash table
# that is rhashing, the more rehashing "steps" are performed, so if the
# server is idle the rehashing is never complete and some more memory is used
# by the hash table.
#
# The default is to use this millisecond 10 times every second in order to
# active rehashing the main dictionaries, freeing memory when possible.
#
# If unsure:
# use "activerehashing no" if you have hard latency requirements and it is
# not a good thing in your environment that Redis can reply form time to time
# to queries with 2 milliseconds delay.
#
# use "activerehashing yes" if you don't have such hard requirements but
# want to free memory asap when possible.
activerehashing yes
################## INCLUDES ###################################
# Include one or more other config files here. This is useful if you
# have a standard template that goes to all redis server but also need
# to customize a few per-server settings. Include files can include
# other files, so use this wisely.
#
# include /path/to/local.conf
# include /path/to/other.conf
五、Redis的數據類型
1. 共計5種類型:string(字符串)、hash(哈希表) list(雙向隊列)、set(集合)和zset(有序集合)
2. String(字串類型)
String是最簡單的類型,一個Key對應一個Value,string類型是二進制安全的。Redis的string可以包含任何數據,比如jpg圖片或者序列化的對象。
--------------------------------------------
1)set 鍵 "值"
設置一個鍵和值,鍵存在則覆蓋,返回ok
127.0.0.1:6379> set name liming
OK
2)get 鍵
獲取一個鍵的值,返回值
127.0.0.1:6379> get name
"liming"
3) setnx 鍵 值
只有當該鍵不存在時設置一個鍵的值,若鍵已存在則返回0表示失敗(防止覆蓋),
127.0.0.1:6379> setnx age 18
(integer) 1
127.0.0.1:6379> setnx age 18
(integer) 0
3) setex 鍵 [有效時間] 值
設置一個指定有效期的鍵和值(單位秒)。不寫有效時間則表示永久有效,等價於set
127.0.0.1:6379> setex movie 30 canglaoshi
OK
127.0.0.1:6379> ttl movie //獲取鍵的有效時間
(integer) 26
127.0.0.1:6379> ttl movie
(integer) 20
127.0.0.1:6379> get movie
"canglaoshi"
127.0.0.1:6379> ttl movie
(integer) -2
127.0.0.1:6379> get movie
(nil)
4)ttl 鍵
以秒爲單位,返回給定 key 的剩餘生存時間
當 key 不存在時,返回 -2 。
當 key 存在但沒有設置剩餘生存時間時,返回 -1 。
否則,以秒爲單位,返回 key 的剩餘生存時間。
5) setrange 鍵 位置 子字串
替換子字符串 (替換長度由子子串長度決定)
127.0.0.1:6379> set key1 "hello world"
OK
127.0.0.1:6379> get key1
"hello world"
127.0.0.1:6379> setrange key1 6 liming
(integer) 12
127.0.0.1:6379> get key1
"hello liming"
#將key1鍵對應值的第6個位置開始替換(字符串位置從0開始計算)
6) mset 鍵1 值1 鍵2 值2 鍵3 值3 ....
批量設置鍵和值,成功則返回ok
127.0.0.1:6379> mset name1 lm name2 sc name3 zjj
OK
7) mget 鍵1 鍵2 鍵3....
批量獲取值
127.0.0.1:6379> mget name1 name2 name3
1) "lm"
2) "sc"
3) "zjj"
7) msetnx 鍵1 值1 鍵2 值2 鍵3 值3 ....
批量設置不存在的鍵和值,成功則返回ok
9) getset 鍵 新值
獲取原值,並設置新值
127.0.0.1:6379> set name "shen chao"
OK
127.0.0.1:6379> get name
"shen chao"
127.0.0.1:6379> getset name "liming"
"shen chao"
127.0.0.1:6379> get name
"liming"
10) getrange 鍵 0 4
獲取指定範圍的值(獲取指定0到4位置上的值,字符串位置從0開始計算)
127.0.0.1:6379> getrange key1 0 4
"hello"
11) incr 鍵
指定鍵的值做加1操作,返回加後的結果(只能加數字)。
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> incr age
(integer) 19
127.0.0.1:6379> get age
"19"
12) incrby 鍵 m
//其中m可以是正整數或負整數
加指定值,鍵不存在時候會設置鍵
127.0.0.1:6379> incrby age 10
(integer) 29
127.0.0.1:6379> get age
"29"
127.0.0.1:6379> incrby age -5
(integer) 24
13) decr 鍵
指定鍵的值做減1操作,返回減後的結果。
127.0.0.1:6379> decr age
(integer) 23
127.0.0.1:6379> get age
"23"
14) decrby 鍵 n
//其中n可以是正整數或負整數
設置某個鍵減上指定值
127.0.0.1:6379> decrby age 5
(integer) 18
127.0.0.1:6379> decrby age -10
(integer) 28
127.0.0.1:6379> get age
"28"
15)append 鍵 追加字串
給指定key的字符串追加value,返回新字符串值的長度
127.0.0.1:6379> append name1 " have a hot body!"
(integer) 19
127.0.0.1:6379> get name1
"lm have a hot body!"
16) strlen 鍵名
strlen求一個鍵對應的值(字串)的長度
127.0.0.1:6379> strlen name1
(integer) 19
17)del命令:刪除一個鍵
127.0.0.1:6379> del name3
(integer) 1
127.0.0.1:6379> get name3
(nil)
3. hashs類型
#注意:redis中沒有表概念,所有的數據都存入鍵中。
string鍵類型:所有的值(可以是任何數據類型)都保存在一個鍵當中,放在一個內存塊中
hashs鍵類型:所有的值也都保存在一個鍵當中,只是放在不同的內存塊中,每個塊稱作字段
1)hset key field value
設置一個鍵,在鍵中保存字段和值
hset 哈希集(鍵) 字段 值
127.0.0.1:6379> hset user1 name4 ysm
(integer) 1
127.0.0.1:6379> keys * //查看庫中所有的鍵
1) "aa"
2) "name"
3) "name1"
4) "user1"
5) "name2"
6) "age"
7) "key1"
2)>hsetnx 鍵 字段 值
設置一個鍵中,不存在的字段和值。如果字段存在則報錯(成功返回1,失敗返回0)
127.0.0.1:6379> hsetnx user1 name1 lm
(integer) 1
127.0.0.1:6379> hsetnx user1 name1 sc
(integer) 0 //報錯
127.0.0.1:6379> hget user1 name1
"lm" //內容沒有更新
3)hmset 鍵 字段1 值1 字段2 值2 …
在一個鍵中,批量設置字段
127.0.0.1:6379> hmset user2 name liming age 36 interest AV-Girl
OK
4)hget 鍵 字段
獲取鍵中的一個指定字段的值
127.0.0.1:6379> hget user1 name1
"lm"
127.0.0.1:6379> hget user2 interest
"AV-Girl"
5)hmget 鍵 字段1 [字段2…]
獲取鍵中一個或多個字段的值
127.0.0.1:6379> hmget user2 name age interest
1) "liming"
2) "36"
3) "AV-Girl"
6) hexists :判斷指定的字段是否存在於鍵中
例如:
127.0.0.1:6379> HEXISTS user2 age
(integer) 1 //存在
127.0.0.1:6379> HEXISTS user1 age
(integer) 0 //不存在
7) hlen :獲取鍵中的字段數量
127.0.0.1:6379> hlen user2
(integer) 3 //user2鍵中有3個字段
8)hkeys :獲取鍵中的所有字段名
127.0.0.1:6379> hkeys user2
1) "name"
2) "age"
3) "interest"
9)hvals:獲取鍵中所有字段的值
127.0.0.1:6379> hvals user2
1) "liming"
2) "36"
3) "AV-Girl"
10) hgetall :獲取鍵中的所有字段和值
127.0.0.1:6379> hgetall user2
1) "name"
2) "liming"
3) "age"
4) "36"
5) "interest"
6) "AV-Girl"
11)hincrby:將鍵中指定字段的值,增加指定的數字
127.0.0.1:6379> hincrby user2 age 5
(integer) 41
127.0.0.1:6379> HINCRBY user2 name 5
(error) ERR hash value is not an integer //值不是數字的字段,不能加數字
12)hdel 鍵 字段1 字段2
刪除鍵中的一個或多個字段
127.0.0.1:6379> hdel user2 age interest
(integer) 2
127.0.0.1:6379> hkeys user2
1) "name"
//刪除一個鍵,還是要使用del命令
4. list類型(雙向鏈表結構)
List是一個鏈表結構,主要功能是push、pop、獲取一個範圍的所有值等等,操作中key理解爲鏈表的名字。Redis的list類型其實就是一個每個子元素都是string類型的雙向鏈表。
1)lpush 鍵 值1 [值2…]
從隊列左邊向隊列寫入一個或多個值(認爲隊列的左面爲隊列頭,右邊爲隊列尾)
127.0.0.1:6379> lpush list1 1
(integer) 1
127.0.0.1:6379> lpush list1 2
(integer) 2
127.0.0.1:6379> lpush list1 3
(integer) 3
127.0.0.1:6379> lpush list1 4
(integer) 4
127.0.0.1:6379> lpush list2 one two three four
(integer) 4
2)lrange 鍵 起始下標 終止下標
從隊列中獲取指定的返回值(從隊列左邊向右獲取)
下標:0代表隊列中第一個元素,1代表第二個元素,依次類推
-1代表隊列中最後一個元素,-2代表倒數第二個元素
127.0.0.1:6379> LRANGE list1 0 -1
1) "4" //4是從左面寫入隊列的最後一個值,所以在隊列的開頭
2) "3"
3) "2"
4) "1" //1是從左面寫入隊列的第一個值,所以直接放到了隊列尾。
127.0.0.1:6379> LRANGE list2 0 -1
1) "four"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> LRANGE list1 0 1
1) "4"
2) "3"
127.0.0.1:6379> LRANGE list2 -4 3
1) "four" //-4代表從隊列右邊數第四個元素
2) "three"
3) "two"
4) "one" //3代表從隊列左邊數第四個元素
3)rpush 鍵 值1 [值2…]
從隊列右邊向隊列寫入一個或多個值
127.0.0.1:6379> RPUSH list3 1 2 3 4
(integer) 4
127.0.0.1:6379> LRANGE list3 0 -1
1) "1" //從隊列右邊向隊列寫入值,第一個值就會寫到隊列的開頭
2) "2"
3) "3"
4) "4"
4)linsert 鍵 before|after 原值 新值
在隊列中指定元素之前或之後插入新值
127.0.0.1:6379> LINSERT list3 before 3 hello
(integer) 5
127.0.0.1:6379> LRANGE list3 0 -1
1) "1"
2) "2"
3) "hello"
4) "3"
5) "4"
5)lset 鍵 下標 新值
給隊列中指定元素設定新值
127.0.0.1:6379> lset list3 2 "5"
OK
127.0.0.1:6379> LRANGE list3 0 -1
1) "1"
2) "2"
3) "5"
4) "3"
5) "4"
6)lrem 鍵 n 指定值
從隊列中刪除n個值爲“指定值”的元素
n > 0 從隊列頭向尾刪除n個元素
n < 0 從隊列尾向頭刪除n個元素
n = 0 刪除所有值爲“指定值”的元素
127.0.0.1:6379> rpush list4 hello 1 hello 2 hello 3 hello
(integer) 7
127.0.0.1:6379> lrange list4 0 -1
1) "hello"
2) "1"
3) "hello"
4) "2"
5) "hello"
6) "3"
7) "hello"
127.0.0.1:6379> lrem list4 -2 hello //刪除後兩個hello
(integer) 2
127.0.0.1:6379> lrange list4 0 -1
1) "hello"
2) "1"
3) "hello"
4) "2"
5) "3"
127.0.0.1:6379> lrem list4 0 hello //刪除所有hello
(integer) 2
127.0.0.1:6379> lrange list4 0 -1
1) "1"
2) "2"
3) "3"
7)ltrim 鍵 起始下標 結束下標
修剪隊列,讓隊列只保留指定指定範圍內的元素
127.0.0.1:6379> RPUSH list5 1 2 3 4
(integer) 4
127.0.0.1:6379> ltrim list5 1 2
OK
127.0.0.1:6379> lrange list5 0 -1
1) "2"
2) "3"
8)lpop 鍵
從指定的隊列左面移除一個值
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> lpop list1
"4"
127.0.0.1:6379> lrange list1 0 -1
1) "3"
2) "2"
3) "1"
9)rpop 鍵
從指定隊列的右邊移除一個值
127.0.0.1:6379> lrange list1 0 -1
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> rpop list1
"1"
127.0.0.1:6379> lrange list1 0 -1
1) "3"
2) "2"
10)rpoplpush 源隊列 目標隊列
移除源隊列的最後一個元素,並把該元素寫入目標隊列
127.0.0.1:6379> lrange list1 0 -1
1) "3"
2) "2"
127.0.0.1:6379> lrange list5 0 -1
1) "2"
2) "3"
127.0.0.1:6379> RPOPLPUSH list1 list5
"2"
127.0.0.1:6379> lrange list1 0 -1
1) "3"
127.0.0.1:6379> lrange list5 0 -1
1) "2"
2) "2"
3) "3"
11)lindex 鍵 下標
獲取隊列中指定下標元素的值
127.0.0.1:6379> lrange list2 0 -1
1) "four"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> lindex list2 1
"three"
127.0.0.1:6379> lindex list2 3
"one"
12)llen 鍵
獲得隊列的長度
127.0.0.1:6379> llen list2
(integer) 4
5.sets類型和操作
Set是集合,它是string類型的無序集合。對集合我們可以取並集、交集、差集。通過這些操作我們可以實現社交網站中的好友推薦和blog的tag功能。(值不能重複!!!!!!)
1) sadd 鍵 值1[值2…]
添加一個或多個元素到集合中
127.0.0.1:6379> sadd mset1 1
(integer) 1
127.0.0.1:6379> sadd mset1 2 3 4
(integer) 3
2) smembers 鍵
獲取集合裏面所有的元素
127.0.0.1:6379> smembers mset1
1) "1"
2) "2"
3) "3"
4) "4"
3) srem 鍵 值1[值2…]
從集合中刪除指定的一個或多個元素
127.0.0.1:6379> srem mset1 3 4
(integer) 2
127.0.0.1:6379> smembers mset1
1) "1"
2) "2"
(刪除鍵,依然使用“del 鍵” 命令)
4) spop 鍵
隨機從集合中刪除一個元素,並返回
127.0.0.1:6379> sadd mset2 4 5 6 7 8
(integer) 5
127.0.0.1:6379> spop mset2
"4"
127.0.0.1:6379> spop mset2
"5"
127.0.0.1:6379> spop mset2
"8"
127.0.0.1:6379> smembers mset2
1) "6"
2) "7"
5) srandmember 鍵 值
隨機返回集合中一個元素,但不刪除
127.0.0.1:6379> sadd mset3 4 5 6 7 8
(integer) 5
127.0.0.1:6379> srandmember mset3
"5"
127.0.0.1:6379> srandmember mset3
"5"
127.0.0.1:6379> srandmember mset3
"4"
6) scard 鍵
獲取集合裏面元素個數
127.0.0.1:6379> scard mset1
(integer) 2
7) sismember 鍵 值
確定一個指定的值是否是集合中的元素
127.0.0.1:6379> smembers mset1
1) "1"
2) "2"
127.0.0.1:6379> sismember mset1 3
(integer) 0
127.0.0.1:6379> sismember mset1 1
(integer) 1
8) sdiff 集合1 集合2
返回集合1與集合2的差集。以集合1爲主
127.0.0.1:6379> sadd mset4 1 2 3
(integer) 3
127.0.0.1:6379> sadd mset5 2 3 4
(integer) 3
127.0.0.1:6379> sdiff mset4 mset5
1) "1"
9) sdiffstore 新集合 集合1 集合2
返回集合1和集合2的差集,並把結果存入新集合
127.0.0.1:6379> sadd mset4 1 2 3
(integer) 3
127.0.0.1:6379> sadd mset5 2 3 4
(integer) 3
127.0.0.1:6379> sdiffstore mset6 mset5 mset4
(integer) 1 //返回值爲1 ,證明成功
127.0.0.1:6379> smembers mset6
1) "4" //結果存入了mset6,值爲4
10) sinter 集合1 集合2
獲得兩個集合的交集
127.0.0.1:6379> smembers mset4
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> smembers mset5
1) "2"
2) "3"
3) "4"
127.0.0.1:6379> sinter mset4 mset5
1) "2"
2) "3"
11) sinterstore 新集合 集合1 集合2
獲得集合1和集合2的交集,並把結果存入新集合
127.0.0.1:6379> sinterstore mset7 mset4 mset5
(integer) 2
127.0.0.1:6379> smembers mset7
1) "2"
2) "3"
12) sunion 集合1 集合2
獲得指定集合的並集
127.0.0.1:6379> sunion mset4 mset5
1) "1"
2) "2"
3) "3"
4) "4"
13) sunionstore 新集合 集合1 集合2
獲得指定集合的並集,並把結果保存如新集合
127.0.0.1:6379> sunionstore mset8 mset4 mset5
(integer) 4
127.0.0.1:6379> smembers mset8
1) "1"
2) "2"
3) "3"
4) "4"
14) smove 源集合 目標集合 值
將指定的值從源集合移動到目標集合
127.0.0.1:6379> smembers mset1
1) "1"
2) "2"
127.0.0.1:6379> smembers mset2
1) "6"
2) "7"
127.0.0.1:6379> smove mset1 mset2 1
(integer) 1
127.0.0.1:6379> smembers mset1
1) "2"
127.0.0.1:6379> smembers mset2
1) "1"
2) "6"
3) "7"