Django

Code

Ticket #437 (closed: duplicate)

Opened 3 years ago

Last modified 2 years ago

[patch] In memory file cache

Reported by: nesh <nesh [at] studioquattro [dot] co [dot] yu> Assigned to: jacob
Milestone: Component: Cache system
Version: Keywords:
Cc: nesh@studioquattro.co.yu Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Attached to this ticket is a implementation of in memory based file cache. I got basic idea from vampire TemplateCache? and re-implemented it to make more general.

Usage is simple, if you want to cache a file just call cache = get_cache('<cache name>') and then file_contents = cache.load('filename') . It will detect if the file is modified since last access and reload file.

I'm currently using it for caching translation objects for Ticket #65.

Attachments

filecache.diff (5.7 kB) - added by nesh <nesh [at] studioquattro [dot] co [dot] yu> on 08/30/05 05:29:23.

Change History

08/30/05 05:29:23 changed by nesh <nesh [at] studioquattro [dot] co [dot] yu>

  • attachment filecache.diff added.

08/30/05 06:37:47 changed by nesh <nesh [at] studioquattro [dot] co [dot] yu>

Usefull example:

""" use django templates to process CSS files """

##################################################
## DEPENDENCIES ##

import os, re
from django.conf.settings import MEDIA_ROOT
from django.core import template
from django.utils.httpwrappers import HttpResponse, HttpResponseNotFound
from django.utils.filecache import get_cache, FileCache

##################################################
## GLOBALS AND CONSTANTS ##

##################################################
## CLASSES ##

class _CSSCache(FileCache):
    def do_load(self, path, mode):
        """ return template.Template instead file content """

        fh = file(path, 'rU')
        t = template.Template(fh.read())
        fh.close()
        return t
    # do_load
# _CSSCache

##################################################
## FUNCTIONS ##

def css(request, name=''):
    """ process css request """

    path = os.path.join(MEDIA_ROOT, 'css', name.strip())
    try:
        tpl = get_cache('css', _CSSCache).load(path)
        try:
            from django.conf.settings import CSS_DATA
            ctx = template.Context(CSS_DATA)
        except ImportError:
            ctx = template.Context()

        return HttpResponse(tpl.render(ctx), mimetype='text/css')
    except IOError, err:
        return HttpResponseNotFound()

09/01/05 18:22:48 changed by adrian

  • owner changed from adrian to jacob.
  • component changed from Tools to Cache system.

09/25/05 13:52:53 changed by jacob

  • status changed from new to closed.
  • resolution set to duplicate.

Duplicate of #515


Add/Change #437 ([patch] In memory file cache)




Change Properties
Action