Django

Code

Ticket #11269 (new)

Opened 1 year ago

Last modified 5 months ago

Allow key_prefix to be callable

Reported by: kmike Assigned to: nobody
Milestone: Component: Cache system
Version: SVN Keywords:
Cc: miracle2k, zeth Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

"cache_page" decorator accepts "key_prefix" argument.

If "cache_page" will allow "key_prefix" to be callable it will be easier to fine-tune view caching. For example, it will be easier to have per-user cache or to cache views based on specified GET parameters (there is dedicated #4992 ticket for that).

I've attached patch for that feature.

The sample syntax:

#have 2 static versions of the my_view response for authenticated and anonymous users

def my_key_prefix(request):
    if request.GET:
        return None #magic value to disable caching

    if request.user.is_authenticated():
        return 'logged_in'
    else:
        return 'not_logged_in'
    
@cache_control(must_revalidate=True)
@cache_page(600, my_key_prefix)
def my_view(request):
....... #something is different for authenticated and anonymous users
#cache my_paginated_view based on "page" parameter in query string
 
def page_key_prefix(request):
    return request.GET.get('page','')

@cache_page(60, page_key_prefix)
def my_paginated_view(request)
.... #page number is passed via 'page' GET parameter and used for pagination

Attachments

callable_key_prefix.diff (2.4 kB) - added by kmike on 06/05/09 21:37:12.

Change History

06/05/09 21:37:12 changed by kmike

  • attachment callable_key_prefix.diff added.

08/05/09 20:01:47 changed by kmike

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

Standalone app with cache_page_with prefix decorator: http://bitbucket.org/kmike/django-view-cache-utils/

08/06/09 17:57:53 changed by Alex

  • stage changed from Unreviewed to Accepted.

10/15/09 10:55:23 changed by miracle2k

  • cc set to miracle2k.

01/05/10 16:53:20 changed by zeth

For the second example, that of get parameters, I just generalise the whole thing, i.e. any and all get parameters are used for the key_prefix.

i.e. something like:

def get_requests(request):
    """Turn the GET parameters into a string.
    Used for caching."""
    key_prefix = ""
    # Get the parameters out
    parameters = request.GET.items()
    # Sort them so we only have one order in the cache
    parameters.sort()
    # Turn it into a string
    for pair in parameters:
        for item in pair:
            key_prefix += "%s-" % item
    return key_prefix

@cache_page(600, my_key_prefix)
def my_view(request):
    #blah blah

This pattern seems to work for all the situations I have encountered so far. This seems to be more DRY than the example above.

01/05/10 16:53:52 changed by zeth

  • cc changed from miracle2k to miracle2k, zeth.

02/18/10 10:18:05 changed by Loststylus

zeth, your get_requests doesn't provide support for multiple GET-variables with the same name (for example, checkboxes sent via GET), so cached results for /?a=1 and /?a=1&a=3 would be the same.

02/18/10 10:24:36 changed by Loststylus

The workaround is to use

parameters = dict(request.GET).items()

instead of

parameters = request.GET.items()


Add/Change #11269 (Allow key_prefix to be callable)




Change Properties
Action