首頁 > 軟體

redis部署及各種資料型別使用命令詳解

2022-03-06 10:00:48

編譯安裝redis

開始部署

[root@mcw01 ~]$ ls
anaconda-ks.cfg  mongodb-linux-x86_64-3.2.8.tgz
[root@mcw01 ~]$ wget http://download.redis.io/releases/redis-4.0.10.tar.gz
--2022-03-05 18:27:02--  http://download.redis.io/releases/redis-4.0.10.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1738465 (1.7M) [application/octet-stream]

Saving to: ‘redis-4.0.10.tar.gz'

100%[====================>] 1,738,465   63.4KB/s   in 25s    

2022-03-05 18:27:27 (68.2 KB/s) - ‘redis-4.0.10.tar.gz' saved [1738465/1738465]

anaconda-ks.cfg  mongodb-linux-x86_64-3.2.8.tgz  redis-4.0.10.tar.gz
[root@mcw01 ~]$ tar xf redis-4.0.10.tar.gz 
anaconda-ks.cfg  mongodb-linux-x86_64-3.2.8.tgz  redis-4.0.10  redis-4.0.10.tar.gz
[root@mcw01 ~]$ cd redis-4.0.10/
[root@mcw01 ~/redis-4.0.10]$ ls  #有makefile檔案,就不需要./configure設定
00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  tests
BUGS             deps     MANIFESTO  runtest          sentinel.conf     utils
CONTRIBUTING     INSTALL  README.md  runtest-cluster  src
[root@mcw01 ~/redis-4.0.10]$ make

報錯 gcc命令沒有找到:

