Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/core/cache/__init__.py: 94%

26 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1""" 

2Caching framework. 

3 

4This package defines set of cache backends that all conform to a simple API. 

5In a nutshell, a cache is a set of values -- which can be any object that 

6may be pickled -- identified by string keys. For the complete API, see 

7the abstract BaseCache class in django.core.cache.backends.base. 

8 

9Client code should use the `cache` variable defined here to access the default 

10cache backend and look up non-default cache backends in the `caches` dict-like 

11object. 

12 

13See docs/topics/cache.txt for information on the public API. 

14""" 

15from django.core import signals 

16from django.core.cache.backends.base import ( 

17 BaseCache, 

18 CacheKeyWarning, 

19 InvalidCacheBackendError, 

20 InvalidCacheKey, 

21) 

22from django.utils.connection import BaseConnectionHandler, ConnectionProxy 

23from django.utils.module_loading import import_string 

24 

25__all__ = [ 

26 "cache", 

27 "caches", 

28 "DEFAULT_CACHE_ALIAS", 

29 "InvalidCacheBackendError", 

30 "CacheKeyWarning", 

31 "BaseCache", 

32 "InvalidCacheKey", 

33] 

34 

35DEFAULT_CACHE_ALIAS = "default" 

36 

37 

38class CacheHandler(BaseConnectionHandler): 

39 settings_name = "CACHES" 

40 exception_class = InvalidCacheBackendError 

41 

42 def create_connection(self, alias): 

43 params = self.settings[alias].copy() 

44 backend = params.pop("BACKEND") 

45 location = params.pop("LOCATION", "") 

46 try: 

47 backend_cls = import_string(backend) 

48 except ImportError as e: 

49 raise InvalidCacheBackendError( 

50 "Could not find backend '%s': %s" % (backend, e) 

51 ) from e 

52 return backend_cls(location, params) 

53 

54 def all(self, initialized_only=False): 

55 return [ 

56 self[alias] 

57 for alias in self 

58 # If initialized_only is True, return only initialized caches. 

59 if not initialized_only or hasattr(self._connections, alias) 

60 ] 

61 

62 

63caches = CacheHandler() 

64 

65cache = ConnectionProxy(caches, DEFAULT_CACHE_ALIAS) 

66 

67 

68def close_caches(**kwargs): 

69 # Some caches need to do a cleanup at the end of a request cycle. If not 

70 # implemented in a particular backend cache.close() is a no-op. 

71 for cache in caches.all(initialized_only=True): 

72 cache.close() 

73 

74 

75signals.request_finished.connect(close_caches)