﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
953	helper functions to get/set file dependant cache data	Nebojša Đorđević - nesh <nesh@…>	Jacob	"{{{
#!python
from django.core.cache import cache
import sha, os

FILE_CACHE_TIMEOUT = 60 * 60 * 60 * 24 * 31 # 1 month
FILE_CACHE_FMT = '%(name)s_%(hash)s'

def set_cached_file(path, value):
    """""" Store file dependent data in cache.
        Timeout is set to FILE_CACHE_TIMEOUT (1month).
        
        Key is created from base name of file and SHA1 digest of the path.
    """"""
    
    mtime = os.path.getmtime(path)
    sh = sha.new()
    sh.update(path)
    hash = sh.hexdigest()
    name = os.path.basename(path)
    cache.set(FILE_CACHE_FMT % locals(), (mtime, value,), FILE_CACHE_TIMEOUT)
#

def get_cached_file(path, default=None):
    """""" Get file content from cache.
        If modification time differ return None and delete
        data from cache.
    """"""
    
    sh = sha.new()
    sh.update(path)
    hash = sh.hexdigest()
    name = os.path.basename(path)
    key = FILE_CACHE_FMT % locals()
    
    cached = cache.get(key, default)
    if cached is None:
        return None
    mtime, value = cached
    
    if (not os.path.isfile(path)) or (os.path.getmtime(path) != mtime): # file is changed or deleted
        cache.delete(key) # delete from cache
        return None
    else:
        return value
#
}}}"	enhancement	closed	Core (Cache system)		minor	invalid		nesh@…	Unreviewed	0	0	0	0	0	0
