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.



189
190
191
# File 'lib/arturo/feature_caching.rb', line 189

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

Instance Method Details

#clearObject



217
218
219
# File 'lib/arturo/feature_caching.rb', line 217

def clear
  @data.clear
end

#delete(name) ⇒ Object



202
203
204
# File 'lib/arturo/feature_caching.rb', line 202

def delete(name)
  @data.delete(name)
end

#read(name, options = nil) ⇒ Object



193
194
195
196
197
198
199
200
# File 'lib/arturo/feature_caching.rb', line 193

def read(name, options = nil)
  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



206
207
208
209
210
211
212
213
214
215
# File 'lib/arturo/feature_caching.rb', line 206

def write(name, value, options = nil)
  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