Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making CONSTANCE_DATABASE_CACHE_BACKEND tenant aware #482

Open
nv0284 opened this issue May 16, 2022 · 2 comments
Open

Making CONSTANCE_DATABASE_CACHE_BACKEND tenant aware #482

nv0284 opened this issue May 16, 2022 · 2 comments

Comments

@nv0284
Copy link

nv0284 commented May 16, 2022

Describe the problem

I have added constance in my TENANT_APPS that will create constance tables for each tenant in db. But in case of caching, the redis is shared between tenants. How can I make the caching of constance config tenant aware?

@sergei-iurchenko
Copy link
Contributor

What exactly tenant library do you use?
You can use database backend and it should work out of box.
May be this option for cache help backend https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-CACHES-KEY_PREFIX

@welliot
Copy link

welliot commented Jul 23, 2023

I had similar issue with django-tenants. The database worked perfectly as connection hits the constance_config table in correct schema for tenant. However when I cache in Redis it has key collision. As tenants share an instance of django KEY_PREFIX doesn't not solve issue. I worked around with the following and pointed CONSTANCE_BACKEND at this class.

from constance.backends.database import DatabaseBackend
from django.db import connection

class ConstanceTenantDatabaseBackend(DatabaseBackend):
    def add_prefix(self, key):
        tenant = connection.tenant
        return "%s:%s:%s" % (tenant.schema_name, self._prefix, key)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants