Skip to content

create factory accepts cache args #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aiocache/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def get(self, alias):
self._caches[alias] = cache
return cache

def create(self, alias):
def create(self, alias, **kwargs):
config = self._get_alias_config(alias)
cache = _create_cache(**deepcopy(config))
cache = _create_cache(**{**config, **kwargs})
return cache


Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ Next snippet shows an example usage:
:linenos:
:emphasize-lines: 6-26

When you do ``caches.get('alias_name')``, the cache instance is built lazily. Next accesses will return the **same** instance. If instead of reusing the same instance, you need a new one every time, use ``caches.create('alias_name')``.
When you do ``caches.get('alias_name')``, the cache instance is built lazily. Next accesses will return the **same** instance. If instead of reusing the same instance, you need a new one every time, use ``caches.create('alias_name')``. One of the advantages of ``caches.create`` is that it accepts extra args that then are passed to the cache constructor. This way you can override args like namespace, endpoint, etc.
14 changes: 14 additions & 0 deletions tests/ut/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ def test_reuse_instance(self):
def test_create_not_reuse(self):
assert caches.create('default') is not caches.create('default')

def test_create_extra_args(self):
settings.set_config({
'default': {
'cache': "aiocache.RedisCache",
'endpoint': "127.0.0.9",
'db': 10,
'port': 6378
}
})
cache = caches.create('default', namespace="whatever", endpoint="127.0.0.10", db=10)
assert cache.namespace == "whatever"
assert cache.endpoint == "127.0.0.10"
assert cache.db == 10

def test_retrieve_cache(self):
settings.set_config({
'default': {
Expand Down