Skip to content

Instantly share code, notes, and snippets.

@lifo
Created June 21, 2009 11:33
Show Gist options
  • Save lifo/133477 to your computer and use it in GitHub Desktop.
Save lifo/133477 to your computer and use it in GitHub Desktop.
# before
def read(name, options = nil)
super
file_name = real_file_path(name)
return nil unless File.exist?(file_name)
if (expires = expires_in(options)) > 0 and
(Time.now - File.mtime(file_name)) > expires
return nil
end
File.open(file_name, 'rb') { |f| Marshal.load(f) } rescue nil
end
# after
def read(name, options = nil)
super
file_name = real_file_path(name)
expires = expires_in(options)
if File.exist?(file_name) && (expires <= 0 || Time.now - File.mtime(file_name) < expires)
File.open(file_name, 'rb') { |f| Marshal.load(f) }
end
end
# and again
def read(name, options = nil)
super
file_name = real_file_path(name)
if File.exist?(file_name) && ((expires = expires_in(options)) <= 0 || Time.now - File.mtime(file_name) < expires)
File.open(file_name, 'rb') { |f| Marshal.load(f) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment