def update_cache_from_env(cache_dict): from django.db import connection cache_prefix = os.environ.get("TENANT_CACHE_PREFIX") if cache_prefix is not None: cache_dict["KEY_PREFIX"] = cache_prefix connection.get_threadlocal().set_cache_prefix(cache_prefix)
def process_response(self, request, response): """Clears the database name and cache prefix on response. This is a precaution against the connection being reused without first calling set_dbname or set_tenant_name. """ connection.get_threadlocal().reset() return response
def update_database_from_env(db_dict): from django.db import connection dbname = os.environ.get("TENANT_DATABASE_NAME") if dbname: db_dict["NAME"] = dbname connection.get_threadlocal().set_dbname(dbname)
def process_response(self, request, response): """Clears the database name and cache prefix on response. This is a precaution against the connection being reused without first calling set_dbname. """ connection.get_threadlocal().reset() return response
def process_request(self, request): mapper = utils.get_mapper() threadlocal = connection.get_threadlocal() threadlocal.set_tenant_name(mapper.get_tenant_name(request)) threadlocal.set_dbname(mapper.get_dbname(request)) threadlocal.set_cache_prefix(mapper.get_cache_prefix(request)) if 'django.contrib.sites' in settings.INSTALLED_APPS: # Clear the sites framework cache. from django.contrib.sites.models import Site Site.objects.clear_cache()
def process_request(self, request): mapper = utils.get_mapper() threadlocal = connection.get_threadlocal() tenant_name = mapper.get_tenant_name(request) threadlocal.set_tenant_name(tenant_name) db_name = mapper.get_db_name(request, tenant_name) # Fixes ValueError when trying to inject SQL, setting empty string as default try: threadlocal.set_db_name(db_name) except ValueError as ve: threadlocal.set_db_name('') threadlocal.set_cache_prefix(mapper.get_cache_prefix(request, tenant_name, db_name)) if 'django.contrib.sites' in settings.INSTALLED_APPS: # Clear the sites framework cache. from django.contrib.sites.models import Site Site.objects.clear_cache()
def update_tenant_name_from_env(): from django.db import connection tenant_name = os.environ.get('TENANT_NAME') if tenant_name: connection.get_threadlocal().set_tenant_name(tenant_name)
def update_cache_from_env(cache_settings): from django.db import connection cache_prefix = os.environ.get('TENANT_CACHE_PREFIX') if cache_prefix is not None and cache_settings is not None: connection.get_threadlocal().set_cache_prefix(cache_prefix)
def update_database_from_env(database_settings): from django.db import connection db_name = os.environ.get('TENANT_DATABASE_NAME') if db_name and database_settings is not None: database_settings['NAME'] = db_name connection.get_threadlocal().set_db_name(db_name)
def multitenant_key_func(key, key_prefix, version): tenant_prefix = connection.get_threadlocal().get_cache_prefix() if tenant_prefix is None: raise ImproperlyConfigured('Multi-tenant cache prefix not available') return '%s:%s:%s:%s' % (tenant_prefix, key_prefix, version, key)
def update_database_from_env(db_dict): from django.db import connection dbname = os.environ.get('TENANT_DATABASE_NAME') if dbname: db_dict['NAME'] = dbname connection.get_threadlocal().set_dbname(dbname)
def update_cache_from_env(cache_settings): from django.db import connection cache_prefix = os.environ.get('TENANT_CACHE_PREFIX') if cache_prefix is not None and cache_settings is not None: cache_settings['KEY_PREFIX'] = cache_prefix connection.get_threadlocal().set_cache_prefix(cache_prefix)
def update_cache_from_env(cache_dict): from django.db import connection cache_prefix = os.environ.get('TENANT_CACHE_PREFIX') if cache_prefix is not None: cache_dict['KEY_PREFIX'] = cache_prefix connection.get_threadlocal().set_cache_prefix(cache_prefix)
def get_threadlocal(): if get_mode() == "SCHEMA": return connection.get_threadlocal() else: # mode == "DATABASE": return my_local_global