def local_memory_cache(monkeypatch): """Configure settings.CACHES to use LocMemCache.""" monkeypatch.setitem( settings.CACHES, 'default', {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}, ) monkeypatch.setattr('django.core.cache.caches', CacheHandler())
What this does is make sure that ?q=1&q=2&a=4&a=6 gets the same cache key as ?a=6&a=4&a=2&q=1 and thus is cached regardless of the order of the querystring parameters. It will sort alphabetically the GET parameters and then sort alphabetically their values to compute the querystring. """ from collections import OrderedDict import hashlib from django.core.cache import CacheHandler, DEFAULT_CACHE_ALIAS from django.middleware.cache import UpdateCacheMiddleware, FetchFromCacheMiddleware from django.utils.cache import patch_response_headers, get_max_age, has_vary_header, _generate_cache_header_key, \ _i18n_cache_key_suffix, cc_delim_re from django.conf import settings from django.utils.decorators import decorator_from_middleware_with_args from django.utils.encoding import force_bytes caches = CacheHandler() def _generate_orderless_cache_url(request): sorted_keys = [k for k in sorted(request.GET.keys(), key=lambda k: k)] sorted_arr = OrderedDict() sorted_query_string = "?" ampersand = "" counter = 0 for key in sorted_keys: sorted_arr[key] = [v for v in sorted(request.GET.getlist(key))] for value in sorted_arr[key]: if counter > 0: ampersand = "&"