|
1 | 1 | from copy import deepcopy
|
2 | 2 |
|
3 |
| -from aiocache.settings import settings |
4 |
| - |
5 | 3 |
|
6 | 4 | def _class_from_string(class_path):
|
7 | 5 | class_name = class_path.split('.')[-1]
|
@@ -33,34 +31,96 @@ def _create_cache(cache, serializer=None, plugins=None, **kwargs):
|
33 | 31 |
|
34 | 32 | class CacheHandler:
|
35 | 33 |
|
| 34 | + _config = { |
| 35 | + 'default': { |
| 36 | + 'cache': "aiocache.SimpleMemoryCache", |
| 37 | + 'serializer': { |
| 38 | + 'class': "aiocache.serializers.DefaultSerializer" |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
36 | 43 | def __init__(self):
|
37 | 44 | self._caches = {}
|
38 | 45 |
|
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 |
| - |
49 | 46 | def get(self, alias):
|
50 | 47 | try:
|
51 | 48 | return self._caches[alias]
|
52 | 49 | except KeyError:
|
53 | 50 | pass
|
54 | 51 |
|
55 |
| - config = self._get_alias_config(alias) |
| 52 | + config = self.get_alias_config(alias) |
56 | 53 | cache = _create_cache(**deepcopy(config))
|
57 | 54 | self._caches[alias] = cache
|
58 | 55 | return cache
|
59 | 56 |
|
60 | 57 | def create(self, alias, **kwargs):
|
61 |
| - config = self._get_alias_config(alias) |
| 58 | + config = self.get_alias_config(alias) |
62 | 59 | cache = _create_cache(**{**config, **kwargs})
|
63 | 60 | return cache
|
64 | 61 |
|
| 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 | + |
65 | 125 |
|
66 | 126 | caches = CacheHandler()
|
0 commit comments