Class: ActiveSupport::Cache::TTLedMemCacheStore

Inherits:
MemCacheStore
  • Object
show all
Defined in:
lib/that_old_cache/ttled_mem_cache_store.rb

Instance Method Summary collapse

Instance Method Details

#expired?(key) ⇒ Boolean

only works for TTLed keys

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/that_old_cache/ttled_mem_cache_store.rb', line 32

def expired?(key)
  begin
    ttl(key) < Time.now.to_i
  rescue NoMethodError 
    false
  end
end

#read(key, options = nil) ⇒ Object

Adds the following option:

:valid_for => true 
:raw is not supported with :valid_for


22
23
24
25
26
27
28
29
# File 'lib/that_old_cache/ttled_mem_cache_store.rb', line 22

def read(key, options = nil)
  if options && options.delete(:valid_for)
    options.delete(:raw)
    parse(super)[:data] rescue nil
  else
    super
  end
end

#ttl(key) ⇒ Object

only works for TTLed keys



41
42
43
# File 'lib/that_old_cache/ttled_mem_cache_store.rb', line 41

def ttl(key)
  parse(read(key))[:ttl] rescue nil
end

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

Adds the following option:

:valid_for => number of seconds from now that the cache with "expire"
:raw is not supported with :valid_for


10
11
12
13
14
15
16
17
# File 'lib/that_old_cache/ttled_mem_cache_store.rb', line 10

def write(key, value, options = nil)
  if options && options[:valid_for]
    options.delete(:raw)
    super(key, encode({ :data => value, :ttl => time_for(options.delete(:valid_for)) }), options)
  else
    super
  end
end