標籤:test div hose cli 庫存 添加 沒有 target tar.gz
python訪問redis1 Linux上安裝redisa) 下載
$ wget http://download.redis.io/releases/redis-3.0.5.tar.gz
b) 編譯
# yum install gcc tcl# tar -zxf redis-3.0.5.tar.gz# cd redis-3.0.5# make# make test# sudo make install
這樣可運行檔案redis-server等就從redis-3.0.5/src複製到/usr/local/bin
c) 啟動服務(6379連接埠要開啟)
$ redis-server
或
$ redis-server /path/to/redis.conf
1428:M 04 Feb 11:47:33.817 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1428:M 04 Feb 11:47:33.817 # Server started, Redis version 3.0.5
1428:M 04 Feb 11:47:33.818 # 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.
1428:M 04 Feb 11:47:33.818 # 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.
d) 伺服器配置,以除去上面的警告
以root使用者操作
1) 改動核心參數: somaxconn
該核心參數預設值通常是128,對於負載非常大的服務程式來說大大的不夠。
通常會將它改動為2048或者更大。
# echo 2048 > /proc/sys/net/core/somaxconn
重新啟動後儲存:
在/etc/sysctl.conf中加入例如以下
net.core.somaxconn = 2048
然後在終端中運行
# sysctl -p
2) 改動vm.overcommit_memory
overcommit_memory 指定了核心針對記憶體配置的策略。其值能夠是0、1、2。
0, 表示核心將檢查是否有足夠的可用記憶體供應用進程使用;假設有足夠的可用記憶體,記憶體申請同意。否則,記憶體申請失敗,並把錯誤返回給應用進程。
1。 表示核心同意分配全部的實體記憶體,而無論當前的記憶體狀態怎樣。
2, 表示核心同意分配超過全部實體記憶體和交換空間總和的記憶體
# vi /etc/sysctl.conf
添加以下一行:
vm.overcommit_memory=1
設定重新啟動後保持, 運行命令:
# sysctl vm.overcommit_memory=1
3) transparent_hugepage
禁用透明巨頁記憶體配置以提高效能
查看目前狀態:
$ cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
參數說明
never 關閉,不使用透明記憶體
alway 盡量使用透明記憶體。掃描記憶體,有512個 4k頁面能夠整合。就整合成一個2M的頁面
madvise 避免改變記憶體佔用
禁用透明巨頁記憶體:
# echo never >/sys/kernel/mm/transparent_hugepage/enabled# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
永久禁用, 將命令加入到rc.local中:
# vi rc.local
...# redis required never for transparent_hugepageif test -f /sys/kernel/mm/transparent_hugepage/enabled; then echo never > /sys/kernel/mm/transparent_hugepage/enabledfiif test -f /sys/kernel/mm/transparent_hugepage/defrag; then echo never > /sys/kernel/mm/transparent_hugepage/defragfiexit 0
e) redis.conf配置
參考以下的redis.conf的配置項: redis配置具體解釋
http://blog.csdn.net/ithomer/article/details/9232891
2 redis-py
redis-py 這是 Redis 眼下最成熟的 Python client開發包。
redis 首頁:
https://github.com/andymccurdy/redis-py
a) 下載原始碼
$ git clone https://github.com/andymccurdy/redis-py.git
b) 安裝
$ cd redis-py$ sudo python redis install
3 hiredis
hiredis是redis資料庫的C介面,眼下只能在linux下使用,幾個主要的函數就能夠操作redis資料庫了。
hiredis 是 Redis 官方指定的 C 語言client開發包,支援 Redis 完整的命令集、管線以及事件驅動編程。
hiredis首頁:
https://github.com/redis/hiredis
a) 下載安裝
$ git clone https://github.com/redis/hiredis.git$ cd hiredis$ make$ sudo make install
範例:
http://blog.csdn.net/mfc_vc_andy/article/details/8095839
p=304">http://www.leoox.com/?
p=304
4 hiredis-py
python對hiredis的封裝類。
首頁:
https://github.com/redis/hiredis-py
a) 下載
$ wget https://pypi.python.org/packages/source/h/hiredis/hiredis-0.2.0.tar.gz
b) 安裝
$ tar zxf hiredis-0.2.0.tar.gz$ cd hiredis-0.2.0$ python setup.py build$ sudo python setup.py install
5 python操作redis
http://blog.csdn.net/chosen0ne/article/details/7319807
http://blog.csdn.net/chenggong2dm/article/details/6102540
6 優雅關閉redis-server
$ redis-cli -h 127.0.0.1 -p 6379 shutdown
7 redis-server啟用password及簡單範例a) 複製redis.conf到/etc/b) 建立redis資料庫隱藏檔夾
$ sudo mkdir /var/redis
c) 改動/etc/redis.conf
$ sudo vi redis.conf
只改動的內容例如以下:
daemonize yesloglevel warninglogfile /var/log/redis.logdir /var/redis/requirepass Abc123
d) 啟動和關閉服務
$ sudo redis-server /etc/redis.conf帶password關閉服務:$ redis-cli -h 127.0.0.1 -p 6379 -a Abc123 shutdown
e) clientpython
此時client須要使用帶password串連伺服器:
import redisr=redis.StrictRedis(host="127.0.0.1", port=6379, db=0, password="Abc123")r.set("city", "shanghai")r.get("city")
給key設定到期時間。比方我們只儲存60秒的資料,能夠這樣設定:
r.expire("city", 60)緊接著訪問它:
r.get("city")shanghai過60秒再訪問:
r.get("city")
沒有了。
8 附錄
更具體的範例參考:
https://github.com/andymccurdy/redis-py
redis的完整的參考:
http://www.redis.net.cn/tutorial/3505.html
python訪問redis