Skip to content

Commit

Permalink
Fix mis-caching of arguments containing colons
Browse files Browse the repository at this point in the history
The usage of quote() is inspired by Django's own `make_template_fragment_key`.

Fixes #32
  • Loading branch information
akx committed Jun 12, 2019
1 parent cd47c4c commit 3170917
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/cache_memoize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from functools import wraps

import hashlib
from urllib.parse import quote

from django.core.cache import caches, DEFAULT_CACHE_ALIAS

from django.utils.encoding import force_text, force_bytes
from django.utils.encoding import force_bytes

MARKER = object()

Expand Down Expand Up @@ -91,8 +93,8 @@ def noop(*args):
def decorator(func):
def _default_make_cache_key(*args, **kwargs):
cache_key = ":".join(
[force_text(x) for x in args_rewrite(*args)]
+ [force_text("{}={}".format(k, v)) for k, v in kwargs.items()]
[quote(str(x)) for x in args_rewrite(*args)]
+ [quote("{}={}".format(k, v)) for k, v in kwargs.items()]
)
return hashlib.md5(
force_bytes("cache_memoize" + (prefix or func.__name__) + cache_key)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_cache_memoize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random
from threading import Thread, Lock

import pytest
from django.core.cache import cache

from cache_memoize import cache_memoize
Expand Down Expand Up @@ -72,6 +73,27 @@ def returnnothing(a, b, k="bla"):
assert len(calls_made) == 1


@pytest.mark.parametrize(
"bits", [("a", "b", "c"), ("ä", "á", "ö"), ("ë".encode(), b"\02", b"i")]
)
def test_colons(bits):
calls_made = []

@cache_memoize(10)
def fun(a, b, k="bla"):
calls_made.append((a, b, k))
return (a, b, k)

sep = ":"
if isinstance(bits[0], bytes):
sep = sep.encode()
a1, a2 = (sep.join(bits[:2]), bits[2])
b1, b2 = (bits[0], sep.join(bits[1:]))
fun(a1, a2)
fun(b1, b2)
assert len(calls_made) == 2


def test_cache_memoize_hit_miss_callables():

hits = []
Expand Down

0 comments on commit 3170917

Please sign in to comment.