Replace the cache dictionnary by a thread local variable (~ #9891)

This makes it safe to use multitenancy with threads.
This commit is contained in:
Benjamin Dauvergne 2016-02-06 15:03:10 +01:00 committed by Frédéric Péters
parent 1c57786387
commit eb0a27e712
1 changed files with 12 additions and 1 deletions

View File

@ -1,9 +1,10 @@
import re
import warnings
import psycopg2
import threading
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.models import ContentType, ContentTypeManager
from django.core.exceptions import ImproperlyConfigured, ValidationError
import django.db.utils
@ -173,3 +174,13 @@ class FakeTenant:
"""
def __init__(self, schema_name):
self.schema_name = schema_name
# Make the ContentType cache tenant and thread safe
ContentTypeManager._thread_local_cache = threading.local()
class ContentTypeCacheDescriptor(object):
def __get__(self, obj):
if not hasattr(obj._thread_local_cache, '_cache'):
obj._thread_local_cache._cache = {}
return obj._thread_local_cache._cache
ContentTypeManager._cache = ContentTypeCacheDescriptor()