当前位置:Gxlcms > 数据库问题 > Python读写Redis数据库

Python读写Redis数据库

时间:2021-07-01 10:21:17 帮助过:1人阅读

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)

上面操作是先写入一条数据,然后再读取,如果写入或者读取数据太多,那么我们最好用批处理,这样效率会更高。

  1. <span style="color: #0000ff;">import</span><span style="color: #000000;"> redis
  2. </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> datetime
  3. </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Database:
  4. </span><span style="color: #0000ff;">def</span> <span style="color: #800080;">__init__</span><span style="color: #000000;">(self):
  5. self.host </span>= <span style="color: #800000;">‘</span><span style="color: #800000;">localhost</span><span style="color: #800000;">‘</span><span style="color: #000000;">
  6. self.port </span>= 6379<span style="color: #000000;">
  7. self.write_pool </span>=<span style="color: #000000;"> {}
  8. </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> add_write(self,website,city,year,month,day,deal_number):
  9. key </span>= <span style="color: #800000;">‘</span><span style="color: #800000;">_</span><span style="color: #800000;">‘</span><span style="color: #000000;">.join([website,city,str(year),str(month),str(day)])
  10. val </span>=<span style="color: #000000;"> deal_number
  11. self.write_pool[key] </span>=<span style="color: #000000;"> val
  12. </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> batch_write(self):
  13. </span><span style="color: #0000ff;">try</span><span style="color: #000000;">:
  14. r </span>= redis.StrictRedis(host=self.host,port=<span style="color: #000000;">self.port)
  15. r.mset(self.write_pool)
  16. </span><span style="color: #0000ff;">except</span><span style="color: #000000;"> Exception, exception:
  17. </span><span style="color: #0000ff;">print</span><span style="color: #000000;"> exception
  18. </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> add_data():
  19. beg </span>=<span style="color: #000000;"> datetime.datetime.now()
  20. db </span>=<span style="color: #000000;"> Database()
  21. </span><span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> range(1,10000<span style="color: #000000;">):
  22. db.add_write(</span><span style="color: #800000;">‘</span><span style="color: #800000;">meituan</span><span style="color: #800000;">‘</span>,<span style="color: #800000;">‘</span><span style="color: #800000;">beijing</span><span style="color: #800000;">‘</span>,2013,i,1<span style="color: #000000;">,i)
  23. db.batch_write()
  24. end </span>=<span style="color: #000000;"> datetime.datetime.now()
  25. </span><span style="color: #0000ff;">print</span> end-<span style="color: #000000;">beg
  26. </span><span style="color: #0000ff;">if</span> <span style="color: #800080;">__name__</span> == <span style="color: #800000;">‘</span><span style="color: #800000;">__main__</span><span style="color: #800000;">‘</span><span style="color: #000000;">:
  27. add_data() </span>

参考文章:  http://www.jb51.net/article/48212.htm

 

Python读写Redis数据库

标签:

人气教程排行