redis實戰篇
一、安裝命令步驟
1 安裝wget命令 yum install wget ,然後執行 mkdir install 且進入 cd install
2 下載安裝包:wget http://download.redis.io/releases/redis-5.0.8.tar.gz 進行 解壓:tar xf redis-5.0.8.tar.gz
[root@localhost soft]# ll -h
total 1.9M
drwxrwxr-x 6 root root 4.0K Mar 12 08:07 redis-5.0.8
-rw-r--r-- 1 root root 1.9M Jun 6 23:44 redis-5.0.8.tar.gz
3 cd redis-src 看README.md ,執行編譯命令: make
如果報錯,執行yum install gcc , make distclean ,然後再次執行make
4 cd src 可以看到生成了可執行程序, cd .. 退出來後 make install PREFIX=/opt/soft/redis5
5 vi /etc/profile
export REDIS_HOME=/opt/install/redis5
export PATH=$PATH:$REDIS_HOME/bin
查看配置的redis環境變量路徑
source /etc/profile
echo $PATH
6 cd utils 然後執行 ./install_server.sh
1)一個物理機可以有多個redis實例(進程),通過port區分
2)可執行程序就一份在目錄,但是內存中未來的多個實例需要各自的配置文件和持久化目錄等資源
3) service redis_6379 start/stop/status
執行 cd /etc/init.d/ **** 可以看到新創建的redis_6379
4)腳本會幫啓動redis
7 查看redis是否啓動 ps -fe|grep redis
啓動另外的實例:cd redis-util , ./install_server.sh
查看啓動狀態 service redis_6380 status ,
查看進程 pe -fe|grep redis
二、redis常用命令
查看上面端口進程: ps -ef |grep redis
[root@localhost ~]# ps -ef |grep redis
root 8326 1 0 Jun12 ? 00:01:14 /opt/mashibing/redis5/bin/redis-server 127.0.0.1:6379
root 8472 1 0 Jun12 ? 00:01:06 /opt/mashibing/redis5/bin/redis-server 127.0.0.1:6380
root 8937 1 0 Jun12 ? 00:00:38 /opt/mashibing/redis5/bin/redis-server 127.0.0.1:6381
root 11998 11585 0 03:44 pts/1 00:00:00 grep redis
1、 redis客戶端啓動:redis-cli
默認6379端口,也可以指定端口和數據庫 redis-cli -p 6380
,切換數據庫select 6
;
或者直接輸入:redis-cli -p 6380 -n 6
,表示在6380端口切換到6號數據庫,redis有16個數據庫,默認6379,0號庫
[root@localhost ~]# redis-cli -p 6380 -n 6
127.0.0.1:6380[6]>
2、輸入help
127.0.0.1:6380[6]> help
redis-cli 5.0.8
To get help about Redis commands 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
To set redis-cli preferences:
":set hints" enable online hints
":set nohints" disable online hints
Set your preferences in ~/.redisclirc
127.0.0.1:6380[6]>
參照上面可以知道 :
help用法
help @se +Tab鍵補全
,有命令使用說明,或者直接help se+Tab鍵
同樣有補全,eg:
127.0.0.1:6380[6]> help SET
SET key value [expiration EX seconds|PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
group: string
127.0.0.1:6380[6]> FLUSHALL -- 清除所有值,正式環境勿用,等同flushdb
OK
127.0.0.1:6380[6]> keys * -- 查詢所有key
(empty list or set)
3 、面向字符串常用操作
set k1 abc nx
redis分佈式鎖主要命令
以下測試set
裏帶nx
的 命令,當k1有值時, set k1 abc nx
里加nx表示k1無值時纔會將其賦值爲abc
,否則如果k1
已經有值,則使用 set...nx
不會賦值成功,redis
分佈式鎖也用到這個命令實現的。
特點: nx只能創建,xx只能更新。
127.0.0.1:6380[6]> set k1 1
OK
127.0.0.1:6380[6]> set k1 abc nx
(nil)
127.0.0.1:6380[6]> get k1
"1"
127.0.0.1:6380[6]> set k2 abc nx
OK
127.0.0.1:6380[6]> get k2
"abc"
127.0.0.1:6380[6]> clear -- 清屏
127.0.0.1:6380[6]> exit -- 退出客戶端會話
mset,mget
字符串同時賦值多個key的值 :
127.0.0.1:6380[6]> mset a 1 b 2 c cc -- mset 多個鍵值賦值
OK
127.0.0.1:6380[6]> mget a b
1) "1"
2) "2"
127.0.0.1:6380[6]> mget a b c -- mget 多個鍵值取值
1) "1"
2) "2"
3) "cc"
127.0.0.1:6380[6]> get c
"cc"
輸入: help @string
append
測試追加命令append
127.0.0.1:6380[6]> help @string
APPEND key value
summary: Append a value to a key
since: 2.0.0
............
-------------------------------------
127.0.0.1:6380[6]> set k3 hello
OK
127.0.0.1:6380[6]> append k3 ' world!' -- 追加命令 append
(integer) 12
127.0.0.1:6380[6]> get k3
"hello world!"
有了這個正反向索引,按照範圍查找可有兩種。
正反向索引 getrange 、setrange
按範圍獲取或覆蓋k3的指定位置的值,getrange 【正反向索引】 ,setrange ; eg:
-- 正反向索引
127.0.0.1:6380[6]> getrange k3 6 11
"world!"
127.0.0.1:6380[6]> getrange k3 6 -1
"world!"
127.0.0.1:6380[6]> getrange k3 0 -1
"hello world!"
127.0.0.1:6380[6]> getrange k3 6 11
"world!"
-------------------------------------
127.0.0.1:6380[6]> get k3
"hello world!"
127.0.0.1:6380[6]> SETRANGE k3 6 Q -- setRange 指定位置覆蓋原值,從腳標6開始覆蓋通過Q
(integer) 12
127.0.0.1:6380[6]> get k3
"hello Qorld!"
127.0.0.1:6380[6]> SETRANGE k3 6 xiaoQ
(integer) 12
127.0.0.1:6380[6]> get k3
"hello xiaoQ!"
strlen
獲取長度strlen +key
127.0.0.1:6380[6]> strlen k3 -- 獲取長度
(integer) 12
encoding獲取string裏的數值類型
redis裏沒有int類型,因爲string類型裏也操作數值類型數據,如下
通過object 的encoding能夠獲取數值類型
127.0.0.1:6380[6]> FLUSHDB
OK
127.0.0.1:6380[6]> set k1 11
OK
127.0.0.1:6380[6]> type k1
string
127.0.0.1:6380[6]> set k2 hello
OK
127.0.0.1:6380[6]> type k2
string
127.0.0.1:6380[6]> help OBJECT
OBJECT subcommand [arguments [arguments ...]]
summary: Inspect the internals of Redis objects
since: 2.2.3
group: generic
127.0.0.1:6380[6]> OBJECT help
1) OBJECT <subcommand> arg arg ... arg. Subcommands are:
2) ENCODING <key> -- Return the kind of internal representation used in order to store the value associated with a key.
3) FREQ <key> -- Return the access frequency index of the key. The returned integer is proportional to the logarithm of the recent access frequency of the key.
4) IDLETIME <key> -- Return the idle time of the key, that is the approximated number of seconds elapsed since the last access to the key.
5) REFCOUNT <key> -- Return the number of references of the value associated with the specified key.
127.0.0.1:6380[6]>
127.0.0.1:6380[6]> OBJECT encoding k1 -- 通過encoding獲取k1爲數值類型
"int"
127.0.0.1:6380[6]> OBJECT encoding k2
"embstr"
自增,自減
incr +key 自增1 ,自增量incrby key increment ,自增浮點數,自減同理help獲取方式,DECRBY ,DECR
127.0.0.1:6380[6]> incr k1
(integer) 12
127.0.0.1:6380[6]> get k1
"12"
127.0.0.1:6380[6]> help incr
INCR key
summary: Increment the integer value of a key by one
since: 1.0.0
group: string
127.0.0.1:6380[6]> help incrby
INCRBY key increment
summary: Increment the integer value of a key by the given amount
since: 1.0.0
group: string
127.0.0.1:6380[6]> INCRBY k1 10 -- 增量爲10
(integer) 22
127.0.0.1:6380[6]> get k1
"22"
127.0.0.1:6380[6]> INCRBYFLOAT k1 3.5 -- 增量爲3.5 浮點數
"25.5"
127.0.0.1:6380[6]> DECRBY K1 11 -- 自減11
(integer) -11
127.0.0.1:6380[6]> GET K1
"-11"
127.0.0.1:6380[6]> DECR K1 -- 自減1
(integer) -12
127.0.0.1:6380[6]> GET K1
"-12"
redis的二進制安全
redis二進制安全是因爲數據存取是二進制的數據,存取不會保留數據編碼格式。redis中是通過SDS實現字符串對象的工具;redis只取字節流。
當用戶使用一個程序讀取一個二進制文件時,該文件會被按照這個程序的規則進行解釋。如果這個程序所使用的格式編碼和文件被寫入的格式編碼一致,那麼這個文件可以被正常讀取,否則會使文件損壞,導致數據永久消失,所以重要文件最好放在只讀的存儲介質中,比如光盤。【hbase也是二進制安全的】
所以多語言開發傾向json,xml形式傳輸數據,不使用序列化的原因是需要額外增加編碼器和解碼器,不然容易出錯,嚴重的話還會導致數據丟失。
eg:
讀取數據編碼格式爲gb2312
127.0.0.1:6380[6]> set k1 中 -- 給k1賦值爲 “中”
OK
127.0.0.1:6380[6]> strlen k1
(integer) 2
127.0.0.1:6380[6]> get k1
"\xd6\xd0" -- 十六進制存取
-------------------------------------------------
編碼格式切換爲ISO-8859-1 後給k2賦值
127.0.0.1:6380[6]> set k2 ? -- 給k2賦值爲 “中”
OK
127.0.0.1:6380[6]> STRLEN k2 --不同於k1的編碼,所以長度不一致
(integer) 1
127.0.0.1:6380[6]> get k2
"?"
127.0.0.1:6380[6]> exit
下面數據跟當前編碼格式一致,則轉換的就是對的,k2跟當前編碼不一致,所以依然獲取不到
[root@localhost ~]# redis-cli -p 6380 -n 6 --raw -- 格式化數據編碼
127.0.0.1:6380[6]> get k2 -- 編碼格式切換回gb2312後
?
127.0.0.1:6380[6]> get k1
中
getset 減少io請求。
127.0.0.1:6380[6]> help getset
GETSET key value
summary: Set the string value of a key and return its old value
since: 1.0.0
group: string
127.0.0.1:6380[6]> getset k1 abc
中
127.0.0.1:6380[6]> get k1
abc
msetnx redis原子性操作示例
給k1,k2賦值,用msetnx給k1,k3賦值失敗,原因k1是存在的,執行此命令就失敗了,所以整體失敗了
nx只管創建,不管更新
127.0.0.1:6380[6]> FLUSHALL
OK
127.0.0.1:6380[6]> mset k1 a k2 b
OK
127.0.0.1:6380[6]> mget k1 k2
a
b
127.0.0.1:6380[6]> MSETNX k1 aa k3 cc -- 返回結果不是ok 是0表示執行結果失敗,原因nx只管創建,k1是存在的,k1執行失敗,所以整體失敗,體現該操作爲原子性
0
127.0.0.1:6380[6]> mget k1 k2 k3
a
b
字節操作
1 SETBIT key offset value – offset 是指二進制位的偏移量
一字節8個二進制位。
127.0.0.1:6380[6]> help setbit
SETBIT key offset value
summary: Sets or clears the bit at offset in the string value stored at key
since: 2.2.0
group: string
127.0.0.1:6380[6]> FLUSHDB
OK
127.0.0.1:6380[6]> get keys
(nil)
127.0.0.1:6380[6]> setbit k1 1 1
(integer) 0
127.0.0.1:6380[6]> STRLEN k1
(integer) 1
127.0.0.1:6380[6]> get k1
"@"
127.0.0.1:6380[6]> setbit k1 7 1
(integer) 0
127.0.0.1:6380[6]> get k1
"A"
127.0.0.1:6380[6]> STRLEN k1
(integer) 1
127.0.0.1:6380[6]> setbit k1 9 1
(integer) 0
127.0.0.1:6380[6]> get k1
"A@"
127.0.0.1:6380[6]> strlen k1
(integer) 2
驗證ascii碼值對應字符值:man ascii
[root@localhost ~]# man ascii
...
Oct Dec Hex Char Oct Dec Hex Char
------------------------------------------------------------------------
000 0 00 NUL '\0' 100 64 40 @
001 1 01 SOH (start of heading) 101 65 41 A
002 2 02 STX (start of text) 102 66 42 B
003 3 03 ETX (end of text) 103 67 43 C
004 4 04 EOT (end of transmission) 104 68
...
標準字符集爲ascii,其他一般叫做擴展字符集;
擴展的含義:其他字符集不再對ascii重編碼。
ascii碼首位必須是0,其他可以操作
2 BITPOS key bit [start] [end]
bit 二進制位的值 start ,end表示字節
含義: 查找bit在字節區間首次出現的位置
127.0.0.1:6380[6]> help bitpos
BITPOS key bit [start] [end]
summary: Find first bit set or clear in a string
since: 2.8.7
group: string
127.0.0.1:6380[6]> bitpos k1 1 0 0
(integer) 1
127.0.0.1:6380[6]> bitpos k1 1 1 1
(integer) 9
到此,k1的值圖文表示如下
3 BITCOUNT key [start end] 二進制位1的個數統計,在start end這個字節區間
127.0.0.1:6380[6]> bitpos k1 1 0 0
(integer) 1
127.0.0.1:6380[6]> bitpos k1 1 1 1
(integer) 9
127.0.0.1:6380[6]> BITCOUNT k1 0 0
(integer) 2
127.0.0.1:6380[6]> BITCOUNT k1 0 1
(integer) 3
127.0.0.1:6380[6]> BITCOUNT k1 1 1
(integer) 1
4 BITOP operation destkey key [key …]
operation與或非,亦或等之類的操作
destkey 目標key:操作的結果放在這裏
key…:多個參與運算操作的key
127.0.0.1:6380[6]> help bitop
BITOP operation destkey key [key ...]
summary: Perform bitwise operations between strings
since: 2.6.0
group: string
127.0.0.1:6380[6]> FLUSHALL
OK
127.0.0.1:6380[6]> setbit k1 1 1
(integer) 0
127.0.0.1:6380[6]> setbit k1 6 1
(integer) 0
127.0.0.1:6380[6]> FLUSHALL
OK
127.0.0.1:6380[6]> setbit k1 1 1
(integer) 0
127.0.0.1:6380[6]> setbit k1 7 1
(integer) 0
127.0.0.1:6380[6]> setbit k2 1 1
(integer) 0
127.0.0.1:6380[6]> setbit k2 6 1
(integer) 0
127.0.0.1:6380[6]> get k1
"A"
127.0.0.1:6380[6]> get k2
"B"
127.0.0.1:6380[6]> bitop and targetkey k1 k2
(integer) 1
127.0.0.1:6380[6]> get targetkey
"@"
A,B相與之後,得到@
AB或運算後,得到C
127.0.0.1:6380[6]> bitop or targetkey2 k1 k2
(integer) 1
127.0.0.1:6380[6]> get targetkey2
"C"
redis中的位圖bitmap,很重要
場景:企業用戶系統要統計用戶登錄天數,窗口隨機,電商網站統計某活動區間用戶登錄天數。如何維護並支持用戶登錄的時候登記,且隨機窗口查詢。
設計mysql登錄表,每次登記都產生一筆,登記時間(4個字節),userid(大概只能用4個字節),一筆登錄用戶佔用8字節。一個用戶在一年裏就會產生很多用戶,然後客戶很多的情況下,怎麼維護。
4 、面向list常用操作
三、redis管道之類
其他
redis:面向網絡io ,多路複用的時候,單線程單進程就能處理;
mysql傾向bio ,比如十萬百萬個連接會到達很多請求,如果每個請求要出發到磁盤io,會導致帶寬阻塞,就會等很久,所以mysql是用的bio
誠心歡迎各位交流探討技術QQ:2402774969