rails, finding a incremental number to use for cache busting
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)..
精彩评论