首先說一下在Windows下安裝Redis,安裝包能夠在https://github.com/MSOpenTech/redis/releases中找到,能夠下載msi安裝文件,也能夠下載zip的壓縮文件。html
下載zip文件以後解壓,解壓後是這些文件:python
裏面這個Windows Service Documentation.docx是一個文檔,裏面有安裝指導和使用方法。git
也能夠直接下載msi安裝文件,直接安裝,安裝以後的安裝目錄中也是這些文件,能夠對redis進行相關的配置。github
安裝完成以後能夠對redis進行測試,雙擊redis-cli.exe,若是不報錯的話,應該會鏈接上本地的redis,進行簡單的測試:redis
默認安裝的是6379端口,測試成功。ide
也能夠輸入help,查看幫助:性能
127.0.0.1:6379> help 測試
redis-cli 3.2.100 ui
To get help about Redis commands type: spa
"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 perferences:
":set hints" enable online hints
":set nohints" disable online hints
Set your preferences in ~/.redisclirc
下面說一下用Python操做Redis吧,使用Python安裝Redis的話須要安裝redis-py的庫
一、安裝redis-py
easy_install redis 也可使用pip install redis安裝,或者在https://github.com/andymccurdy/redis-py下載而後執行python setup.py install安裝
二、安裝Parser安裝
Parser能夠控制如何解析redis響應的內容。redis-py包含兩個Parser類,PythonParser和HiredisParser。默認,若是已經安裝了hiredis模塊,redis-py會使用HiredisParser,不然會使用PythonParser。HiredisParser是C編寫的,由redis核心團隊維護,性能要比PythonParser提升10倍以上,因此推薦使用。安裝方法,使用easy_install:
easy_install hiredis 或者pip install hiredis
三、使用python操做redis
redis-py提供兩個類Redis和StrictRedis用於實現Redis的命令,StrictRedis用於實現大部分官方的命令,並使用官方的語法和命令(好比,SET命令對應與StrictRedis.set方法)。Redis是StrictRedis的子類,用於向後兼容舊版本的redis-py。
import redis
r = redis.StrictRedis(host='127.0.0.1', port=6379)
r.set('foo', 'hello')
r.rpush('mylist', 'one')
print r.get('foo')
print r.rpop('mylist')
redis-py使用connection pool來管理對一個redis server的全部鏈接,避免每次創建、釋放鏈接的開銷。默認,每一個Redis實例都會維護一個本身的鏈接池。能夠直接創建一個鏈接池,而後做爲參數Redis,這樣就能夠實現多個Redis實例共享一個鏈接池。
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
r = redis.Redis(connection_pool=pool)
r.set('one', 'first')
r.set('two', 'second')
print r.get('one')
print r.get('two')
redis pipeline機制,能夠在一次請求中執行多個命令,這樣避免了屢次的往返時延。
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
r = redis.Redis(connection_pool=pool)
pipe = r.pipeline()
pipe.set('one', 'first')
pipe.set('two', 'second')
pipe.execute()
pipe.set('one'. 'first').rpush('list', 'hello').rpush('list', 'world').execute()
redis-py默認在一次pipeline中的操做是原子的,要改變這種方式,能夠傳入transaction=False
pipe = r.pipeline(transaction=False)