Code

Opened 9 years ago

Closed 9 years ago

Last modified 8 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: UI/UX:

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> 9 years ago.

Download all attachments as: .zip

Change History (4)

Changed 9 years ago by nesh <nesh [at] studioquattro [dot] co [dot] yu>

comment:1 Changed 9 years ago 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()

comment:2 Changed 9 years ago by adrian

  • Component changed from Tools to Cache system
  • Owner changed from adrian to jacob

comment:3 Changed 9 years ago by jacob

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #515

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.