Skip to content

Commit

Permalink
None result obtained from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
poleha committed Nov 5, 2018
2 parents fda8f8b + 062c710 commit da4fc0f
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 27 deletions.
9 changes: 0 additions & 9 deletions .travis.yml
Expand Up @@ -5,7 +5,6 @@ branches:
- master
python:
- '2.7'
- '3.4'
- '3.5'
- '3.6'
- pypy
Expand All @@ -15,11 +14,3 @@ install:
script: tox -v
after_success:
- bash <(curl -s https://codecov.io/bash)
deploy:
provider: pypi
user: peterbe
password:
secure: I3Z1kTaNQGB6yXxZfzFtsXvkJy/2fFVrCVWYLiMQanHlwaEGeQn9xH56ULXz7RNUxLAILjqWjdcEjiApbGi5jXJokb2Asa/F4KoVNcciqYZKcBO3OTPSjOa7plBK/v3QCyGs8yaBylsYqucy2fm99t6XKmgflXRC4GCUhe8OoQ3xLAjWailMAEhAe9k+al5ccTHZhEh/hD/nTxO36n9GchVJEzebWu8Uj0FPv+Y6getXryvVEE7AQ/u+ePX8x5YclyhWDsWTEHQ3CRjIx67+fRy/fO3FoEThXzDMdts2qNUj+8bZkHAyHzzslaKWxeq+9LHKuusBgc364SC4ME0SdTtnBFdu0AiyvCcxBKwma5pUAHvn9Ys4+TqWkiHCDNZlU1J/EepxxuMCaR4HvbblYVcYQLG4AUMaRUNhNh/da7r7dUj2umyhBRxnkKEmOHzpXwWV6ZYTNJYgFLjoNHKPEw1VRGnQuVkZmRFM0vrSniLk9dxDuvMSJBi6sObR5C/DvL10tbQJtfachG8MRZXI6fFZEILH9WRliLWedZuSRhr3GVeKE9xkFkg40MhBW/sTyMbYXn/54/XsjFFKTlRHGlpL4rVzoZqbtWsjnto8SGNorlLrg3UioGCtrM4B+Xmwmf+/wDFYV5LaCRBDKwM8RNtSgOb3a82H9Qt7PCOfjIU=
on:
repo: peterbe/django-cache-memoize
distributions: sdist bdist_wheel
12 changes: 12 additions & 0 deletions CHANGELOG.rst
@@ -1,3 +1,15 @@
0.1.3

- Ability to pass in your own custom cache-key callable function.
Thanks @jaumebecks
`pull#10 <https://github.com/peterbe/django-cache-memoize/pull/10>`_

0.1.2

- Ability to specify a different-than-default cache alias
Thanks @benspaulding
`pull#6 <https://github.com/peterbe/django-cache-memoize/pull/6>`_

0.1.1
~~~~~

Expand Down
11 changes: 11 additions & 0 deletions README.rst
Expand Up @@ -376,3 +376,14 @@ However, this works...
.. _`django-memoize`: http://pythonhosted.org/django-memoize/
.. _`Thomas Vavrys`: https://github.com/tvavrys


Development
===========

The most basic thing is to clone the repo and run:

.. code-block:: shell
pip install -e ".[dev]"
tox
6 changes: 6 additions & 0 deletions deploy.sh
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

rm -fr dist/*
python setup.py sdist
twine upload dist/*
5 changes: 4 additions & 1 deletion setup.py
Expand Up @@ -6,7 +6,7 @@

setup(
name='django-cache-memoize',
version='0.1.1',
version='0.1.3',
description=(
'Django utility for a memoization decorator that uses the Django '
'cache framework.'
Expand All @@ -31,4 +31,7 @@
],
keywords=['django', 'memoize', 'cache', 'decorator'],
zip_safe=False,
extras_require={
'dev': ['flake8', 'tox', 'twine'],
}
)
7 changes: 6 additions & 1 deletion src/cache_memoize/__init__.py
Expand Up @@ -13,6 +13,7 @@ def cache_memoize(
args_rewrite=None,
hit_callable=None,
miss_callable=None,
key_generator_callable=None,
store_result=True,
cache_alias=DEFAULT_CACHE_ALIAS,
):
Expand All @@ -26,6 +27,7 @@ def cache_memoize(
re-represent them for the sake of the cache key.
:arg function hit_callable: Gets executed if key was in cache.
:arg function miss_callable: Gets executed if key was *not* in cache.
:arg key_generator_callable: Custom cache key name generator.
:arg bool store_result: If you know the result is not important, just
that the cache blocked it from running repeatedly, set this to False.
:arg string cache_alias: The cache alias to use; defaults to 'default'.
Expand Down Expand Up @@ -99,7 +101,10 @@ def _make_cache_key(*args, **kwargs):
@wraps(func)
def inner(*args, **kwargs):
refresh = kwargs.pop('_refresh', False)
cache_key = _make_cache_key(*args, **kwargs)
if key_generator_callable is None:
cache_key = _make_cache_key(*args, **kwargs)
else:
cache_key = key_generator_callable(*args, **kwargs)
if refresh:
result = None
else:
Expand Down
2 changes: 2 additions & 0 deletions tests/requirements.txt
@@ -1,3 +1,5 @@
-e .

pytest
pytest-coverage
python-memcached
Expand Down
20 changes: 20 additions & 0 deletions tests/test_cache_memoize.py
Expand Up @@ -204,6 +204,26 @@ def runmeonce(a):
assert len(calls_made) == 2


def test_cache_memoize_works_with_custom_key_generator():

calls_made = []

def key_generator(*args):
key = (':{}' * len(args)).format(*args)
return 'custom_namespace:{}'.format(key)

@cache_memoize(10, key_generator_callable=key_generator)
def runmeonce(arg1, arg2):
calls_made.append((arg1, arg2))
return arg1 + 1

runmeonce(1, 2)
runmeonce(1, 2)
assert len(calls_made) == 1
runmeonce(1, 3)
assert len(calls_made) == 2


def test_cache_memoize_empty_placeholder():
calls_made = []

Expand Down
32 changes: 16 additions & 16 deletions tox.ini
Expand Up @@ -3,16 +3,17 @@ skipsdist = True
usedevelop = True
minversion = 1.8
envlist =
flake8-py27,
flake8-py35,
flake8-py36,
readme-py27,
docs-py27-dj{18,19,110},
tests-py{27,34,35,36,py}-dj{18,19,110,111}
docs-py27-dj{18,110},
py27-django{18,19,110,111},
py35-django{18,19,110,111,20,21},
py36-django{18,19,110,111,20,21},
skip_missing_interpreters = True

[testenv]
basepython =
py27: python2.7
py34: python3.4
py35: python3.5
py36: python3.6
pypy: pypy
Expand All @@ -22,10 +23,12 @@ setenv =
PYTHONPATH = {toxinidir}
deps =
-rtests/requirements.txt
dj18: Django<1.9
dj19: Django<1.10
dj110: Django<1.11
dj111: Django<1.12
django18: Django >=1.8, <1.9
django19: Django >=1.9, <1.10
django110: Django >=1.10, <1.11
django111: Django >=1.11, <2.0
django20: Django>=2.0,<2.1
django21: Django>=2.1
docs: sphinx
commands =
python --version
Expand All @@ -36,13 +39,10 @@ commands =
commands = python setup.py check -r -s
deps = readme_renderer

[testenv:flake8-py27]
commands = flake8 src/cache_memoize.py
deps = flake8

[testenv:flake8-py35]
commands = flake8 src/cache_memoize.py
deps = flake8
[testenv:flake8-py36]
commands = flake8 src tests
deps =
flake8

[flake8]
exclude=.tox
Expand Down

0 comments on commit da4fc0f

Please sign in to comment.