Skip to content

Commit 30c9818

Browse files
committed
Revert "Revert "Deprecate settings module""
This reverts commit 156f03c.
1 parent 156f03c commit 30c9818

File tree

7 files changed

+145
-78
lines changed

7 files changed

+145
-78
lines changed

aiocache/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .log import logger
2-
from .settings import settings
32
from .backends.memory import SimpleMemoryCache
43
from .factory import caches
4+
from .settings import settings
55
from .decorators import cached, multi_cached
66
from ._version import __version__
77

aiocache/factory.py

+74-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from copy import deepcopy
22

3-
from aiocache.settings import settings
4-
53

64
def _class_from_string(class_path):
75
class_name = class_path.split('.')[-1]
@@ -33,34 +31,96 @@ def _create_cache(cache, serializer=None, plugins=None, **kwargs):
3331

3432
class CacheHandler:
3533

34+
_config = {
35+
'default': {
36+
'cache': "aiocache.SimpleMemoryCache",
37+
'serializer': {
38+
'class': "aiocache.serializers.DefaultSerializer"
39+
}
40+
}
41+
}
42+
3643
def __init__(self):
3744
self._caches = {}
3845

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-
4946
def get(self, alias):
5047
try:
5148
return self._caches[alias]
5249
except KeyError:
5350
pass
5451

55-
config = self._get_alias_config(alias)
52+
config = self.get_alias_config(alias)
5653
cache = _create_cache(**deepcopy(config))
5754
self._caches[alias] = cache
5855
return cache
5956

6057
def create(self, alias, **kwargs):
61-
config = self._get_alias_config(alias)
58+
config = self.get_alias_config(alias)
6259
cache = _create_cache(**{**config, **kwargs})
6360
return cache
6461

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+
65125

66126
caches = CacheHandler()

aiocache/settings.py

+8-52
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from copy import deepcopy
1+
from aiocache import caches
2+
from aiocache.log import logger
23

34

45
class settings:
@@ -20,60 +21,15 @@ def __new__(cls):
2021

2122
@classmethod
2223
def get_alias(cls, alias):
23-
return cls._config[alias]
24+
logger.warning("Deprecated, please use 'aiocache.caches.get_config[alias]'")
25+
return caches.get_alias_config(alias)
2426

2527
@classmethod
2628
def get_config(cls):
27-
"""
28-
Return copy of current stored config
29-
"""
30-
return deepcopy(cls._config)
29+
logger.warning("Deprecated, please use 'aiocache.caches.get_config'")
30+
return caches.get_config()
3131

3232
@classmethod
3333
def set_config(cls, config):
34-
"""
35-
Set (override) the default settings for aiocache from a dict-like structure.
36-
The structure is the following::
37-
38-
{
39-
'default': {
40-
'cache': "aiocache.SimpleMemoryCache",
41-
'serializer': {
42-
'class': "aiocache.serializers.DefaultSerializer"
43-
}
44-
},
45-
'redis_alt': {
46-
'cache': "aiocache.RedisCache",
47-
'endpoint': "127.0.0.10",
48-
'port': 6378,
49-
'serializer': {
50-
'class': "aiocache.serializers.PickleSerializer"
51-
},
52-
'plugins': [
53-
{'class': "aiocache.plugins.HitMissRatioPlugin"},
54-
{'class': "aiocache.plugins.TimingPlugin"}
55-
]
56-
}
57-
}
58-
59-
'default' key must always exist when passing a new config. Default configuration
60-
is::
61-
62-
{
63-
'default': {
64-
'cache': "aiocache.SimpleMemoryCache",
65-
'serializer': {
66-
'class': "aiocache.serializers.DefaultSerializer"
67-
}
68-
}
69-
}
70-
71-
You can set your own classes there.
72-
The class params accept both str and class types.
73-
74-
All keys in the config are optional, if they are not passed the defaults
75-
for the specified class will be used.
76-
"""
77-
if "default" not in config:
78-
raise ValueError("default config must be provided")
79-
cls._config = config
34+
logger.warning("Deprecated, please use 'aiocache.caches.get_config'")
35+
return caches.set_config(config)

docs/configuration.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Configuration
66
Cache aliases
77
-------------
88

9-
The settings module allows to setup cache configurations and then use them with the specified alias. To set the config, call ``settings.set_config``:
9+
The caches module allows to setup cache configurations and then use them with the specified alias. To set the config, call ``caches.set_config``:
1010

11-
.. automethod:: aiocache.settings.set_config
11+
.. automethod:: aiocache.caches.set_config
1212

13-
To retrieve a copy of the current config, you can use ``settings.get_config``.
13+
To retrieve a copy of the current config, you can use ``caches.get_config``.
1414

