﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
20315	Default cache instance is memoized, but no other cache instance is	acjohnson55	nobody	"It seems odd to me that the default cache instance is stored forever at module load-time, but that any cache instance retrieved by get_cache is recreated every time. Is there a reason get_cache is not memoized but the default cache is? I ask because I'm working on a library that rides on top of the caching library, and I'm trying to make sure there's no danger in memoizing cache instances, as django.core.cache.cache is. If the cache object is expensive to create, this could really make a difference.

From django/core/cache/__init__.py:

{{{
def get_cache(backend, **kwargs):
    try:
        if '://' in backend:
            # for backwards compatibility
            backend, location, params = parse_backend_uri(backend)
            if backend in BACKENDS:
                backend = 'django.core.cache.backends.%s' % BACKENDS[backend]
            params.update(kwargs)
            mod = importlib.import_module(backend)
            backend_cls = mod.CacheClass
        else:
            backend, location, params = parse_backend_conf(backend, **kwargs)
            backend_cls = import_by_path(backend)
    except (AttributeError, ImportError, ImproperlyConfigured) as e:
        raise InvalidCacheBackendError(
            ""Could not find backend '%s': %s"" % (backend, e))
    cache = backend_cls(location, params)
    # Some caches -- python-memcached in particular -- 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
    signals.request_finished.connect(cache.close)
    return cache

cache = get_cache(DEFAULT_CACHE_ALIAS)
}}}"	Cleanup/optimization	closed	Core (Cache system)	1.5	Normal	invalid			Unreviewed	0	0	0	0	0	0
