def type(self, key): if self._db.get(KEY_CODEC.encode_string(key)) is not None: return 'string' if self._db.get(KEY_CODEC.encode_set(key)) is not None: return 'set' if self._db.get(KEY_CODEC.encode_hash(key)) is not None: return 'hash' if self._db.get(KEY_CODEC.encode_zset(key)) is not None: return 'zset' return 'none'
def rename(self, old_name, new_name): if self.exists(old_name): if old_name == new_name: return # replace the key that holds the key ID and don't touch the rest key_type = self.type(old_name) if key_type == 'zset': old_db_key = KEY_CODEC.encode_zset(old_name) new_db_key = KEY_CODEC.encode_zset(new_name) elif key_type == 'hash': old_db_key = KEY_CODEC.encode_hash(old_name) new_db_key = KEY_CODEC.encode_hash(new_name) elif key_type == 'set': old_db_key = KEY_CODEC.encode_set(old_name) new_db_key = KEY_CODEC.encode_set(new_name) elif key_type == 'string': old_db_key = KEY_CODEC.encode_string(old_name) new_db_key = KEY_CODEC.encode_string(new_name) else: raise DredisError("invalid key type") self._replace_db_key(new_db_key, old_db_key) else: raise NoKeyError()
def delete(self, *keys): result = 0 for key in keys: if self._db.get(KEY_CODEC.encode_string(key)) is not None: self._delete_db_string(key) result += 1 elif self._db.get(KEY_CODEC.encode_set(key)) is not None: self._delete_db_set(key) result += 1 elif self._db.get(KEY_CODEC.encode_hash(key)) is not None: self._delete_db_hash(key) result += 1 elif self._db.get(KEY_CODEC.encode_zset(key)) is not None: self._delete_db_zset(key) result += 1 return result
def set(self, key, value): self._db.put(KEY_CODEC.encode_string(key), value)
def get(self, key): return self._db.get(KEY_CODEC.encode_string(key))
def _delete_db_string(self, key): # there is one set of db keys for strings: # * string self._db.delete(KEY_CODEC.encode_string(key))