diff --git a/.travis.yml b/.travis.yml index 6021c12..e181efb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,6 @@ branches: - master python: - '2.7' -- '3.4' - '3.5' - '3.6' - pypy @@ -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 diff --git a/CHANGELOG.rst b/CHANGELOG.rst index bb5e4c7..456d42f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,15 @@ +0.1.3 + +- Ability to pass in your own custom cache-key callable function. + Thanks @jaumebecks + `pull#10 `_ + +0.1.2 + +- Ability to specify a different-than-default cache alias + Thanks @benspaulding + `pull#6 `_ + 0.1.1 ~~~~~ diff --git a/README.rst b/README.rst index 3d353e8..9ceb632 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..da2a9b0 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e + +rm -fr dist/* +python setup.py sdist +twine upload dist/* diff --git a/setup.py b/setup.py index 0b29f75..88a5f26 100644 --- a/setup.py +++ b/setup.py @@ -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.' @@ -31,4 +31,7 @@ ], keywords=['django', 'memoize', 'cache', 'decorator'], zip_safe=False, + extras_require={ + 'dev': ['flake8', 'tox', 'twine'], + } ) diff --git a/src/cache_memoize/__init__.py b/src/cache_memoize/__init__.py index 3bc0183..10124c8 100644 --- a/src/cache_memoize/__init__.py +++ b/src/cache_memoize/__init__.py @@ -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, ): @@ -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'. @@ -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: diff --git a/tests/requirements.txt b/tests/requirements.txt index 59ae8a8..cca2f52 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,3 +1,5 @@ +-e . + pytest pytest-coverage python-memcached diff --git a/tests/test_cache_memoize.py b/tests/test_cache_memoize.py index 740af1f..c721ef1 100644 --- a/tests/test_cache_memoize.py +++ b/tests/test_cache_memoize.py @@ -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 = [] diff --git a/tox.ini b/tox.ini index 7f57aa9..ae04b23 100644 --- a/tox.ini +++ b/tox.ini @@ -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 @@ -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 @@ -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