drgbpq5930 2015-07-16 07:37
浏览 41
已采纳

Revel框架中的缓存失效

I'm looking for a way to invalidate cached static content upon version change. Preferably using commit id to invalidate. Is there anyway to do this in revel framework ?

I would prefer if its automatic but I could live with updating it each time if its a single place I have to edit.

The current strategy I have is changing the name of the static content route to include version but this requires several changes. In places that feel unnatural, for instance in the routing file.

  • 写回答

2条回答 默认 最新

  • duanshan3427 2015-07-16 09:16
    关注

    You could do it manually via a config variable and an intercept method.

    resourceversion.go

    Create this file in your controllers folder:

    package controllers
    
    import (
        "github.com/revel/revel"
    )
    
    // interceptor method, called before every request. 
    // Sets a template variable to the resourceVersion read from app.conf
    func SetVersion(c *revel.Controller) revel.Result {
        c.RenderArgs["resourceVersion"] = revel.Config.StringDefault("resourceVersion", "1")
        return nil
    }
    

    init.go

    In the init() method, append this line:

    revel.InterceptMethod(controllers.SetVersion, revel.BEFORE)
    

    templates

    In your templates, where you want to use the resource version:

    <link rel="stylesheet" type="text/css" href="/public/css/style.css?{{.resourceVersion}}">
    

    app.conf

    And finally, the place you will update it - add this line above the dev section to apply to dev and prod, or have a different one in each, whatever suits.

    resourceVersion=20150716
    

    I guess you could create a script as part of your build and release process that would automatically edit this config variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R