class myssdb_hash(object): def __init__(self, table, host="localhost", port=8888, **kwargs): self.name = table pool = BlockingConnectionPool(host=host, port=port, **kwargs) self.__conn = SSDB(connection_pool=pool) # put a data def put(self, value, **kwargs): if isinstance(value, dict): key = value.get("_id", str(uuid.uuid1())) elif isinstance(value, basestring): try: tmp = json.loads(value) if isinstance(tmp, dict): key = tmp.get("_id", str(uuid.uuid1())) else: key = str(uuid.uuid1()) except: key = str(uuid.uuid1()) else: key = str(uuid.uuid1()) status = self.__conn.hset(self.name, key, value if isinstance(value, basestring) else json.dumps(value)) self.__conn.hincr("counter_" + self.name, time.strftime("%Y-%m-%d", time.localtime(float(time.time())))) return status # get a data def get(self, **kwargs): tmp = self.__conn.hscan(self.name, "", "", limit=1) if not tmp: return None try: self.__conn.hdel(self.name, tmp.keys()[0]) except Exception as e: print str(e) return None return tmp.values()[0] # table size def size(self, *args, **kwargs): return self.__conn.hsize(self.name) def __list(self): start_key = "" for i in xrange(0, self.size(), 1000): tmp = self.__conn.hscan(self.name, start_key, "", limit=1000) start_key = tmp.keys()[-1] for value in tmp.values(): yield value def get_values(self): return self.__list()
class SSDBHashMap(object, SSDBBase): def __init__(self, hashmap_name, host="127.0.0.1", port=8888, max_connections=10, timeout=60): self.hashmap_name = hashmap_name self.host = host self.port = port self.max_connections = max_connections self.timeout = timeout pool = BlockingConnectionPool(connection_class=Connection, max_connections=max_connections, timeout=timeout, host=host, port=port) self.ssdb = SSDB(connection_pool=pool) SSDBBase.__init__(self, self.ssdb) def set(self, key, value): return self.ssdb.hset(self.hashmap_name, key, value) def get(self, key): return self.ssdb.hget(self.hashmap_name, key) def delete(self, key): return self.ssdb.hdel(self.hashmap_name, key) def keys(self, name_start="", name_end="", limit=10): return self.ssdb.hkeys(self.hashmap_name, name_start, name_end, limit) def exists(self, key): return self.ssdb.hexists(self.hashmap_name, key) def size(self): return self.ssdb.hsize(self.hashmap_name) def list(self, name_start="", name_end="", limit=10): #列出名字处于区间 (name_start, name_end] 的 hashmap return self.ssdb.hlist(name_start, name_end, limit) def scan(self, key_start, key_end="", limit=10): return self.ssdb.hscan(self.hashmap_name, key_start, key_end, limit) def clear(self): return self.ssdb.hclear(self.hashmap_name)
class QueueHash(QueueBase.QueueBase): def __init__(self, name, host='localhost', port=8888, **kwargs): QueueBase.QueueBase.__init__(self, name, host, port) self.__conn = SSDB(connection_pool=BlockingConnectionPool( host=self.host, port=self.port)) @QueueBase.catch def put(self, value, *args, **kwargs): if isinstance(value, dict): key = value.get('_id', str(uuid.uuid1())) elif isinstance(value, basestring): try: tmp = json.loads(value) if isinstance(tmp, dict): key = tmp.get('_id', str(uuid.uuid1())) else: key = str(uuid.uuid1()) except: key = str(uuid.uuid1()) else: key = str(uuid.uuid1()) return self.__conn.hset( self.name, key, value if isinstance(value, basestring) else json.dumps(value, ensure_ascii=False).encode('utf-8')) @QueueBase.catch def get(self, *args, **kwargs): tmp = self.__conn.hscan(self.name, '', '', limit=1) if not tmp: return None try: self.__conn.hdel(self.name, tmp.keys()[0]) except Exception as e: print str(e) return None return tmp.values()[0] @QueueBase.catch def size(self, *args, **kwargs): return self.__conn.hsize(self.name)