Skip to content

Commit

Permalink
Fix colon mis-caching issue (#33)
Browse files Browse the repository at this point in the history
* Split tests a bit

* Fix mis-caching of arguments containing colons

The usage of quote() is inspired by Django's own `make_template_fragment_key`.

Fixes #32

* Travis: use xenial dist
  • Loading branch information
akx authored and peterbe committed Jun 18, 2019
1 parent 677c8af commit f1c6b84
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
dist: xenial
language: python
matrix:
include:
- python: "3.5"
- python: "3.6"
- python: "3.7"
dist: xenial
- python: pypy3
cache: pip
install:
Expand Down
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
33 changes: 31 additions & 2 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 @@ -39,6 +40,9 @@ def runmeonce(a, b, k="bla"):
runmeonce("A" * 200, "B" * 200, {"C" * 100: "D" * 100})
assert len(calls_made) == 5


def test_prefixes():
calls_made = []
# different prefixes
@cache_memoize(10, prefix="first")
def foo(value):
Expand All @@ -51,9 +55,13 @@ def bar(value):
return "ho"

foo("hey")
assert len(calls_made) == 1
bar("hey")
assert len(calls_made) == 7
assert len(calls_made) == 2


def test_no_store_result():
calls_made = []
# Test when you don't care about the result
@cache_memoize(10, store_result=False, prefix="different")
def returnnothing(a, b, k="bla"):
Expand All @@ -62,7 +70,28 @@ def returnnothing(a, b, k="bla"):

returnnothing(1, 2)
returnnothing(1, 2)
assert len(calls_made) == 8
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():
Expand Down

0 comments on commit f1c6b84

Please sign in to comment.