Skip to content

Instantly share code, notes, and snippets.

@betamatt
Created February 21, 2010 04:07
Show Gist options
  • Save betamatt/310111 to your computer and use it in GitHub Desktop.
Save betamatt/310111 to your computer and use it in GitHub Desktop.
Example of a compressed file-based cache store
ActiveSupport::Cache::FileStore.class_eval do
def read(name, options = nil)
super
File.open(real_file_path(name), 'rb') do |f|
gz = Zlib::GzipReader.new(f)
value = Marshal.load(gz)
gz.close
value
end
rescue => e
nil
end
def write(name, value, options = nil)
super
ensure_cache_path(File.dirname(real_file_path(name)))
File.atomic_write(real_file_path(name), cache_path) do |f|
gz = Zlib::GzipWriter.new(f)
Marshal.dump(value, gz)
gz.close
end
value
rescue => e
logger.error "Couldn't create cache directory: #{name} (#{e.message})" if logger
end
private
def real_file_path(name)
'%s/%s.cache.gz' % [@cache_path, name.gsub('?', '.').gsub(':', '.')]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment