from redis import Redis


class RedisHashMap(object):

    def __init__(self, conn: Redis, key: str):
        self.conn = conn
        self.key = key

    def __setitem__(self, field, value):
        self.conn.hset(self.key, field, value)

    def __getitem__(self, field):
        return bytes.decode(self.conn.hget(self.key, field))

    def __delitem__(self, field):
        self.conn.hdel(self.key, field)

    def __contains__(self, field):
        self.conn.hexists(self.key, field)

    def delete(self):
        self.conn.delete(self.key)
