日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python读取redis存储数据的存储时间_Python读写Redis数据库操作示例

發(fā)布時間:2025/3/15 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python读取redis存储数据的存储时间_Python读写Redis数据库操作示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

使用Python如何操作Redis呢?下面用實例來說明用Python讀寫Redis數(shù)據(jù)庫。

比如,我們插入一條數(shù)據(jù),如下:

import redis

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)

上面操作是先寫入一條數(shù)據(jù),然后再讀取,如果寫入或者讀取數(shù)據(jù)太多,那么我們最好用批處理,這樣效率會更高。

import redis

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()

總結(jié)

以上是生活随笔為你收集整理的python读取redis存储数据的存储时间_Python读写Redis数据库操作示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。