Class: TimedCachedStore

Inherits:
ActiveSupport::Cache::MemoryStore
  • Object
show all
Defined in:
lib/timed_cached_store.rb

Overview

A cache store implementation which stores everything into memory. it keeps a time stamp, allowing auto expiry of objects it has a similar behaviour to memcached, but does not require memcache however this will not clean up automatically, only upon request

Instance Method Summary collapse

Instance Method Details

#delete_all_expiredObject


26
27
28
29
30
31
32
# File 'lib/timed_cached_store.rb', line 26

def delete_all_expired
  @data.keys.each do |key|
    if key =~ /(\w+)_timestamp/
      delete_if_expired($1)
    end
  end
end

#exist?(name, options = nil) ⇒ Boolean


7
8
9
10
# File 'lib/timed_cached_store.rb', line 7

def exist?(name, options = nil)
  delete_if_expired name
  super
end

#read(name) ⇒ Object


12
13
14
15
# File 'lib/timed_cached_store.rb', line 12

def read(name)
  delete_if_expired name
  super
end

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


17
18
19
20
21
22
23
24
# File 'lib/timed_cached_store.rb', line 17

def write(name, value, options = nil)
  if options and options[:expires_in]
    time                  = Time.now
    ttl                   = time + options[:expires_in].to_i
    @data[ts_field(name)] = { :created_at => time, :expires_at => ttl }
  end
  super
end