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条)

报告相同问题?