django-fancy-cache


Namedjango-fancy-cache JSON
Version 1.3.1 PyPI version JSON
download
home_pagehttps://github.com/peterbe/django-fancy-cache
SummaryA Django 'cache_page' decorator on steroids
upload_time2023-09-26 22:08:49
maintainer
docs_urlNone
authorPeter Bengtsson
requires_python
licenseBSD
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            django-fancy-cache
==================

Copyright Peter Bengtsson, mail@peterbe.com, 2013-2022

License: BSD

About django-fancy-cache
------------------------

A Django ``cache_page`` decorator on steroids.

Unlike the stock ``django.views.decorators.cache.change_page`` this
decorator makes it possible to set a ``key_prefix`` that is a
callable. This callable is passed the request and if it returns ``None``
the page is not cached.

Also, you can set another callable called ``post_process_response``
(which is passed the response and the request) which can do some
additional changes to the response before it's set in cache.

Lastly, you can set ``post_process_response_always=True`` so that the
``post_process_response`` callable is always called, even when the
response is coming from the cache.


How to use it
-------------

In your Django views:

.. code:: python

    from fancy_cache import cache_page
    from django.utils.decorators import method_decorator
    from django.views.generic import TemplateView

    @cache_page(60 * 60)
    def myview(request):
        return render(request, 'page1.html')

    def prefixer(request):
        if request.method != 'GET':
            return None
        if request.GET.get('no-cache'):
            return None
        return 'myprefix'

    @cache_page(60 * 60, key_prefix=prefixer)
    def myotherview(request):
        return render(request, 'page2.html')

    def post_processor(response, request):
        response.content += '<!-- this was post processed -->'
        return response

    @cache_page(
        60 * 60,
        key_prefix=prefixer,
        post_process_response=post_processor)
    def yetanotherotherview(request):
        return render(request, 'page3.html')


    class MyClassBasedView(TemplateView):
        template_name = 'page4.html'

        @method_decorator(cache_page(60*60))
        def get(self, request, *args, **kwargs):
            return super().get(request, *args, **kwargs)

Optional uses
-------------

If you want to you can have ``django-fancy-cache`` record every URL it
caches. This can be useful for things like invalidation or curious
statistical inspection.

You can either switch this on on the decorator itself. Like this:

.. code:: python

    from fancy_cache import cache_page

    @cache_page(60 * 60, remember_all_urls=True)
    def myview(request):
        return render(request, 'page1.html')

Or, more conveniently to apply it to all uses of the ``cache_page``
decorator you can set the default in your settings with:

.. code:: python

    FANCY_REMEMBER_ALL_URLS = True

Now, suppose you have the this option enabled. Now you can do things
like this:

.. code:: python

    >>> from fancy_cache.memory import find_urls
    >>> list(find_urls(['/some/searchpath', '/or/like/*/this.*']))
    >>> # or, to get all:
    >>> list(find_urls([]))

There is also another option to this and that is to purge (aka.
invalidate) the remembered URLs. You simply all the ``purge=True``
option like this:

.. code:: python

    >>> from fancy_cache.memory import find_urls
    >>> list(find_urls([], purge=True))

Note: Since ``find_urls()`` returns a generator, the purging won't
happen unless you exhaust the generator. E.g. looping over it or
turning it into a list.

**If you are using Memcached**, you must enable check-and-set to
remember all urls by enabling the ``FANCY_USE_MEMCACHED_CHECK_AND_SET``
flag and enabling ``cas`` in your ``CACHES`` settings:

.. code:: python

    # in settings.py

    FANCY_USE_MEMCACHED_CHECK_AND_SET = True

    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
            'LOCATION': '127.0.0.1:11211',
            # This OPTIONS setting enables Memcached check-and-set which is
            # required for remember_all_urls or FANCY_REMEMBER_ALL_URLS.
            'OPTIONS': {
                'behaviors': {
                    'cas': True
                }
            }
        }
     }

