Opened 19 years ago

Closed 19 years ago

#953 closed enhancement (invalid)

helper functions to get/set file dependant cache data

Reported by: Nebojša Đorđević - nesh <nesh@…> Owned by: Jacob
Component: Core (Cache system) Version:
Severity: minor Keywords:
Cc: nesh@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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
#

Change History (2)

comment:1 by Jacob, 19 years ago

Can you please explain what this is and why you're filing it as a ticket?

comment:2 by Nebojša Đorđević - nesh <nesh@…>, 19 years ago

Resolution: invalid
Status: newclosed

Sorry, I didn't think before I send this ticket. :)

This is just pair of helper functions to manage cache entries which are dependent on some file stored data.

Probably ticket is not the right place for this, so I'm closing this.

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