Login

Tag "cache"

Snippet List

Git media cache busting tag

This tag appends the current git revision as a GET parameter to a media files so the web server can set an expires header far in the future. Your project must be structured such that MEDIA_ROOT/../.git exists. Usage: `<link rel="stylesheet" type="text/css" href="{% media myapp/css/base.css %}">`

  • cache
  • media
  • git
Read More

Template tag to clear cached template fragment

This is a custom template tag that clears the cache that was created with the **cache** tag. {% load clearcache %} {% clearcache [fragment_name] [var1] [var2] .. %} Create **app/templatetags** folder with **__init__.py** and copy code into **clearcache.py** file. polls/ templatetags/ __init__.py clearcache.py Based on django.templatetags.cache. See Django docs on [custom template tags](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/)

  • template
  • cache
  • clear
  • fragment
Read More

Reset cache between tests

I don't understand why the cache is accumulated between the tests. I thought one of the axioms of unit testing is that the tests should not affect each other. Couldn't find anything that explains why it's done this way but it seems a bit strange. Anybody know if there's a reason or is this a reason for me to upload a patch to Django code?

  • cache
  • tests
  • locmem
  • setup
Read More

Strip Google Analytics cookies for caching middleware purposes

You may notice that using Google Analytics's 'urchin' with the CacheMiddleware and SessionMiddleware or AuthenticationMiddleware middleware classes means that nothing is ever cached. Google Analytics updates its cookies with every page view, and the Auth/Session middlewares add cookies to the caching engine's 'Vary' list. This means every page view is given a unique cache key. This middleware class will remove urchin's '__utmX' cookies from the request object before it hits the caching middleware. Put it at the top of your MIDDLEWARE_CLASSES in settings.py. nf / [email protected]

  • middleware
  • cache
  • google
  • analytics
Read More
Author: nf
  • 7
  • 13

Conditional Caching

This trick is for caching a view only if it passes some condition, for example, if there are more than zero items in a list. The same methodology could be used for conditional applying of other decorators.

  • views
  • cache
  • decorators
Read More

cache_page that does nothing

When debugging/developing you want to be able to refresh your views every time you make a little change. But when in production mode you might want to cache these views because they contain long and resource hungry calculations or something. By putting this above "hack" in after importing `cache_page` you only cache the views in production mode.

  • cache
  • decorator
  • cache_page
Read More

invalidation of cache-template-tag cache

this function invalidates a template-fragment cache bit. say you have: {% load cache %} {% cache 600 user_cache user.id %} something expensive here {% endcache %} maybe you want to force an update. With this function you can, just call: invalidate_template_cache("user_cache", user.id)

  • template
  • cache
  • invalidate
Read More

limit view request rate decorator

Limit rate request decorator for view. Authenificated user can't request decorated view often then timeout. Usage: @limit_request_rate(time_beetween_request_sec) def my_view(request): ... get_cell_value from [here](http://code.activestate.com/recipes/439096/)

  • django
  • cache
  • view
  • decorator
  • limit-request-rate
  • closure
  • cell
Read More

Scalable and invalidateble cache_page decorator

This cache_page decorator can be used in replacement to Django's django.views.decorators.cache.cache_page. It resolves two problems: - invalidation (its cache key is not dependent of header nor request, then you can use model signals (or method 'put' in Google App Engine) to invalidate a URL or a list of them) - easier to scale (can be used at once memcached server by many differente servers) Feel free to show me where it can have problems or limitations. **Updated and improved a lot**

  • cache
  • decorator
  • invalidation
Read More

Language aware cache decorator

Caches a view based on the users language code, a cache_key and optional function arguments. The cache_key can be a string, a callable or None. If it's None, the the name of the decorated function is used. You can pass a tuple `func_args` of arguments. If passed, these arguments are part of the cache key. See examples for details.

  • cache
  • decorator
  • caching
Read More

Database Cache Management View

A simple view used to manage the page cache stored in the database (here Postgresql in the django_cache table, you also have to set correctly CACHE_TABLE_OID, by the OID of the cache table (you can get it in PgAdmin)

  • admin
  • cache
  • view
  • postgresql
  • management
Read More

78 snippets posted so far.