Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#437 closed enhancement (duplicate)

[patch] In memory file cache

Reported by: nesh <nesh [at] studioquattro [dot] co [dot] yu> Owned by: Jacob
Component: Core (Cache system) Version:
Severity: normal Keywords:
Cc: nesh@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 (1)

filecache.diff (5.7 KB ) - added by nesh <nesh [at] studioquattro [dot] co [dot] yu> 19 years ago.

Download all attachments as: .zip

Change History (4)

by nesh <nesh [at] studioquattro [dot] co [dot] yu>, 19 years ago

Attachment: filecache.diff added

comment:1 by nesh <nesh [at] studioquattro [dot] co [dot] yu>, 19 years ago

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()

comment:2 by Adrian Holovaty, 19 years ago

Component: ToolsCache system
Owner: changed from Adrian Holovaty to Jacob

comment:3 by Jacob, 19 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #515

Note: See TracTickets for help on using tickets.
Back to Top