开发者

rails, finding a incremental number to use for cache busting

开发者 https://www.devze.com 2023-03-29 16:19 出处:网络
In rails, I would like to find an incremental number that increases or at least changes with everyone deployment or git commit/push.

In rails, I would like to find an incremental number that increases or at least changes with everyone deployment or git commit/push.

I need this for cache busting the CDN.

config.action_controller.asset_path = proc { |asset_path|
  "/rel-#{RELEASE_NUMBER}#{asset_path}"
}

Any suggestion开发者_C百科s? Thanks


Something like?

def release_sha
  @release_sha ||= `git log -1 --pretty=format:%h 2>/dev/null`
end

You could do one better with this approach and get the SHA for different folders/files.

def release_sha(path=nil)
  @release_sha ||= {}
  @release_sha[path] ||= `git log -1 --pretty=format:%h #{path} 2>/dev/null`
end

Then call it like release_sha('/public/images') for the latest SHA for that folder.


Just use the current timestamp (epoch)..

Time.now.usec

You can write this value to a constant and put it in config/initlaizers/cache_buster.rb for example with a script (or write your own rake task)..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号