Skip to content

Commit 46a00ea

Browse files
committed
Revert "Revert "Revert "Deprecate settings module"""
This reverts commit 30c9818.
1 parent 3ff6586 commit 46a00ea

File tree

7 files changed

+80
-135
lines changed

7 files changed

+80
-135
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
23
from .backends.memory import SimpleMemoryCache
34
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

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

3+
from aiocache.settings import settings
4+
35

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

3234
class CacheHandler:
3335

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

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+
4649
def get(self, alias):
4750
try:
4851
return self._caches[alias]
4952
except KeyError:
5053
pass
5154

52-
config = self.get_alias_config(alias)
55+
config = self._get_alias_config(alias)
5356
cache = _create_cache(**deepcopy(config))
5457
self._caches[alias] = cache
5558
return cache
5659

5760
def create(self, alias, **kwargs):
58-
config = self.get_alias_config(alias)
61+
config = self._get_alias_config(alias)
5962
cache = _create_cache(**{**config, **kwargs})
6063
return cache
6164

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-
12565

12666
caches = CacheHandler()

aiocache/settings.py

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

43

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

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

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

3232
@classmethod
3333
def set_config(cls, config):
34-
logger.warning("Deprecated, please use 'aiocache.caches.get_config'")
35-
return caches.set_config(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

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 caches module allows to setup cache configurations and then use them with the specified alias. To set the config, call ``caches.set_config``:
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``:
1010

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

13-
To retrieve a copy of the current config, you can use ``caches.get_config``.
13+
To retrieve a copy of the current config, you can use ``settings.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 caches, SimpleMemoryCache, RedisCache
3+
from aiocache import settings, caches, SimpleMemoryCache, RedisCache
44
from aiocache.serializers import DefaultSerializer, PickleSerializer
55

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

tests/ut/conftest.py

+3-3
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 caches, RedisCache, MemcachedCache
5+
from aiocache import settings, RedisCache, MemcachedCache
66
from aiocache.plugins import BasePlugin
77
from aiocache.serializers import DefaultSerializer
88

@@ -15,8 +15,8 @@ def pytest_namespace():
1515

1616

1717
@pytest.fixture(autouse=True)
18-
def reset_caches():
19-
caches.set_config({
18+
def reset_settings():
19+
settings.set_config({
2020
'default': {
2121
'cache': "aiocache.SimpleMemoryCache",
2222
'serializer': {

tests/ut/test_factory.py

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

3-
from aiocache import SimpleMemoryCache, RedisCache, caches
3+
from aiocache import SimpleMemoryCache, RedisCache, settings, 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-
caches.set_config({
51+
settings.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-
caches.set_config({
65+
settings.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-
caches.set_config({
88+
settings.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-
caches.set_config({
111+
settings.set_config({
112112
'default': {
113113
'cache': "aiocache.RedisCache",
114114
'endpoint': "127.0.0.10",
@@ -136,42 +136,3 @@ 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)