Created
February 21, 2010 04:07
-
-
Save betamatt/310111 to your computer and use it in GitHub Desktop.
Example of a compressed file-based cache store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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