from redis import StrictRedis


class TenantRedisClient:
    """Wraps a shared ``StrictRedis``, prefixing every key with ``{tenant}:``.

    One ``StrictRedis`` connection pool is shared across tenants; each tenant
    gets its own lightweight ``TenantRedisClient`` instance so that keys are
    automatically scoped without the caller having to care about it.
    """

    def __init__(self, client: StrictRedis, tenant: str):
        self._client = client
        self._tenant = tenant

    def _key(self, name: str) -> str:
        return f"{self._tenant}:{name}"

    def lock(self, name, timeout=None, blocking_timeout=None, **kwargs):
        return self._client.lock(
            name=self._key(name),
            timeout=timeout,
            blocking_timeout=blocking_timeout,
            **kwargs,
        )

    def get(self, name):
        return self._client.get(self._key(name))

    def set(self, name, value, **kwargs):
        return self._client.set(self._key(name), value, **kwargs)

    def delete(self, *names):
        return self._client.delete(*(self._key(n) for n in names))