cd hiredis && make static
make[3]: Entering directory `/root/redis-4.0.10/deps/hiredis'
gcc -std=c99 -pedantic -c -O3 -fPIC  -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb  net.c
make[3]: gcc: Command not found
make[3]: *** [net.o] Error 127
make[3]: Leaving directory `/root/redis-4.0.10/deps/hiredis'
make[2]: *** [hiredis] Error 2
make[2]: Leaving directory `/root/redis-4.0.10/deps'
make[1]: [persist-settings] Error 2 (ignored)
    CC adlist.o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/root/redis-4.0.10/src'
make: *** [all] Error 2

#解決編譯安裝所需的依賴環境繼續部署

um install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y
然後重新執行:
[root@mcw01 ~/redis-4.0.10]$ make  #發現報錯沒有某個目錄或檔案。在這之前編譯失敗了,目錄被改變了部分,所以直接刪除重新解壓,然後make編譯
......
Hint: It's a good idea to run 'make test' ;)
make[1]: Leaving directory `/root/redis-4.0.10/src'
[root@mcw01 ~/redis-4.0.10]$ ls
00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  tests
BUGS             deps     MANIFESTO  runtest          sentinel.conf     utils
CONTRIBUTING     INSTALL  README.md  runtest-cluster  src
[root@mcw01 ~/redis-4.0.10]$ make install 
cd src && make install
make[1]: Entering directory `/root/redis-4.0.10/src'
    CC Makefile.dep
    INSTALL install
[root@mcw01 ~/redis-4.0.10]$ 
[root@mcw01 ~/redis-4.0.10]$ redis-c   #當編譯好之後,就可以命令補全,也就是已經設定好環境變數了
redis-check-aof  redis-check-rdb  redis-cli        
[root@mcw01 ~/redis-4.0.10]$ which redis-server  #預設編譯安裝到/usr/local下去了
/usr/local/bin/redis-server
[root@mcw01 ~/redis-4.0.10]$

redis伺服器端執行和使用者端連線

[root@mcw01 ~]$ redis-server  #安裝好之後,直接執行這個命令
24270:C 05 Mar 18:43:08.857 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
24270:C 05 Mar 18:43:08.857 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=24270, just started
24270:C 05 Mar 18:43:08.857 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.10 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 24270
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |    `-._`-._        _.-'_.-'    |                                  
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               
24270:M 05 Mar 18:43:08.880 # Server initialized
24270:M 05 Mar 18:43:08.880 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
24270:M 05 Mar 18:43:08.880 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
24270:M 05 Mar 18:43:08.880 * Ready to accept connections
使用者端連線:
[root@mcw01 ~]$ redis-cli 127.0.0.1 6379  ^C #我再開一個視窗,預設連線的ip 埠
[root@mcw01 ~]$ redis-cli  #連上之後,ping命令,返回pong,說明連線成功的
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

redis設定

[root@mcw01 ~]$ ln -s redis-4.0.10  redis 
[root@mcw01 ~]$ ls
anaconda-ks.cfg  dump.rdb  mongodb-linux-x86_64-3.2.8.tgz  redis  redis-4.0.10  redis-4.0.10.tar.gz
[root@mcw01 ~]$ ls redis
00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  tests
BUGS             deps     MANIFESTO  runtest          sentinel.conf     utils
CONTRIBUTING     INSTALL  README.md  runtest-cluster  src
[root@mcw01 ~]$ cd redis
[root@mcw01 ~/redis]$ egrep -v "^#|^$" redis.conf >mcwRedis.conf
[root@mcw01 ~/redis]$ cat mcwRedis.conf 
bind 127.0.0.1   
protected-mode yes  #保護模式開啟,並且繫結ip是本機,應該是不支援遠端存取,只支援本機存取。
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
[root@mcw01 ~/redis]$

redis簡單設定並啟動

[root@mcw01 ~/redis]$ cat mcwRedis.conf 
bind 10.0.0.11
protected-mode yes
port 6379
requirepass mcw123 #設定認證密碼
daemonize yes #後臺守護行程的方式開啟
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
[root@mcw01 ~/redis]$ 
[root@mcw01 ~/redis]$ ls  
00-RELEASENOTES  COPYING  Makefile       README.md   runtest-cluster   src
BUGS             deps     MANIFESTO      redis.conf  runtest-sentinel  tests
CONTRIBUTING     INSTALL  mcwRedis.conf  runtest     sentinel.conf     utils
[root@mcw01 ~/redis]$ redis-server mcwRedis.conf  #指定組態檔啟動。這應該是後面有註釋導致的吧,去掉註釋
*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 4
>>> 'requirepass mcw123 #設定認證密碼'
Bad directive or wrong number of arguments
去掉註釋,然後成功啟動,這裡不能在設定後面加註釋,容易出錯
[root@mcw01 ~/redis]$ vim mcwRedis.conf 
requirepass mcw123
daemonize yes
[root@mcw01 ~/redis]$ redis-server mcwRedis.conf 
24391:C 05 Mar 19:10:38.749 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
24391:C 05 Mar 19:10:38.749 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=24391, just started
24391:C 05 Mar 19:10:38.749 # Configuration loaded
[root@mcw01 ~/redis]$ netstat -lntup|grep redis #成功啟動了
tcp        0      0 10.0.0.11:6379          0.0.0.0:*               LISTEN      24392/redis-server  
[root@mcw01 ~/redis]$ redis-cli  #現在直接這麼連線就不行了
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> 
[root@mcw01 ~/redis]$ redis-cli  -h 10.0.0.11 -p 6379   #指定ip埠連線
10.0.0.11:6379> ping
(error) NOAUTH Authentication required.
10.0.0.11:6379> auth mcw123
OK
10.0.0.11:6379> ping  #認證之後就可以正常執行命令了
PONG
10.0.0.11:6379>

set、get命令

10.0.0.11:6379> set name mcw
OK
10.0.0.11:6379> get name
"mcw"
10.0.0.11:6379> set age  18  
OK
10.0.0.11:6379> get age  #它將值改為字串型別的了
"18"
10.0.0.11:6379>

簡單組態檔介紹

bind 192.168.182.130  #繫結伺服器端地址 
protected-mode yes    #安全模式 
port 6800        #埠 
requirepass  haohaio                #密碼                                                                       
daemonize yes    #後臺執行 
pidfile /var/run/redis_6379.pid    #程序id檔案
loglevel notice        #紀錄檔等級
logfile ""

redis基礎資料型別

redis是一種高階的key:value儲存系統,其中value支援五種資料型別
    字串(strings)
    雜湊(hashes)
    列表(lists)
    集合(sets)
    有序集合(sorted sets)  

redis命令

redis簡單命令

keys * 檢視所有key
type key 檢視key型別
expire key seconds 過期時間
ttl key 檢視key過期剩餘時間 -2表示key已經不存在了
persist 取消key的過期時間 -1表示key存在,沒有過期時間

exists key 判斷key存在 存在返回1 否則0
del keys 刪除key 可以刪除多個
dbsize 計算key的數量
flushdb #清除redis所有key,慎用
flushdb #清除redis所有key,慎用
flushdb #清除redis所有key,慎用

keys * 
10.0.0.11:6379> keys *
1) "age"
2) "name"
10.0.0.11:6379> set gender man
OK
1) "gender"
2) "age"
3) "name"
10.0.0.11:6379> 
keys *         檢視所有key
type key      檢視key型別
新增一個列表,檢視key型別,是列表型別
10.0.0.11:6379> type name
string
10.0.0.11:6379> LPUSH paidui1 xiaoma mcw
(integer) 2
10.0.0.11:6379> keys * 
1) "paidui1"
2) "gender"
3) "age"
4) "name"
10.0.0.11:6379> type paidui1
list
expire key seconds    過期時間
ttl key     檢視key過期剩餘時間        -2表示key已經不存在了
給已有的paidui1設定一個過期時間多少秒,然後ttl  key可以檢視過期時間,當-2時就已經過期了,過期了就已經被移除了
10.0.0.11:6379> EXPIRE paidui1 10
(integer) 1
10.0.0.11:6379> ttl paidui1
(integer) -2
persist     取消key的過期時間   -1表示key存在,沒有過期時間
比如下面,將name設定50秒過期,當要取消name的過期時間,執行命令後,ttl值變成-1,就不會出現過期刪除的情況了
10.0.0.11:6379> EXPIRE name 50
10.0.0.11:6379> ttl name
(integer) 22
(integer) 19
10.0.0.11:6379> PERSIST name
(integer) -1
exists key     判斷key存在    存在返回1    否則0
10.0.0.11:6379> exists name
10.0.0.11:6379> exists mcw
(integer) 0
del keys     刪除key    可以刪除多個
4) "myname"
10.0.0.11:6379> del myname
dbsize         計算key的數量
10.0.0.11:6379> dbsize
(integer) 3
flushdb  #清除redis所有key,慎用
10.0.0.11:6379> flushdb
(empty list or set)
10.0.0.11:6379>

字串型別命令

redis資料型別1,字串型別,通過set命令,設定字串型別的key
    strings型別
    set   設定key
    get   獲取key
    append  追加string
    mset   設定多個鍵值對
    mget   獲取多個鍵值對
    del  刪除key
    incr  遞增+1
    decr  遞減-1
set   設定key
get   獲取key
append  追加string
10.0.0.11:6379> set "name" "mcw"
OK
10.0.0.11:6379> get name
"mcw"
10.0.0.11:6379> append "name" "Haoren"
(integer) 9
10.0.0.11:6379> get name
"mcwHaoren"
10.0.0.11:6379> 
mset   設定多個鍵值對
mget   獲取多個鍵值對
10.0.0.11:6379> mset name2 "xiaoma" "name3" xiaohong
OK
10.0.0.11:6379> keys *
1) "name2"
2) "name"
3) "name3"
10.0.0.11:6379> get name2
"xiaoma"
10.0.0.11:6379> get name3
"xiaohong"
10.0.0.11:6379> 
10.0.0.11:6379> mget name name2 name3
1) "mcwHaoren"
2) "xiaoma"
3) "xiaohong"
10.0.0.11:6379> 
del  刪除key
10.0.0.11:6379> keys *
1) "name2"
2) "name"
3) "name3"
10.0.0.11:6379> del name
(integer) 1
10.0.0.11:6379> del name2 name3
(integer) 2
10.0.0.11:6379> keys *
(empty list or set)
10.0.0.11:6379> 
incr  遞增+1
decr  遞減-1
相當於把字串轉換為數位,計算後結果再改成字串。
還可以減為負值
10.0.0.11:6379> keys *
(empty list or set)
10.0.0.11:6379> set "dian zan" 0
OK
10.0.0.11:6379> get "dian zan"
"0"
10.0.0.11:6379> incr "dian zan"
(integer) 1
10.0.0.11:6379> incr "dian zan"
(integer) 2
10.0.0.11:6379> get "dian zan"
"2"
10.0.0.11:6379> incr "dian zan"
(integer) 3
10.0.0.11:6379> get "dian zan"
"3"
10.0.0.11:6379> decr "dian zan"
(integer) 2
10.0.0.11:6379> get "dian zan"
"2"
10.0.0.11:6379> 
10.0.0.11:6379> keys *
1) "dian zan"
10.0.0.11:6379> 
10.0.0.11:6379> get "dian zan"
"2"
10.0.0.11:6379> decr "dian zan"
(integer) 1
10.0.0.11:6379> decr "dian zan"
(integer) 0
10.0.0.11:6379> decr "dian zan"
(integer) -1
10.0.0.11:6379> decr "dian zan"
(integer) -2
10.0.0.11:6379> decr "dian zan"
(integer) -3

list型別,雙向佇列

lpush 從列表左邊插
rpush 從列表右邊插
lrange 獲取一定長度的元素 lrange key start stop
ltrim 擷取一定長度列表
lpop 刪除最左邊一個元素
rpop 刪除最右邊一個元素
lpushx/rpushx key存在則新增值,不存在不處理

lpush         從列表左邊插
從左邊插入,再從左邊獲取,先插入的在裡面後取出,後插入的在左邊先取出
10.0.0.11:6379> keys *
(empty list or set)
10.0.0.11:6379> lpush paidui mcw1 mcw2 mcw3 mcw4
(integer) 4
10.0.0.11:6379> lrange paidui 0 -1
1) "mcw4"
2) "mcw3"
3) "mcw2"
4) "mcw1"
10.0.0.11:6379> lpush paidui mcw5
(integer) 5
10.0.0.11:6379> lrange paidui 0 -1
1) "mcw5"
2) "mcw4"
3) "mcw3"
4) "mcw2"
5) "mcw1"
10.0.0.11:6379> 
rpush         從列表右邊插
從列表左邊取,第一個取出來的是最後推入的,最後一個取出的是第一個推入的。
可以想象成如下排隊順序, mcw5 mcw4 mcw3 mcw2 mcw1   
當從右邊推入mcw6的時候就是 :mcw5 mcw4 mcw3 mcw2 mcw1 mcw6
當從左邊取資料的時候就是:
第一次取:mcw5
第二次取: mcw4
....
第六次取:mcw6
10.0.0.11:6379> lrange paidui 0 -1
1) "mcw5"
2) "mcw4"
3) "mcw3"
4) "mcw2"
5) "mcw1"
10.0.0.11:6379> rpush paidui mcw6
(integer) 6
10.0.0.11:6379> lrange paidui 0 -1
1) "mcw5"
2) "mcw4"
3) "mcw3"
4) "mcw2"
5) "mcw1"
6) "mcw6"
lrange          獲取一定長度的元素  lrange key  start stop
取列表型別的資料,指定開始資料和結束資料,比如開始0,結束-1,這是取列表所有的值
10.0.0.11:6379> lrange paidui 0 -1
1) "mcw5"
2) "mcw4"
3) "mcw3"
4) "mcw2"
5) "mcw1"
10.0.0.11:6379> 
10.0.0.11:6379> keys * 
1) "paidui"
10.0.0.11:6379> type paidui
list
10.0.0.11:6379> get paidui   #列表型別不能用get取值
(error) WRONGTYPE Operation against a key holding the wrong kind of value
10.0.0.11:6379> lrange paid 0 -1  #不存在的鍵
(empty list or set)
10.0.0.11:6379> 
10.0.0.11:6379> lrange paidui 0 -1
1) "mcw5"
2) "mcw4"
3) "mcw3"
4) "mcw2"
5) "mcw1"
10.0.0.11:6379> 
ltrim               擷取一定長度列表
根據查詢可知列表資料順序如下:mcw5 mcw4 mcw3 mcw2 mcw1
索引分別是:                   0    1    2     3    4  
這裡是從paidui左邊擷取,從索引1開始,從索引2結束,其中包含索引2的資料,擷取之後,這個key的值就變成擷取的值了mcw4 mcw3
然後從左邊查詢,第一個查出來的資料就是mcw4,第二個就是mcw3
10.0.0.11:6379> lrange paidui 0 -1
1) "mcw5"
2) "mcw4"
3) "mcw3"
4) "mcw2"
5) "mcw1"
10.0.0.11:6379> ltrim paidui 1 2
OK
10.0.0.11:6379> lrange paidui 0 -1
1) "mcw4"
2) "mcw3"
10.0.0.11:6379> 
lpop                 刪除最左邊一個元素
rpop                     刪除最右邊一個元素
根據lrange可以列表資料的順序是:mcw5 mcw4 mcw3 mcw2 mcw1 mcw6
所以從左邊刪除,刪的是mcw5,從右邊刪除,刪的是mcw6
10.0.0.11:6379> lrange paidui 0 -1
1) "mcw5"
2) "mcw4"
3) "mcw3"
4) "mcw2"
5) "mcw1"
6) "mcw6"
10.0.0.11:6379> lpop paidui
"mcw5"
10.0.0.11:6379> rpop paidui
"mcw6"
10.0.0.11:6379> lrange paidui 0 -1
1) "mcw4"
2) "mcw3"
3) "mcw2"
4) "mcw1"
10.0.0.11:6379> 
lpushx/rpushx                key存在則新增值,不存在不處理
不存在返回個0。預設key不存在會建立key的
10.0.0.11:6379> keys * 
1) "paidui"
10.0.0.11:6379> lpushx paid mcw0
(integer) 0
10.0.0.11:6379> keys * 
1) "paidui"
10.0.0.11:6379> lpushx paidui mcw0
(integer) 5
10.0.0.11:6379> keys * 
1) "paidui"
10.0.0.11:6379> lrange paidui 0 -1
1) "mcw0"
2) "mcw4"
3) "mcw3"
4) "mcw2"
5) "mcw1"
10.0.0.11:6379>

redis的集合,是一種無序的集合,集合中的元素沒有先後順序。

集合相關的操作也很豐富,如新增新元素、刪除已有元素、取交集、取並集、取差集等。我們來看例子:

sadd/srem 新增/刪除 元素
sismember 判斷是否為set的一個元素
smembers 返回集合所有的成員
sdiff 返回一個集合和其他集合的差異
sinter 返回幾個集合的交集
sunion 返回幾個集合的並集

sadd/srem   新增/刪除 元素
smembers    返回集合所有的成員
10.0.0.11:6379> keys *
(empty list or set)
10.0.0.11:6379> sadd zoo gou mao zhu  #新增
(integer) 3
1) "zoo"
10.0.0.11:6379> type zoo
set
10.0.0.11:6379> smembers zoo  #查詢
1) "mao"
2) "zhu"
3) "gou"
10.0.0.11:6379> 
10.0.0.11:6379> srem zoo zhu  #刪除
(integer) 1
10.0.0.11:6379> smembers zoo
2) "gou"
新增成員
10.0.0.11:6379> sadd zoo zhu #新增
sismember   判斷是否為set的一個元素
mao是集合zoo裡的成員嗎,是,返回的是1;
zhu是集合zoo裡的成員嗎,不是,返回的是0
10.0.0.11:6379> sismember zoo mao
10.0.0.11:6379> sismember zoo zhu
(integer) 0
sdiff             返回一個集合和其他集合的差異
如下,sdiff 集合1 集合2 
返回集合1中有的,而集合2中沒有的。就是取差集
1) "zoo2"
2) "zoo"
10.0.0.11:6379> smembers zoo2
1) "she"
3) "shu"
10.0.0.11:6379> sdiff zoo zoo2
10.0.0.11:6379> sdiff zoo2 zoo
2) "shu"
sinter           返回幾個集合的交集
如下,幾個集合應該不只是包含兩個集合
10.0.0.11:6379> sinter zoo zoo2
1) "zhu"
10.0.0.11:6379> sinter zoo2 zoo
sunion          返回幾個集合的並集
集合取並集,幾個集合應該不只是能用兩個集合
10.0.0.11:6379> sunion zoo zoo2
4) "mao"
5) "shu"
10.0.0.11:6379>

雜湊資料結構

hashes即雜湊。雜湊是從redis-2.0.0版本之後才有的資料結構。

hashes存的是字串和字串值之間的對映,比如一個使用者要儲存其全名、姓氏、年齡等等,就很適合使用雜湊。

hset 設定雜湊值
hget 獲取雜湊值
hmset 設定多對雜湊值
hmget 獲取多對雜湊值
hsetnx 如果雜湊已經存在,則不設定(防止覆蓋key)
hkeys 返回所有fileds
hvals 返回所有values
hlen 返回雜湊包含域(field)的數量
hdel 刪除雜湊指定的域(field)
hexists 判斷是否存在

hset 設定雜湊值
hget  獲取雜湊值
10.0.0.11:6379> hset stu1 name "mcw" age 18 height "170"
(integer) 3
10.0.0.11:6379> keys * 
1) "stu1"
10.0.0.11:6379> type stu1
hash
10.0.0.11:6379> hget stu1 name
"mcw"
10.0.0.11:6379> hget stu1 age
"18"
10.0.0.11:6379> hget stu1 height
"170"
10.0.0.11:6379> 
10.0.0.11:6379> hset new1 title "happy new year" content "ni hao"
(integer) 2
10.0.0.11:6379> 
hmset  設定多對雜湊值
hmget  獲取多對雜湊值
10.0.0.11:6379> 
10.0.0.11:6379> keys * 
1) "stu1"
2) "new1"
10.0.0.11:6379> hmget stu1 name age height
1) "mcw"
2) "18"
3) "170"
10.0.0.11:6379> 
hsetnx   如果雜湊已經存在,則不設定(防止覆蓋key)
10.0.0.11:6379> hsetnx stu2 name "mcw2" age 18 height "168"
(error) ERR wrong number of arguments for 'hsetnx' command
10.0.0.11:6379> 
10.0.0.11:6379> hsetnx stu2 name "mcw2" 
(integer) 1
10.0.0.11:6379> hsetnx stu2 name "mcw2" 
(integer) 0
10.0.0.11:6379> keys *
1) "stu2"
2) "stu1"
3) "new1"
10.0.0.11:6379> hsetnx stu2 name "mcw1"  
(integer) 0
10.0.0.11:6379> hget stu2 name  #存在stu2,使用hsetnx沒被修改
"mcw2"
10.0.0.11:6379> hset stu2 name "mcw1" 
(integer) 0
10.0.0.11:6379> hget stu2 name  #存在stu2,使用hset被修改了
"mcw1"
10.0.0.11:6379> 
hkeys     返回所有fileds
10.0.0.11:6379> keys *
1) "stu2"
2) "stu1"
3) "new1"
10.0.0.11:6379> hkeys stu1
1) "name"
2) "age"
3) "height"
10.0.0.11:6379> 
hvals     返回所有values
10.0.0.11:6379> keys *
1) "stu2"
2) "stu1"
3) "new1"
10.0.0.11:6379> hvals stu1
1) "mcw"
2) "18"
3) "170"
10.0.0.11:6379> 
hlen      返回雜湊包含域(field)的數量
10.0.0.11:6379> keys *
1) "stu2"
2) "stu1"
3) "new1"
10.0.0.11:6379> hkeys stu1
1) "name"
2) "age"
3) "height"
10.0.0.11:6379> hlen stu1
(integer) 3
10.0.0.11:6379> hkeys new1
1) "title"
2) "content"
10.0.0.11:6379> hlen new1
(integer) 2
10.0.0.11:6379> 
hdel     刪除雜湊指定的域(field)
10.0.0.11:6379> keys *
1) "stu2"
2) "stu1"
3) "new1"
10.0.0.11:6379> type stu1
hash
10.0.0.11:6379> 
10.0.0.11:6379> hkeys stu1
1) "name"
2) "age"
3) "height"
10.0.0.11:6379> hdel stu1 height  #刪除指定欄位
(integer) 1
10.0.0.11:6379> hkeys stu1
1) "name"
2) "age"
10.0.0.11:6379> 
hexists    判斷是否存在
10.0.0.11:6379> keys *
1) "stu2"
2) "stu1"
3) "new1"
10.0.0.11:6379> type stu1
hash
10.0.0.11:6379> hkeys stu1
1) "name"
2) "age"
10.0.0.11:6379> hexists stu1 name   #存在欄位返回1
(integer) 1
10.0.0.11:6379> hexists stu1 height #不存在欄位返回0
(integer) 0
10.0.0.11:6379>

釋出訂閱

主要應用:通知,公告
可以作為訊息佇列或者訊息管道

一個釋出,多個訂閱
釋出:廣播電臺頻道能傳送給所有接收這個頻道的人聲音資訊
訂閱:很多人都能接收到這個廣播電臺的訊息

比如qq群公告:一個釋出,多個訂閱(接收到)

如下,一個釋出者,兩個訂閱者,訂閱者1訂閱跟python和linux頻道有關的訊息;訂閱者2訂閱和golang和Linux有感的訊息

當釋出者釋出訊息的時候,訂閱者1接收到了訂閱頻道python發來的訊息

當釋出者釋出給linux頻道的時候,兩個訂閱人都接收到了訊息。釋出者這裡還返回了幾個訂閱人接收到了訊息。上面那條寫錯了釋出到一個沒有人訂閱的頻道上,返回0個訂閱者

釋出人釋出的頻道和接收的頻道,這樣寫是需要精準判斷,不能模糊匹配接收訊息的

當訂閱者這裡使用psubscribe的時候,後面變成了模式

當訂閱者使用psubscribe命令,且訂閱的頻道使用萬用字元的時候,那麼釋出者釋出的能跟訂閱者模糊匹配到的頻道,就被訂閱者接收到了

參考:https://www.cnblogs.com/pyyu/p/9843950.html redis 部落格地址

到此這篇關於redis部署以及各種資料型別使用命令等詳解的文章就介紹到這了,更多相關redis資料型別使用命令內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com