postgresql_backend: cache ContentType based on the manager object and model classes (#32248)

model classes and manager object are duplicated during migrations.
This commit is contained in:
Benjamin Dauvergne 2019-04-12 17:13:46 +02:00
parent 631743c45e
commit 22d7ace8c0
1 changed files with 3 additions and 2 deletions

View File

@ -185,14 +185,15 @@ class ContentTypeCacheDescriptor(object):
# use weak for transient Manager
owner._thread_local_cache._cache = weakref.WeakKeyDictionary()
global_cache = owner._thread_local_cache._cache
get_cache = global_cache.get(owner)
model_cache = global_cache.setdefault(obj, weakref.WeakKeyDictionary())
get_cache = model_cache.get(obj.model)
if not get_cache:
# use an LRU cache to evict dead tenants with time and to prevent
# bloat with lot of tenants
@lru_cache(maxsize=200)
def get_cache(schema_name):
return {}
global_cache[owner] = get_cache
global_cache[obj.model] = get_cache
tenant = getattr(connection, 'tenant', None)
schema_name = getattr(tenant, 'schema_name', 'public')
return get_cache(schema_name)