Python讀寫(xiě)Redis數(shù)據(jù)庫(kù)操作示例
比如,我們插入一條數(shù)據(jù),如下:
class Database:
def __init__(self):
self.host = 'localhost'
self.port = 6379
def write(self,website,city,year,month,day,deal_number):
try:
key = '_'.join([website,city,str(year),str(month),str(day)])
val = deal_number
r = redis.StrictRedis(host=self.host,port=self.port)
r.set(key,val)
except Exception, exception:
print exception
def read(self,website,city,year,month,day):
try:
key = '_'.join([website,city,str(year),str(month),str(day)])
r = redis.StrictRedis(host=self.host,port=self.port)
value = r.get(key)
print value
return value
except Exception, exception:
print exception
if __name__ == '__main__':
db = Database()
db.write('meituan','beijing',2013,9,1,8000)
db.read('meituan','beijing',2013,9,1)
上面操作是先寫(xiě)入一條數(shù)據(jù),然后再讀取,如果寫(xiě)入或者讀取數(shù)據(jù)太多,那么我們最好用批處理,這樣效率會(huì)更高。
import datetime
class Database:
def __init__(self):
self.host = 'localhost'
self.port = 6379
self.write_pool = {}
def add_write(self,website,city,year,month,day,deal_number):
key = '_'.join([website,city,str(year),str(month),str(day)])
val = deal_number
self.write_pool[key] = val
def batch_write(self):
try:
r = redis.StrictRedis(host=self.host,port=self.port)
r.mset(self.write_pool)
except Exception, exception:
print exception
def add_data():
beg = datetime.datetime.now()
db = Database()
for i in range(1,10000):
db.add_write('meituan','beijing',2013,i,1,i)
db.batch_write()
end = datetime.datetime.now()
print end-beg
if __name__ == '__main__':
add_data()
- Python操作Redis之設(shè)置key的過(guò)期時(shí)間實(shí)例代碼
- python操作redis的方法
- python操作redis方法總結(jié)
- Python操作redis實(shí)例小結(jié)【String、Hash、List、Set等】
- python實(shí)現(xiàn)redis三種cas事務(wù)操作
- python筆記:mysql、redis操作方法
- 基于python實(shí)現(xiàn)操作redis及消息隊(duì)列
- Python 抓取數(shù)據(jù)存儲(chǔ)到Redis中的操作
- Python訪問(wèn)Redis的詳細(xì)操作
- Python操作Redis數(shù)據(jù)庫(kù)的超詳細(xì)教程
相關(guān)文章
Python時(shí)間處理模塊time和datetime詳解
本文詳細(xì)介紹了Python中常用的時(shí)間處理模塊time和datetime,time模塊提供多種時(shí)間獲取和轉(zhuǎn)換功能,datetime模塊則在time的基礎(chǔ)上增加了日期和時(shí)間的組合處理,如datetime.now()獲取當(dāng)前日期時(shí)間,兩個(gè)模塊在日常編程中非常有用,尤其是在需要時(shí)間日期計(jì)算和轉(zhuǎn)換的場(chǎng)景下2024-10-10
pycharm 如何跳出服務(wù)器證書(shū)不受信任的提示
這篇文章主要介紹了pycharm 跳出服務(wù)器證書(shū)不受信任的提示操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
opencv3/C++實(shí)現(xiàn)視頻背景去除建模(BSM)
今天小編就為大家分享一篇opencv3/C++實(shí)現(xiàn)視頻背景去除建模(BSM),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
pytorch中交叉熵?fù)p失(nn.CrossEntropyLoss())的計(jì)算過(guò)程詳解
今天小編就為大家分享一篇pytorch中交叉熵?fù)p失(nn.CrossEntropyLoss())的計(jì)算過(guò)程詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
windows server 2008 r2 標(biāo)準(zhǔn)版安裝python環(huán)境
本文主要介紹了windows server 2008 r2 標(biāo)準(zhǔn)版安裝python環(huán)境,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
python 獲取頁(yè)面表格數(shù)據(jù)存放到csv中的方法
今天小編就為大家分享一篇python 獲取頁(yè)面表格數(shù)據(jù)存放到csv中的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)實(shí)現(xiàn)查找兩個(gè)字典相同點(diǎn)的方法
這篇文章主要介紹了Python實(shí)現(xiàn)查找兩個(gè)字典相同點(diǎn)的方法,涉及Python常見(jiàn)集合運(yùn)算操作技巧,需要的朋友可以參考下2018-02-02
Python爬蟲(chóng)基于lxml解決數(shù)據(jù)編碼亂碼問(wèn)題
這篇文章主要介紹了Python爬蟲(chóng)基于lxml解決數(shù)據(jù)編碼亂碼問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07

