【Python】Python使用redis
轉自:
#1、安裝
好吧,我承認我只會最簡單的安裝
sudo apt-get install redis-server
支援包: (其實就一個檔案,搞過來就能用)
sudo apt-get install python-redis
#2、配置
配置一下吧,預設配置檔案在: “/etc/redis/redis.conf”
繫結ip:
“bind 127.0.0.1″ -> “bind 10.0.1.7″
將磁碟同步改為 不同步或每秒同步,一直同步的話太慢了:
“appendfsync always” -> “appendfsync no”
檢查一下後臺執行是否開啟:
“daemonize yes”
或者其他你想設定的,例如:
連線超時時間 : “timeout 300″
執行級別: “loglevel notice” (個人認為預設的這個就挺好,非出現大異常,不用改為debug )
#3、使用
#! /usr/bin/env python
#coding=utf-8
import redis
print redis.__file__
# 連線,可選不同
r = redis.Redis(host='10.0.1.7', port=6379, db=0)
# -------------------------------------------
# 看資訊
info = r.info()
for key in info:
print "%s: %s" % (key, info[key])
# 查資料庫大小
print '\ndbsize: %s' % r.dbsize()
# 看連線
print "ping %s" % r.ping()
# 選資料庫
#r.select(2)
# 移動資料去2資料庫
#r.move('a',2)
# 其他
#r.save('a') # 存資料
#r.lastsave('a') # 取最後一次save時間
#r.flush() #重新整理
#r.shutdown() #關閉所有客戶端,停掉所有服務,退出伺服器
#
#--------------------------------------------
# 它有四種型別: string(key,value)、list(序列)、set(集合)、zset(有序集合,多了一個順序屬性)
# 不知道你用的哪種型別?
# print r.get_type('a') #可以告訴你
# -------------------------------------------
# string操作
print '-'*20
# 塞資料
r['c1'] = 'bar'
#或者
r.set('c2','bar')
#這裡有個 getset屬性,如果為True 可以在存新資料時將上次儲存內容同時搞出來
print 'getset:',r.getset('c2','jj')
#如果你想設定一個遞增的整數 每執行一次它自加1:
print 'incr:',r.incr('a')
#如果你想設定一個遞減的整數 please:
print 'decr:',r.decr('a')
# 取資料
print 'r['']:',r['c1']
#或者
print 'get:',r.get('a')
#或者 同時取一批
print 'mget:',r.mget('c1','c2')
#或者 同時取一批 它們的名字(key)很像 而恰好你又不想輸全部
print 'keys:',r.keys('c*')
#又或者 你只想隨機取一個:
print 'randomkey:',r.randomkey()
# 檢視一個資料有沒有 有 1 無0
print 'existes:',r.exists('a')
# 刪資料 1是刪除成功 0和None是沒這個東西
print 'delete:',r.delete('cc')
# 哦對了 它是支援批次操作的
print 'delete:',r.delete('c1','c2')
# 其他
r.rename('a','c3') #呃.改名
r.expire('c3',10) #讓資料10秒後過期 說實話我不太明白麼意思
r.ttl('c3') #看剩餘過期時間 不存在返回-1
#--------------------------------
# 序列(list)操作
print '-'*20
# 它是兩頭通的
# 塞入
r.push('b','gg')
r.push('b','hh')
# head 屬性控制是不是從另一頭塞
r.push('b','ee',head=True)
# 看長度
print 'list len:',r.llen('b')
# 列出一批出來
print 'list lrange:',r.lrange('b',start=0,end=-1)
# 取出一位
print 'list index 0:',r.lindex('b',0)
# 修剪列表
#若start 大於end,則將這個list清空
print 'list ltrim :',r.ltrim('b',start=0,end=3) #只留 從0到3四位
# 排序
# 這可是個大工程
#--------------------------------
# 集合(set)操作
# 塞資料
r.sadd('s', 'a')
# 判斷一個set長度為多少 不存在為0
r.scard('s')
# 判斷set中一個物件是否存在
r.sismember('s','a')
# 求交集
r.sadd('s2','a')
r.sinter('s1','s2')
#求交集並將結果賦值
r.sinterstore('s3','s1','s2')
# 看一個set物件
r.smembers('s3')
# 求並集
r.sunion('s1','s2')
# 阿 我想聰明的你已經猜到了
#求並集 並將結果返回
r.sunionstore('ss','s1','s2','s3')
# 求不同
# 在s1中有,但在s2和s3中都沒有的數
r.sdiff('s1','s2','s3')
r.sdiffstore('s4','s1','s2')# 這個你懂的
# 取個隨機數
r.srandmember('s1')
#-------------------------------------
#zset 有序set
#'zadd', 'zcard', 'zincr', 'zrange', 'zrangebyscore', 'zrem', 'zscore'
# 分別對應
#新增, 數量, 自加1,取資料,按照積分(範圍)取資料,刪除,取積分
# 我靠 你玩死我了 redis!
# 今天在實驗中,我嘗試插入一條zset型別資料:
r1.zset(u'','liwu',3)
# 插入成功
# 我繼續插入
r1.zset(u'',u'\u9001\u793c',5)
#報錯:
#UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
#這次插入的是禮物的中文詞 unicode編碼
#為什麼會失敗那,這條資料是我從redis裡面取出來然後沒做任何修改再插入的阿
#redis返回和接受的資料型別都是unicode編碼的阿
#好吧,我們再次插入試試
#再次插入
r1.zset('',u'\u9001\u793c',5)
#成功了
#插入
r1.zset('','禮物',5)
#依然成功,跟入redis.py 1024行
return self.send_command('ZADD %s %s %s\r\n%s\r\n' % (
key, score, len(member), member))
# 哦 萬惡的編碼轉換!
#不過取的時候,不論第一個是何種型別的資料都無關係
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2133488/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python下 redis的使用PythonRedis
- 【Python】python使用redis做佇列服務PythonRedis佇列
- python下redis安裝和使用PythonRedis
- 使用python來操作redis用法詳解PythonRedis
- Python操作RedisPythonRedis
- python 操作redisPythonRedis
- Windows下安裝Redis及使用Python操作Redis的方法WindowsRedisPython
- python操作redis(二)PythonRedis
- Python連線RedisPythonRedis
- 【Redis】用python操作redis叢集RedisPython
- redis學習(1)python連線redisRedisPython
- python操作Redis詳解PythonRedis
- python連線redis測試PythonRedis
- 【PyTHON】redis資料遷移PythonRedis
- 【Python學習教程】Python常見面試題之Redis篇!Python面試題Redis
- Python Redis常用操作(持續更新)PythonRedis
- Python 資料庫騷操作 — RedisPython資料庫Redis
- redis在python中的應用RedisPython
- day40-Python連線RedisPythonRedis
- Python 資料庫騷操作 -- RedisPython資料庫Redis
- python redis自帶門神 lock 方法PythonRedis
- python訪問redis的問題PythonRedis
- Python連線Redis連線配置PythonRedis
- 【Python之旅】第七篇(三):使用Redis訂閱服務PythonRedis
- Redis大key掃描Python指令碼RedisPython指令碼
- Python操作Redis快取資料庫PythonRedis快取資料庫
- Python標準庫系列之Redis模組PythonRedis
- Python下操作Memcache/Redis/RabbitMQ說明PythonRedisMQ
- 用Python編寫自己的微型RedisPythonRedis
- Python使用技巧Python
- Python BeautifulSoup 使用Python
- Python namedtuple使用Python
- python curses使用Python
- python-- 使用Python
- python turtle 使用Python
- 【臨實戰】使用 Python 從 Redis 中刪除 4000W 個 KEYPythonRedis
- 使用 Redis 和 Python 構建一個共享單車的應用程式RedisPython
- debug——python redis的 list ,使用 lrem 刪除不了對應的值PythonRedisREM