The second way to inspect all recorded URLs is to use the
``fancy-cache`` management command. This is only available if you have
added ``fancy_cache`` to your ``INSTALLED_APPS`` setting. Now you can do
this::

    $ ./manage.py fancy-cache --help
    $ ./manage.py fancy-cache
    $ ./manage.py fancy-cache /some/searchpath /or/like/*/this.*
    $ ./manage.py fancy-cache /some/place/* --purge
    $ # or to purge them all!
    $ ./manage.py fancy-cache --purge

Note, it will only print out URLs that if found (and purged, if
applicable).

The third way to inspect the recorded URLs is to add this to your root
``urls.py``:

.. code:: python

    url(r'fancy-cache', include('fancy_cache.urls')),

Now, if you visit ``http://localhost:8000/fancy-cache`` you get a table
listing every URL that ``django-fancy-cache`` has recorded.


Optional uses (for the exceptionally curious)
---------------------------------------------

If you have enabled ``FANCY_REMEMBER_ALL_URLS`` you can also enable
``FANCY_REMEMBER_STATS_ALL_URLS`` in your settings. What this does is
that it attempts to count the number of cache hits and cache misses
you have for each URL.

This counting of hits and misses is configured to last "a long time".
Possibly longer than you cache your view. So, over time you can expect
to have more than one miss because your view cache expires and it
starts over.

You can see the stats whenever you use any of the ways described in
the section above. For example like this:

.. code:: python

    >>> from fancy_cache.memory import find_urls
    >>> found = list(find_urls([]))[0]
    >>> found[0]
    '/some/page.html'
    >>> found[2]
    {'hits': 1235, 'misses': 12}

There is obviously a small additional performance cost of using the
``FANCY_REMEMBER_ALL_URLS`` and/or ``FANCY_REMEMBER_STATS_ALL_URLS`` in
your project so only use it if you don't have any smarter way to
invalidate, for debugging or if you really want make it possible to
purge all cached responses when you run an upgrade of your site or
something.

Running the test suite
----------------------

The simplest way is to simply run::

    $ pip install tox
    $ tox

Or to run it without ``tox`` you can simply run::

    $ export PYTHONPATH=`pwd`
    $ export DJANGO_SETTINGS_MODULE=fancy_tests.tests.settings
    $ django-admin.py test


Changelog
---------

1.3.1
    * Fix a bug whereby ``FANCY_COMPRESS_REMEMBERED_URLS`` setting
      raises a TypeError upon first implementation.

1.3.0
    * Enable ``FANCY_COMPRESS_REMEMBERED_URLS`` setting to compress
      ``remembered_urls`` dictionary when ``FANCY_REMEMBER_ALL_URLS``
      is True.
    * Bugfix: use correct location for ``REMEMBERED_URLS`` 
      when using Memcached.
    * Add support for Python 3.11, Django 4.1 & 4.2
    * Drop support for Python < 3.8, Django < 3.2, Django 4.0

1.2.1
    * Bugfix: conflict between the DummyCache backend when 
      ``FANCY_USE_MEMCACHED_CHECK_AND_SET`` is ``True``

1.2.0
    * Restructure the remembered_urls cache dict to clean up stale entries
    * Update FancyCacheMiddleware to match latest Django CacheMiddlware
      (Also renames to FancyCacheMiddleware)
    * Apply Memcached check-and-set to the delete_keys function
      if ``settings.FANCY_USE_MEMCACHED_CHECK_AND_SET = True``
    * Drop support for Python <3.6
    * Add support for Python 3.10 and Django 4.0

1.1.0
    * If you use Memcached you can set
      ``settings.FANCY_USE_MEMCACHED_CHECK_AND_SET = True`` so that you
      can use ``cache._cache.cas`` which only workd with Memcached

1.0.0
    * Drop support for Python <3.5 and Django <2.2.0

0.11.0
    * Fix for ``parse_qs`` correctly between Python 2 and Python 3

0.10.0
    * Fix for keeping blank strings in query strings. #39

0.9.0
    * Django 1.10 support

0.8.2
    * Remove deprecated way to define URL patterns and tests in python 3.5

0.8.1
    * Ability to specify different cache backends to be used
      https://github.com/peterbe/django-fancy-cache/pull/31

0.8.0
    * Started keeping a Changelog



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/peterbe/django-fancy-cache",
    "name": "django-fancy-cache",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Peter Bengtsson",
    "author_email": "mail@peterbe.com",
    "download_url": "https://files.pythonhosted.org/packages/18/19/034edb9c21ac720e932938f38e893cd44ee477ddfd1b57b84555ca8baf43/django-fancy-cache-1.3.1.tar.gz",
    "platform": null,
    "description": "django-fancy-cache\n==================\n\nCopyright Peter Bengtsson, mail@peterbe.com, 2013-2022\n\nLicense: BSD\n\nAbout django-fancy-cache\n------------------------\n\nA Django ``cache_page`` decorator on steroids.\n\nUnlike the stock ``django.views.decorators.cache.change_page`` this\ndecorator makes it possible to set a ``key_prefix`` that is a\ncallable. This callable is passed the request and if it returns ``None``\nthe page is not cached.\n\nAlso, you can set another callable called ``post_process_response``\n(which is passed the response and the request) which can do some\nadditional changes to the response before it's set in cache.\n\nLastly, you can set ``post_process_response_always=True`` so that the\n``post_process_response`` callable is always called, even when the\nresponse is coming from the cache.\n\n\nHow to use it\n-------------\n\nIn your Django views:\n\n.. code:: python\n\n    from fancy_cache import cache_page\n    from django.utils.decorators import method_decorator\n    from django.views.generic import TemplateView\n\n    @cache_page(60 * 60)\n    def myview(request):\n        return render(request, 'page1.html')\n\n    def prefixer(request):\n        if request.method != 'GET':\n            return None\n        if request.GET.get('no-cache'):\n            return None\n        return 'myprefix'\n\n    @cache_page(60 * 60, key_prefix=prefixer)\n    def myotherview(request):\n        return render(request, 'page2.html')\n\n    def post_processor(response, request):\n        response.content += '<!-- this was post processed -->'\n        return response\n\n    @cache_page(\n        60 * 60,\n        key_prefix=prefixer,\n        post_process_response=post_processor)\n    def yetanotherotherview(request):\n        return render(request, 'page3.html')\n\n\n    class MyClassBasedView(TemplateView):\n        template_name = 'page4.html'\n\n        @method_decorator(cache_page(60*60))\n        def get(self, request, *args, **kwargs):\n            return super().get(request, *args, **kwargs)\n\nOptional uses\n-------------\n\nIf you want to you can have ``django-fancy-cache`` record every URL it\ncaches. This can be useful for things like invalidation or curious\nstatistical inspection.\n\nYou can either switch this on on the decorator itself. Like this:\n\n.. code:: python\n\n    from fancy_cache import cache_page\n\n    @cache_page(60 * 60, remember_all_urls=True)\n    def myview(request):\n        return render(request, 'page1.html')\n\nOr, more conveniently to apply it to all uses of the ``cache_page``\ndecorator you can set the default in your settings with:\n\n.. code:: python\n\n    FANCY_REMEMBER_ALL_URLS = True\n\nNow, suppose you have the this option enabled. Now you can do things\nlike this:\n\n.. code:: python\n\n    >>> from fancy_cache.memory import find_urls\n    >>> list(find_urls(['/some/searchpath', '/or/like/*/this.*']))\n    >>> # or, to get all:\n    >>> list(find_urls([]))\n\nThere is also another option to this and that is to purge (aka.\ninvalidate) the remembered URLs. You simply all the ``purge=True``\noption like this:\n\n.. code:: python\n\n    >>> from fancy_cache.memory import find_urls\n    >>> list(find_urls([], purge=True))\n\nNote: Since ``find_urls()`` returns a generator, the purging won't\nhappen unless you exhaust the generator. E.g. looping over it or\nturning it into a list.\n\n**If you are using Memcached**, you must enable check-and-set to\nremember all urls by enabling the ``FANCY_USE_MEMCACHED_CHECK_AND_SET``\nflag and enabling ``cas`` in your ``CACHES`` settings:\n\n.. code:: python\n\n    # in settings.py\n\n    FANCY_USE_MEMCACHED_CHECK_AND_SET = True\n\n    CACHES = {\n        'default': {\n            'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',\n            'LOCATION': '127.0.0.1:11211',\n            # This OPTIONS setting enables Memcached check-and-set which is\n            # required for remember_all_urls or FANCY_REMEMBER_ALL_URLS.\n            'OPTIONS': {\n                'behaviors': {\n                    'cas': True\n                }\n            }\n        }\n     }\n\nThe second way to inspect all recorded URLs is to use the\n``fancy-cache`` management command. This is only available if you have\nadded ``fancy_cache`` to your ``INSTALLED_APPS`` setting. Now you can do\nthis::\n\n    $ ./manage.py fancy-cache --help\n    $ ./manage.py fancy-cache\n    $ ./manage.py fancy-cache /some/searchpath /or/like/*/this.*\n    $ ./manage.py fancy-cache /some/place/* --purge\n    $ # or to purge them all!\n    $ ./manage.py fancy-cache --purge\n\nNote, it will only print out URLs that if found (and purged, if\napplicable).\n\nThe third way to inspect the recorded URLs is to add this to your root\n``urls.py``:\n\n.. code:: python\n\n    url(r'fancy-cache', include('fancy_cache.urls')),\n\nNow, if you visit ``http://localhost:8000/fancy-cache`` you get a table\nlisting every URL that ``django-fancy-cache`` has recorded.\n\n\nOptional uses (for the exceptionally curious)\n---------------------------------------------\n\nIf you have enabled ``FANCY_REMEMBER_ALL_URLS`` you can also enable\n``FANCY_REMEMBER_STATS_ALL_URLS`` in your settings. What this does is\nthat it attempts to count the number of cache hits and cache misses\nyou have for each URL.\n\nThis counting of hits and misses is configured to last \"a long time\".\nPossibly longer than you cache your view. So, over time you can expect\nto have more than one miss because your view cache expires and it\nstarts over.\n\nYou can see the stats whenever you use any of the ways described in\nthe section above. For example like this:\n\n.. code:: python\n\n    >>> from fancy_cache.memory import find_urls\n    >>> found = list(find_urls([]))[0]\n    >>> found[0]\n    '/some/page.html'\n    >>> found[2]\n    {'hits': 1235, 'misses': 12}\n\nThere is obviously a small additional performance cost of using the\n``FANCY_REMEMBER_ALL_URLS`` and/or ``FANCY_REMEMBER_STATS_ALL_URLS`` in\nyour project so only use it if you don't have any smarter way to\ninvalidate, for debugging or if you really want make it possible to\npurge all cached responses when you run an upgrade of your site or\nsomething.\n\nRunning the test suite\n----------------------\n\nThe simplest way is to simply run::\n\n    $ pip install tox\n    $ tox\n\nOr to run it without ``tox`` you can simply run::\n\n    $ export PYTHONPATH=`pwd`\n    $ export DJANGO_SETTINGS_MODULE=fancy_tests.tests.settings\n    $ django-admin.py test\n\n\nChangelog\n---------\n\n1.3.1\n    * Fix a bug whereby ``FANCY_COMPRESS_REMEMBERED_URLS`` setting\n      raises a TypeError upon first implementation.\n\n1.3.0\n    * Enable ``FANCY_COMPRESS_REMEMBERED_URLS`` setting to compress\n      ``remembered_urls`` dictionary when ``FANCY_REMEMBER_ALL_URLS``\n      is True.\n    * Bugfix: use correct location for ``REMEMBERED_URLS`` \n      when using Memcached.\n    * Add support for Python 3.11, Django 4.1 & 4.2\n    * Drop support for Python < 3.8, Django < 3.2, Django 4.0\n\n1.2.1\n    * Bugfix: conflict between the DummyCache backend when \n      ``FANCY_USE_MEMCACHED_CHECK_AND_SET`` is ``True``\n\n1.2.0\n    * Restructure the remembered_urls cache dict to clean up stale entries\n    * Update FancyCacheMiddleware to match latest Django CacheMiddlware\n      (Also renames to FancyCacheMiddleware)\n    * Apply Memcached check-and-set to the delete_keys function\n      if ``settings.FANCY_USE_MEMCACHED_CHECK_AND_SET = True``\n    * Drop support for Python <3.6\n    * Add support for Python 3.10 and Django 4.0\n\n1.1.0\n    * If you use Memcached you can set\n      ``settings.FANCY_USE_MEMCACHED_CHECK_AND_SET = True`` so that you\n      can use ``cache._cache.cas`` which only workd with Memcached\n\n1.0.0\n    * Drop support for Python <3.5 and Django <2.2.0\n\n0.11.0\n    * Fix for ``parse_qs`` correctly between Python 2 and Python 3\n\n0.10.0\n    * Fix for keeping blank strings in query strings. #39\n\n0.9.0\n    * Django 1.10 support\n\n0.8.2\n    * Remove deprecated way to define URL patterns and tests in python 3.5\n\n0.8.1\n    * Ability to specify different cache backends to be used\n      https://github.com/peterbe/django-fancy-cache/pull/31\n\n0.8.0\n    * Started keeping a Changelog\n\n\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "A Django 'cache_page' decorator on steroids",
    "version": "1.3.1",
    "project_urls": {
        "Homepage": "https://github.com/peterbe/django-fancy-cache"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e446cc8684096582dc27e40e18ceee9dedd073273e16f1bfba0b5cb62f1af192",
                "md5": "b825c1b92c2e8fa7bc0206aaf5a5e1e9",
                "sha256": "fdee48ce0d43c08d5605d3d2f7a599a31b332b165cd97058bcabdb2e99b9c296"
            },
            "downloads": -1,
            "filename": "django_fancy_cache-1.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b825c1b92c2e8fa7bc0206aaf5a5e1e9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 22867,
            "upload_time": "2023-09-26T22:08:47",
            "upload_time_iso_8601": "2023-09-26T22:08:47.574938Z",
            "url": "https://files.pythonhosted.org/packages/e4/46/cc8684096582dc27e40e18ceee9dedd073273e16f1bfba0b5cb62f1af192/django_fancy_cache-1.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1819034edb9c21ac720e932938f38e893cd44ee477ddfd1b57b84555ca8baf43",
                "md5": "e9c4f9d7158dd935bf56fe0fc7c4892a",
                "sha256": "89f9c98281201e1e71b49135e6ddc2336a6210be0e2bef1371cbdcf022bbb829"
            },
            "downloads": -1,
            "filename": "django-fancy-cache-1.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "e9c4f9d7158dd935bf56fe0fc7c4892a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 18568,
            "upload_time": "2023-09-26T22:08:49",
            "upload_time_iso_8601": "2023-09-26T22:08:49.295213Z",
            "url": "https://files.pythonhosted.org/packages/18/19/034edb9c21ac720e932938f38e893cd44ee477ddfd1b57b84555ca8baf43/django-fancy-cache-1.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-26 22:08:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "peterbe",
    "github_project": "django-fancy-cache",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "django-fancy-cache"
}
        
Elapsed time: 0.26762s