关键词搜索

源码搜索 ×
×

Web笔记-通过版本号控制客户端浏览器中的缓存

发布2020-03-01浏览4594次

详情内容

这里举个例子:

通过Python管理静态资源。但有时候,js或者css更新了,浏览器不知道,还使用缓存的情况。

如下所示:

通过在url中带个?这种方式,使得浏览器去获取新的资源

看下根请求下相关链接:

后面这一串是根据时间产生的随机数。

 

如果是开发环境,我们通过这种方式,使得客户端浏览器都获取到新的资源。

生产环境,通过文件进行指定版本:

相关的python代码如下:

在配置文件夹中新增:

在静态资源管理文件中,local_setting.py中配置了这个文件,就读一行,也就是版本号,如果没有,就使用随机数据去做,相关代码如下:

UrlManager.py

  1. from application import app
  2. from common.libs.DataHelper import getCurrentTime
  3. import os
  4. class UrlManager(object):
  5. @staticmethod
  6. def buildUrl(path):
  7. config_domain = app.config['DOMAIN']
  8. return "%s%s" % (config_domain['www'], path)
  9. @staticmethod
  10. def buildStaticUrl(path):
  11. path = "/static" + path + "?ver=" + UrlManager.getReleaseVersion();
  12. return UrlManager.buildUrl(path)
  13. #版本管理
  14. #开发模式 使用时间作为版本号
  15. #生产模式 使用版本文件进行管理
  16. @staticmethod
  17. def getReleaseVersion():
  18. ver = "%s" % (getCurrentTime("%Y%m%d%H%M%S%f"))
  19. release_path = app.config.get("RELEASE_PATH");
  20. if release_path and os.path.exists(release_path):
  21. with open(release_path, "r") as f:
  22. ver = f.readline()
  23. return ver
  24. return ver

其中getCurrentTimer如下:

DataHelper.py

  1. import datetime
  2. def getCurrentTime(frm = "%Y-%m-%d %H:%M:%S"):
  3. dt = datetime.datetime.now()
  4. return dt.strftime(frm)

app.config.get(XXX)中这个app是在核心文件(核心变量定义文件中定义的)

如下:

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载