def enable_instrumentation(self):
        if isinstance(middleware_cache.caches, CacheHandlerPatch):
            cache.caches = middleware_cache.caches
        else:
            cache.caches = CacheHandlerPatch()

        # Wrap the patched cache inside Django's ConnectionProxy
        if ConnectionProxy:
            cache.cache = ConnectionProxy(cache.caches, DEFAULT_CACHE_ALIAS)
    def enable_instrumentation(self):
        for alias in cache.caches:
            if not isinstance(cache.caches[alias], CacheStatTracker):
                cache.caches[alias] = CacheStatTracker(cache.caches[alias])

        if not isinstance(middleware_cache.caches, CacheHandlerPatch):
            middleware_cache.caches = cache.caches

        # Wrap the patched cache inside Django's ConnectionProxy
        if ConnectionProxy:
            cache.cache = ConnectionProxy(cache.caches, DEFAULT_CACHE_ALIAS)
Example #3
0
        backend = params.pop("BACKEND")
        location = params.pop("LOCATION", "")
        try:
            backend_cls = import_string(backend)
        except ImportError as e:
            raise InvalidCacheBackendError("Could not find backend '%s': %s" %
                                           (backend, e)) from e
        return backend_cls(location, params)

    def all(self, initialized_only=False):
        return [
            self[alias] for alias in self
            # If initialized_only is True, return only initialized caches.
            if not initialized_only or hasattr(self._connections, alias)
        ]


caches = CacheHandler()

cache = ConnectionProxy(caches, DEFAULT_CACHE_ALIAS)


def close_caches(**kwargs):
    # Some caches need to do a cleanup at the end of a request cycle. If not
    # implemented in a particular backend cache.close() is a no-op.
    for cache in caches.all(initialized_only=True):
        cache.close()


signals.request_finished.connect(close_caches)
Example #4
0
    "ProgrammingError",
    "DataError",
    "NotSupportedError",
    "Error",
    "InterfaceError",
    "OperationalError",
    "DEFAULT_DB_ALIAS",
    "DJANGO_VERSION_PICKLE_KEY",
]

connections = ConnectionHandler()

router = ConnectionRouter()

# For backwards compatibility. Prefer connections['default'] instead.
connection = ConnectionProxy(connections, DEFAULT_DB_ALIAS)


# Register an event to reset saved queries when a Django request is started.
def reset_queries(**kwargs):
    for conn in connections.all(initialized_only=True):
        conn.queries_log.clear()


signals.request_started.connect(reset_queries)


# Register an event to reset transaction state and close connections past
# their lifetime.
def close_old_connections(**kwargs):
    for conn in connections.all(initialized_only=True):
 def disable_instrumentation(self):
     for alias in cache.caches:
         if isinstance(cache.caches[alias], CacheStatTracker):
             cache.caches[alias] = cache.caches[alias].cache
     if ConnectionProxy:
         cache.cache = ConnectionProxy(cache.caches, DEFAULT_CACHE_ALIAS)