class WithTimedCache::Cache
Attributes
key[R]
location[R]
max_age[R]
Public Class Methods
file_extension()
click to toggle source
# File lib/with_timed_cache/cache.rb, line 39 def self.file_extension @file_extension ||= self.local_class_name.sub(/Cache$/, "").downcase end
local_class_name()
click to toggle source
# File lib/with_timed_cache/cache.rb, line 43 def self.local_class_name @local_class_name ||= self.to_s.split("::").last rescue self.to_s end
new(key, opts = {})
click to toggle source
# File lib/with_timed_cache/cache.rb, line 8 def initialize(key, opts = {}) @key = key @max_age = opts[:max_age] || 1.hour @location = opts[:location] || File.join("tmp", filename) @location = File.join(@location, filename) if File.directory?(@location) self end
Public Instance Methods
exists?()
click to toggle source
# File lib/with_timed_cache/cache.rb, line 20 def exists? File.exists? @location end
filename()
click to toggle source
# File lib/with_timed_cache/cache.rb, line 32 def filename extension = self.class.file_extension filename = "#{@key}_cache" filename += ".#{extension}" unless extension.empty? filename end
read()
click to toggle source
# File lib/with_timed_cache/cache.rb, line 24 def read Marshal.load(File.read(@location)) end
stale?()
click to toggle source
# File lib/with_timed_cache/cache.rb, line 16 def stale? File.mtime(@location) < @max_age.ago end
write(data)
click to toggle source
# File lib/with_timed_cache/cache.rb, line 28 def write(data) File.open(@location, 'w') { |f| f.write Marshal.dump(data) } end