Skip to content

Instantly share code, notes, and snippets.

@Shu-Ji
Last active August 29, 2015 14:08
Show Gist options
  • Save Shu-Ji/e2131085a56710873ce9 to your computer and use it in GitHub Desktop.
Save Shu-Ji/e2131085a56710873ce9 to your computer and use it in GitHub Desktop.
@sched_base.scheduled_job('interval', minutes=1 if settings.DEBUG else 5)
def expire_cached_static_files_hash():
u"""当有静态文件发生变化时,使redis中缓存的hash值失效"""
import re
p = r'?v=([a-f0-9]{32})'
html = str(g.render.macro('default/index.html').cache())
hashes_now = sorted(re.findall(p, html) or [])
if not hashes_now:
return
k = settings.RDS_KS.K_CACHED_STATIC_FILE_TEMPLATES # 'sanhaolu:cache:static_files:hash'
h = g.rds.get(k) or ''
hashes_in_cache = sorted(re.findall(p, h) or [])
if (not hashes_in_cache) or (hashes_now == hashes_in_cache):
return
g.rds.delete(k)
logger.info('Cached files expired.')
# 还可以在这里urllib2.urlopen('http://localhost:8888').read()主动产生新的缓存,
# 也可以等下一个倒霉的用户访问时自动产生。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment