Created
July 27, 2010 04:13
-
-
Save gleicon/491726 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# redict - redis based dictionary | |
# this version uses UserDict and a mostly cached redis data for low I/O impact | |
# almost always consistent dict. to make sure you have all latest data run __sync() periodically | |
# gleicon 2010 - http://zenmachine.wordpress.com - http://github.com/gleicon - http://7co.cc | |
import redis | |
from UserDict import UserDict | |
import time | |
class ReDict(UserDict): | |
def __init__(self, server='127.0.0.1', port=6379, hkey=None): | |
self.server = server | |
self.port = port | |
self.redis_conn = redis.Redis(host=self.server, port=self.port) | |
UserDict.__init__(self) | |
if hkey == None: | |
self.hkey = "redict:%s" % time.time() | |
self.data={} | |
else: | |
self.hkey = hkey | |
self.__sync | |
def __setitem__(self, key, value): | |
self.redis_conn.hset(self.hkey, key, value) | |
UserDict.__setitem__(self, key, value) | |
self.data = self.redis_conn.hgetall(self.hkey) | |
def __sync(self): | |
self.data = self.redis_conn.hgetall(self.hkey) | |
if __name__ == '__main__': | |
#rd = ReDict(hkey="lero") | |
rd = ReDict() | |
rd['oi'] = 1 | |
rd['ei'] = 10 | |
rd['ai'] = 100 | |
print rd['oi'] | |
print rd.has_key('oi') | |
print rd.has_key('xei') | |
print rd.keys() | |
print rd.values() | |
print rd.items() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment