本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7586110
建立时间:2006年5月29日




[Django]File based cache helpers for django
软件技术

lhwork 发表于 2007/3/17 11:44:28

Just two helper functions to manage cache witch is using file base dependent data.from django.core.cache import cacheimport sha, osFILE_CACHE_TIMEOUT = 60 * 60 * 60 * 24 * 31 # 1 monthFILE_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#


阅读全文(4594) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.326 second(s), page refreshed 144751303 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号