Skip to content

Instantly share code, notes, and snippets.

@bitcloud
Created December 30, 2014 01:10
Show Gist options
  • Save bitcloud/f6b9312149a8fc70324b to your computer and use it in GitHub Desktop.
Save bitcloud/f6b9312149a8fc70324b to your computer and use it in GitHub Desktop.
Rails: Delayed Memcache invalidation (unseful for heroku deployments)
# /config/initializers/00_asset_cache_check.rb
currenthash = File.read ".cacheversion"
cachehash = Rails.cache.read "cacheversion"
puts "Checking cache version: #{cachehash} against slug version: #{currenthash}\n"
if currenthash != cachehash
puts "flushing cache\n"
Rails.cache.clear
Rails.cache.write "cacheversion", currenthash
else
puts "cache ok\n"
end
# /lib/tasks/store_asset_cacheversion.rake
# add uuidtools to Gemfile
require "uuidtools"
def storeCacheVersion
cacheversion = UUIDTools::UUID.random_create
File.open(".cacheversion", "w") { |file| file.write(cacheversion) }
end
Rake::Task["assets:precompile"].enhance do
puts "Storing git hash in file for cache invalidation (assets:precompile)\n"
storeCacheVersion
end
Rake::Task["assets:precompile:nondigest"].enhance do
puts "Storing git hash in file for cache invalidation (assets:precompile:nondigest)\n"
storeCacheVersion
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment