複習電商筆記-25-redis操作
Redis啟動
啟動錯誤
The Windows version of Redis allocates a large memory mapped file for sharing the heap with the forked process used in persistence operations. This file will be created in the current working directory or the directory specified by the 'heapdir' directive in the .conf file. Windows is reporting that there is insufficient disk space available for this file (Windows error 0x70). You may fix this problem by either reducing the size of the Redis heap with the --maxheap flag, or by moving the heap file to a local drive with sufficient space. Please see the documentation included with the binary distributions for more details on the --maxheap and --heapdir flags. Redis can not continue. Exiting.
redis.conf配置檔案中設定
maxheap 1014000000
啟動配置
配置檔案:redis.conf
設定模式
vi redis.conf
daemonize yes #預設為no
埠配置:
預設6379
配置資料庫數量:
配置記憶體大小:
會生成一個和記憶體大小一樣的檔案。
maxmemory 200mb #在真實環境必須部署,否則實體記憶體會被耗盡。一般配置200mb/500mb/1gb/2gb。可以分散到多臺伺服器,和其它業務共享伺服器,以充分利用資源。同時因為分散,防止單點故障,造成大量快取失效。
maxmemory 200mb
啟動
redis-server #預設找redis.conf配置檔案
redis-server & #上面ctrl+c中斷reis會退出,這個不會
redis-server redis6380.conf #指定配置檔案,這樣可以啟動多個例項
注意兩種啟動方式的差異
ps -ef|grep redis root 3511 1 0 16:29 ? 00:00:01 redis-server *:6379 root 3515 1 0 16:29 ? 00:00:01 redis-server 127.0.0.1:6380 註釋掉redis.conf配置檔案中的48行;# bind 127.0.0.1 ::1即可
版本
redis-server –v
Redis server v=3.2.5 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=e6c4c3113548f6b0
redis-cli –v
設定訪問密碼
預設沒有密碼,可以隨意訪問。redis速度相當快,在一個較好的伺服器下,外部使用者每秒可以進行15w此的密碼嘗試,這意味著必須指定非常強大的密碼來防止暴力破解。
如果要加,開啟redis.conf的
requirepass 123456 #480行,設定請求密碼,這樣訪問時都需要先登入
127.0.0.1:6379> auth 123456 #客戶端訪問方式
jedis.auth(“123456”); #jedis訪問方式
詳細資訊
redis-cli
127.0.0.1:6379> info #檢視當前redis節點的詳細配置資訊
Redis命令
redis-cli的使用之傳送命令
預設連線:IP 127.0.0.1 埠 6379
redis-cli
指定IP埠:
redis-cli –h 127.0.0.1 –p 6379
Redis提供了PING-PONG機制,測試與客戶端和伺服器連結是否正常
redis-cli ping
或
redis-cli
redis 127.0.0.1:6379>ping
PONG
redis-cli的使用之命令返回值
狀態回覆(最簡單的回覆-redis提供的測試命令)
redis>PING
PONG
127.0.0.1:6379>SET test 123
OK
錯誤回覆(以error開頭,後面跟著錯誤資訊)
127.0.0.1:6379>TEST
(error) ERR unknown command 'TEST'
整數回覆
127.0.0.1:6379>INCR test_incr
(integer) 1
字串回覆(最長久的一種回覆,雙引號包裹)
127.0.0.1:6379>get test
“123”
多行字串回覆
127.0.0.1:6379>KEYS *
1) "test_incr"
2) "test"
exist(退出)
退出
127.0.0.1:6379> exit
shutdown(關閉)
關閉
127.0.0.1:6379> shutdown
keys
字串型別是redis中最基本的資料型別,它能儲存任何形式的字串,包括二進位制資料。可以儲存JSON化的物件、位元組陣列等。一個字串型別鍵允許儲存的資料最大容量是512MB。
賦值與取值:
SET key value
GET key
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set test 123
OK
127.0.0.1:6379> set test1 ab
OK
127.0.0.1:6379> keys *
1) "test1"
2) "test"
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> get test1
"abc"
127.0.0.1:6379> get test2
(nil)
127.0.0.1:6379>
select
redis預設支援16個數據庫,對外都是以一個從0開始的遞增數字命名,可以通過引數database來修改預設資料庫個數。客戶端連線redis服務後會自動選擇0號資料庫,可以通過select命令更換資料庫,例如選擇1號資料庫:
127.0.0.1:6379>SELECT 1
OK
127.0.0.1:6379>GET test
(nil)
說明:
Redis不支援自定義資料庫名稱。
Redis不支援為每個資料庫設定訪問密碼。
Redis的多個數據庫之間不是安全隔離的,FLUSHALL命令會清空所有資料庫的資料。
keys
獲取符合規則的建名列表。
KEYS *
keys test[_]*
keys t[a-d]
說明:
? 匹配一個字元
* 匹配任意個(包括0個)字元
[] 匹配括號間的任一字元,可以使用“-“表示範圍。如a[a-d]匹配ab/ac/ad
\x 匹配字元x,用於轉義符合,如果要匹配“?“就需要使用\?
clear
清除螢幕內容
exists
判斷一個鍵是否存在。
如果鍵存在則返回整數型別1,否則返回0。
127.0.0.1:6379> keys *
1) "test_incr"
2) "test"
127.0.0.1:6379> exists test
(integer) 1
127.0.0.1:6379> exists test1
(integer) 0
127.0.0.1:6379>
del
刪除鍵,可以刪除一個或者多個鍵,多個鍵用空格隔開,返回值是刪除的鍵的個數。
127.0.0.1:6379> del test
(integer) 1
127.0.0.1:6379> del test
(integer) 0
127.0.0.1:6379> del test test_incr
(integer) 1
127.0.0.1:6379>
type
獲得鍵值的資料型別,返回值可能是string(字串)、hash(雜湊型別)、list(列表型別)、set(集合型別)、zset(有序集合型別)。
127.0.0.1:6379> keys *
1) "test1"
2) "test"
127.0.0.1:6379> type test
string
127.0.0.1:6379> type test1
string
help
127.0.0.1:6379> help
redis-cli 2.8.19
Type: "help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
127.0.0.1:6379> help type
TYPE key
summary: Determine the type stored at key
since: 1.0.0
group: generic
官網:http://www.redis.io幫助
flushall
清空所有資料庫。
127.0.0.1:6379> FLUSHALL
OK
flushdb
清空當前資料庫。
127.0.0.1:6379> FLUSHDB
OK
Redis資料型別之字串
存放的字串為二進位制是安全的。字串長度支援到512M。
incr/incrby
遞增數字INCR key
當儲存的字串是整數時,redis提供了一個實用的命令INCR,其作用是讓當前鍵值遞增,並返回遞增後的值。
127.0.0.1:6379> keys *
1) "test1"
2) "test"
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> get test1
"abc"
127.0.0.1:6379> get test2
(nil)
127.0.0.1:6379> incr num
(integer) 1
127.0.0.1:6379> keys *
1) "num"
2) "test1"
3) "test"
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> incr num
(integer) 3
127.0.0.1:6379>
從上面例子可以看出,如果num不存在,則自動會建立,如果存在自動+1。
指定增長係數
語法:INCRBY key increment
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> incr num
(integer) 3
127.0.0.1:6379> incrby num 2
(integer) 5
127.0.0.1:6379> incrby num 2
(integer) 7
127.0.0.1:6379> incrby num 2
(integer) 9
127.0.0.1:6379> incr num
(integer) 10
127.0.0.1:6379>
decr/decrby
減少指定的整數
DECR key
DECRBY key decrement
127.0.0.1:6379> incr num
(integer) 10
127.0.0.1:6379> decr num
(integer) 9
127.0.0.1:6379> decrby num 3
incrbyfloat
整數時,第一次加可以得到正確結果,浮點數後再加浮點就會出現精度問題。
原來下面的例子2.8.7注意在新版本中已經修正了這個浮點精度問題。3.0.7
INCRBYFLOAT key decrement
127.0.0.1:6379> set num
(integer) 131
127.0.0.1:6379> incrbyfloat num 0.7
“131.7”
127.0.0.1:6379> incrbyfloat num 0.7
“132.3999999999999999”
append
向尾部追加值。如果鍵不存在則建立該鍵,其值為寫的value,即相當於SET key value。返回值是追加後字串的總長度。
語法:APPEND key value
127.0.0.1:6379> keys *
1) "num"
2) "test1"
3) "test"
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> append test "abc"
(integer) 6
127.0.0.1:6379> get test
"123abc"
127.0.0.1:6379>
strlen
字串長度,返回資料的長度,如果鍵不存在則返回0。注意,如果鍵值為空串,返回也是0。
語法:STRLEN key
127.0.0.1:6379> get test
"123abc"
127.0.0.1:6379> strlen test
(integer) 6
127.0.0.1:6379> strlen tnt
(integer) 0
127.0.0.1:6379> set tnt ""
OK
127.0.0.1:6379> strlen tnt
(integer) 0
127.0.0.1:6379> exists tnt
(integer) 1
127.0.0.1:6379>
mset/mget
同時設定/獲取多個鍵值
語法:MSET key value [key value …]
MGET key [key …]
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> mset a 1 b 2 c 3
OK
127.0.0.1:6379> mget a b c
1) "1"
2) "2"
3) "3"
127.0.0.1:6379>