1515

1616
Next snippet shows an example usage:

examples/cached_alias_config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import asyncio
22

3-
from aiocache import settings, caches, SimpleMemoryCache, RedisCache
3+
from aiocache import caches, SimpleMemoryCache, RedisCache
44
from aiocache.serializers import DefaultSerializer, PickleSerializer
55

6-
settings.set_config({
6+
caches.set_config({
77
'default': {
88
'cache': "aiocache.SimpleMemoryCache",
99
'serializer': {

tests/ut/conftest.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import asynctest
33

44
from aiocache.base import BaseCache
5-
from aiocache import settings, RedisCache, MemcachedCache
5+
from aiocache import caches, settings, RedisCache, MemcachedCache
66
from aiocache.plugins import BasePlugin
77
from aiocache.serializers import DefaultSerializer
88

@@ -26,6 +26,18 @@ def reset_settings():
2626
})
2727

2828

29+
@pytest.fixture(autouse=True)
30+
def reset_caches():
31+
caches.set_config({
32+
'default': {
33+
'cache': "aiocache.SimpleMemoryCache",
34+
'serializer': {
35+
'class': "aiocache.serializers.DefaultSerializer"
36+
}
37+
}
38+
})
39+
40+
2941
@pytest.fixture(autouse=True)
3042
def disable_logs(mocker):
3143
mocker.patch("aiocache.log.logger")

tests/ut/test_factory.py

+44-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from aiocache import SimpleMemoryCache, RedisCache, settings, caches
3+
from aiocache import SimpleMemoryCache, RedisCache, caches
44
from aiocache.factory import _class_from_string, _create_cache
55
from aiocache.serializers import PickleSerializer
66
from aiocache.plugins import TimingPlugin
@@ -48,7 +48,7 @@ def test_create_not_reuse(self):
4848
assert caches.create('default') is not caches.create('default')
4949

5050
def test_create_extra_args(self):
51-
settings.set_config({
51+
caches.set_config({
5252
'default': {
5353
'cache': "aiocache.RedisCache",
5454
'endpoint': "127.0.0.9",
@@ -62,7 +62,7 @@ def test_create_extra_args(self):
6262
assert cache.db == 10
6363

6464
def test_retrieve_cache(self):
65-
settings.set_config({
65+
caches.set_config({
6666
'default': {
6767
'cache': "aiocache.RedisCache",
6868
'endpoint': "127.0.0.10",
@@ -85,7 +85,7 @@ def test_retrieve_cache(self):
8585
assert len(cache.plugins) == 2
8686

8787
def test_retrieve_cache_new_instance(self):
88-
settings.set_config({
88+
caches.set_config({
8989
'default': {
9090
'cache': "aiocache.RedisCache",
9191
'endpoint': "127.0.0.10",
@@ -108,7 +108,7 @@ def test_retrieve_cache_new_instance(self):
108108
assert len(cache.plugins) == 2
109109

110110
def test_multiple_caches(self):
111-
settings.set_config({
111+
caches.set_config({
112112
'default': {
113113
'cache': "aiocache.RedisCache",
114114
'endpoint': "127.0.0.10",
@@ -136,3 +136,42 @@ def test_multiple_caches(self):
136136
assert len(default.plugins) == 2
137137

138138
assert isinstance(alt, SimpleMemoryCache)
139+
140+
def test_default_caches(self):
141+
assert caches.get_config() == {
142+
'default': {
143+
'cache': "aiocache.SimpleMemoryCache",
144+
'serializer': {
145+
'class': "aiocache.serializers.DefaultSerializer"
146+
}
147+
}
148+
}
149+
150+
def test_get_alias_config(self):
151+
assert caches.get_alias_config("default") == {
152+
'cache': "aiocache.SimpleMemoryCache",
153+
'serializer': {
154+
'class': "aiocache.serializers.DefaultSerializer"
155+
}
156+
}
157+
158+
def test_set_empty_config(self):
159+
with pytest.raises(ValueError):
160+
caches.set_config({})
161+
162+
def test_set_config_no_default(self):
163+
with pytest.raises(ValueError):
164+
caches.set_config({
165+
'no_default': {
166+
'cache': "aiocache.RedisCache",
167+
'endpoint': "127.0.0.10",
168+
'port': 6378,
169+
'serializer': {
170+
'class': "aiocache.serializers.PickleSerializer"
171+
},
172+
'plugins': [
173+
{'class': "aiocache.plugins.HitMissRatioPlugin"},
174+
{'class': "aiocache.plugins.TimingPlugin"}
175+
]
176+
}
177+
})

0 commit comments

Comments
 (0)