Skip to content

Commit

Permalink
Remove key parameter (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer authored Jan 14, 2023
1 parent 5007dc2 commit 79c3f70
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
14 changes: 14 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ CHANGES

.. towncrier release notes start
1.0.0 (2023-xx-xx)
==================

Migration instructions
++++++++++++++++++++++

There are a number of backwards-incompatible changes. These points should help with migrating from an older release:

* The ``key`` parameter has been removed from the ``cached`` decorator. The behaviour can be easily reimplemented with ``key_builder=lambda *a, **kw: "foo"``
* When using the ``key_builder`` parameter in ``@multicached``, the function will now return the original, unmodified keys, only using the transformed keys in the cache (this has always been the documented behaviour, but not the implemented behaviour).
* ``BaseSerializer`` is now an ``ABC``, so cannot be instantiated directly.


0.12.0 (2023-01-13)
===================

Expand All @@ -20,6 +33,7 @@ CHANGES
* Fixed ``from aiocache import *``.
* Fixed ``.delete()`` when values are falsy.


0.11.1 (2019-07-31)
===================

Expand Down
10 changes: 0 additions & 10 deletions aiocache/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ class cached:
happens in the background. Enabled by default
:param ttl: int seconds to store the function call. Default is None which means no expiration.
:param key: str value to set as key for the function return. Takes precedence over
key_builder param. If key and key_builder are not passed, it will use module_name
+ function_name + args + kwargs
:param namespace: string to use as default prefix for the key used in all operations of
the backend. Default is None
:param key_builder: Callable that allows to build the function dynamically. It receives
Expand All @@ -59,7 +56,6 @@ class cached:
def __init__(
self,
ttl=SENTINEL,
key=None,
namespace=None,
key_builder=None,
cache=Cache.MEMORY,
Expand All @@ -70,7 +66,6 @@ def __init__(
**kwargs,
):
self.ttl = ttl
self.key = key
self.key_builder = key_builder
self.noself = noself
self.alias = alias
Expand Down Expand Up @@ -126,8 +121,6 @@ async def decorator(
return result

def get_cache_key(self, f, args, kwargs):
if self.key:
return self.key
if self.key_builder:
return self.key_builder(f, *args, **kwargs)

Expand Down Expand Up @@ -172,9 +165,6 @@ class cached_stampede(cached):
If 0 or None, no locking happens (default is 2). redis and memory backends support
float ttls
:param ttl: int seconds to store the function call. Default is None which means no expiration.
:param key: str value to set as key for the function return. Takes precedence over
key_from_attr param. If key and key_from_attr are not passed, it will use module_name
+ function_name + args + kwargs
:param key_from_attr: str arg or kwarg name from the function to use as a key.
:param namespace: string to use as default prefix for the key used in all operations of
the backend. Default is None
Expand Down
4 changes: 2 additions & 2 deletions examples/cached_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@


@cached(
ttl=10, cache=Cache.REDIS, key="key", serializer=PickleSerializer(),
port=6379, namespace="main")
ttl=10, cache=Cache.REDIS, key_builder=lambda *args, **kw: "key",
serializer=PickleSerializer(), port=6379, namespace="main")
async def cached_call():
return Result("content", 200)

Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def default_cache(self, mocker, cache):
mocker.patch("aiocache.decorators._get_cache", autospec=True, return_value=cache)

async def test_cached_ttl(self, cache):
@cached(ttl=2, key=Keys.KEY)
@cached(ttl=2, key_builder=lambda *args, **kw: Keys.KEY)
async def fn():
return str(random.randint(1, 50))

Expand Down
15 changes: 5 additions & 10 deletions tests/ut/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def spy_stub(self, mocker):
def test_init(self):
c = cached(
ttl=1,
key="key",
key_builder="fn",
key_builder=lambda *args, **kw: "key",
cache=SimpleMemoryCache,
plugins=None,
alias=None,
Expand All @@ -50,8 +49,7 @@ def test_init(self):
)

assert c.ttl == 1
assert c.key == "key"
assert c.key_builder == "fn"
assert c.key_builder() == "key"
assert c.cache is None
assert c._cache == SimpleMemoryCache
assert c._serializer is None
Expand All @@ -76,8 +74,7 @@ def test_alias_takes_precedence(self, mock_cache):
assert c.cache is mock_cache

def test_get_cache_key_with_key(self, decorator):
decorator.key = "key"
decorator.key_builder = "fn"
decorator.key_builder = lambda *args, **kw: "key"
assert decorator.get_cache_key(stub, (1, 2), {"a": 1, "b": 2}) == "key"

def test_get_cache_key_without_key_and_attr(self, decorator):
Expand Down Expand Up @@ -258,8 +255,7 @@ def test_init(self):
c = cached_stampede(
lease=3,
ttl=1,
key="key",
key_builder="fn",
key_builder=lambda *args, **kw: "key",
cache=SimpleMemoryCache,
plugins=None,
alias=None,
Expand All @@ -269,8 +265,7 @@ def test_init(self):
)

assert c.ttl == 1
assert c.key == "key"
assert c.key_builder == "fn"
assert c.key_builder() == "key"
assert c.cache is None
assert c._cache == SimpleMemoryCache
assert c._serializer is None
Expand Down

0 comments on commit 79c3f70

Please sign in to comment.