Class: Arturo::FeatureCaching::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/arturo/feature_caching.rb

Overview

Quack like a Rails cache.

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



53
54
55
# File 'lib/arturo/feature_caching.rb', line 53

def initialize
  @data = {} # of the form {key => [value, expires_at or nil]}
end

Instance Method Details

#clearObject



76
77
78
# File 'lib/arturo/feature_caching.rb', line 76

def clear
  @data.clear
end

#read(name, options = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/arturo/feature_caching.rb', line 56

def read(name, options = nil)
  name = name.to_s
  value, expires_at = *@data[name]
  if value && (expires_at.blank? || expires_at > Time.now)
    value
  else
    nil
  end
end

#write(name, value, options = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/arturo/feature_caching.rb', line 65

def write(name, value, options = nil)
  name = name.to_s
  expires_at = if options && options.respond_to?(:[]) && options[:expires_in]
    Time.now + options.delete(:expires_in)
  else
    nil
  end
  value.freeze.tap do |val|
    @data[name] = [value, expires_at]
  end
end