|
1 | 1 | from copy import deepcopy
|
2 | 2 |
|
| 3 | +from aiocache.settings import settings |
| 4 | + |
3 | 5 |
|
4 | 6 | def _class_from_string(class_path):
|
5 | 7 | class_name = class_path.split('.')[-1]
|
@@ -31,96 +33,34 @@ def _create_cache(cache, serializer=None, plugins=None, **kwargs):
|
31 | 33 |
|
32 | 34 | class CacheHandler:
|
33 | 35 |
|
34 |
| - _config = { |
35 |
| - 'default': { |
36 |
| - 'cache': "aiocache.SimpleMemoryCache", |
37 |
| - 'serializer': { |
38 |
| - 'class': "aiocache.serializers.DefaultSerializer" |
39 |
| - } |
40 |
| - } |
41 |
| - } |
42 |
| - |
43 | 36 | def __init__(self):
|
44 | 37 | self._caches = {}
|
45 | 38 |
|
| 39 | + @classmethod |
| 40 | + def _get_alias_config(cls, alias): |
| 41 | + config = settings.get_config() |
| 42 | + if alias not in config: |
| 43 | + raise KeyError( |
| 44 | + "Could not find config for '{}' in settings, ensure you called settings.from_config" |
| 45 | + " specifying the config for that cache".format(alias)) |
| 46 | + |
| 47 | + return config[alias] |
| 48 | + |
46 | 49 | def get(self, alias):
|
47 | 50 | try:
|
48 | 51 | return self._caches[alias]
|
49 | 52 | except KeyError:
|
50 | 53 | pass
|
51 | 54 |
|
52 |
| - config = self.get_alias_config(alias) |
| 55 | + config = self._get_alias_config(alias) |
53 | 56 | cache = _create_cache(**deepcopy(config))
|
54 | 57 | self._caches[alias] = cache
|
55 | 58 | return cache
|
56 | 59 |
|
57 | 60 | def create(self, alias, **kwargs):
|
58 |
| - config = self.get_alias_config(alias) |
| 61 | + config = self._get_alias_config(alias) |
59 | 62 | cache = _create_cache(**{**config, **kwargs})
|
60 | 63 | return cache
|
61 | 64 |
|
62 |
| - def get_alias_config(self, alias): |
63 |
| - config = self.get_config() |
64 |
| - if alias not in config: |
65 |
| - raise KeyError( |
66 |
| - "Could not find config for '{0}', ensure you include {0} when calling" |
67 |
| - "caches.set_config specifying the config for that cache".format(alias)) |
68 |
| - |
69 |
| - return config[alias] |
70 |
| - |
71 |
| - def get_config(self): |
72 |
| - """ |
73 |
| - Return copy of current stored config |
74 |
| - """ |
75 |
| - return deepcopy(self._config) |
76 |
| - |
77 |
| - def set_config(self, config): |
78 |
| - """ |
79 |
| - Set (override) the default config for cache aliases from a dict-like structure. |
80 |
| - The structure is the following:: |
81 |
| -
|
82 |
| - { |
83 |
| - 'default': { |
84 |
| - 'cache': "aiocache.SimpleMemoryCache", |
85 |
| - 'serializer': { |
86 |
| - 'class': "aiocache.serializers.DefaultSerializer" |
87 |
| - } |
88 |
| - }, |
89 |
| - 'redis_alt': { |
90 |
| - 'cache': "aiocache.RedisCache", |
91 |
| - 'endpoint': "127.0.0.10", |
92 |
| - 'port': 6378, |
93 |
| - 'serializer': { |
94 |
| - 'class': "aiocache.serializers.PickleSerializer" |
95 |
| - }, |
96 |
| - 'plugins': [ |
97 |
| - {'class': "aiocache.plugins.HitMissRatioPlugin"}, |
98 |
| - {'class': "aiocache.plugins.TimingPlugin"} |
99 |
| - ] |
100 |
| - } |
101 |
| - } |
102 |
| -
|
103 |
| - 'default' key must always exist when passing a new config. Default configuration |
104 |
| - is:: |
105 |
| -
|
106 |
| - { |
107 |
| - 'default': { |
108 |
| - 'cache': "aiocache.SimpleMemoryCache", |
109 |
| - 'serializer': { |
110 |
| - 'class': "aiocache.serializers.DefaultSerializer" |
111 |
| - } |
112 |
| - } |
113 |
| - } |
114 |
| -
|
115 |
| - You can set your own classes there. |
116 |
| - The class params accept both str and class types. |
117 |
| -
|
118 |
| - All keys in the config are optional, if they are not passed the defaults |
119 |
| - for the specified class will be used. |
120 |
| - """ |
121 |
| - if "default" not in config: |
122 |
| - raise ValueError("default config must be provided") |
123 |
| - self._config = config |
124 |
| - |
125 | 65 |
|
126 | 66 | caches = CacheHandler()
|
0 commit comments