class DarkMaster(object): def __init__(self, db_user, db_passwd, db_store, db_hosts): """Description: Init method for the database connection manager :param db_user: The username for the database :type str: :param db_passwd: Password :type str: :param db_store: The name of the store you need to connect to :type str: :param db_hosts: A list of db hosts and the ports they run the database on :type list: List of host/port tuples like this: [('127.0.0.1', '6666'), ('192.168.100.105', '6666')] """ self.db_user = db_user self.db_passwd = db_passwd self.db_store = db_store self.db_hosts = db_hosts self._store = None @property def store(self): if self._store is None: self._connect() return self._store def _connect(self): self._store = StoreClient(self.db_store, self.db_hosts) def get_object(self, id): return self.store.get(id) def delete(self, id): self.store.delete(id) return True def all(self): self._store.get_all()
s = StoreClient('test', [('localhost', 6666)]) version = s.put("hello", "1") assert s.get("hello")[0][0] == "1" s.put("hello", "2", version) assert s.get("hello")[0][0] == "2" s.put("hello", "3") assert s.get("hello")[0][0] == "3" s.delete("hello") assert len(s.get("hello")) == 0 ## test get_all pairs = [("a1", "1"), ("a2", "2"), ("a3", "3"), ("a4", "4")] for k, v in pairs: s.put(k, v) vals = s.get_all([k for k, v in pairs]) for k, v in pairs: assert vals[k][0][0] == v requests = 10000 ## Time get requests s.put("hello", "world") start = time.time() for i in xrange(requests): s.get('hello') print requests/(time.time() - start), ' get requests per second' ## Time put requests version = s.put('abc', 'def') start = time.time()