Class: Cachetastic::Adapters::LocalMemory

Inherits:
Base show all
Defined in:
lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/local_memory.rb

Overview

An adapter/store that keeps objects in local memory. This is great for development/testing, but probably shouldn’t be put into production. It’s also a very good example of how to write a adapter.

Instance Attribute Summary collapse

Attributes inherited from Base

#logger, #name

Instance Method Summary collapse

Methods inherited from Base

#configuration, configuration, #debug?, #initialize

Constructor Details

This class inherits a constructor from Cachetastic::Adapters::Base

Instance Attribute Details

#local_storeObject

Returns the value of attribute local_store.



6
7
8
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/local_memory.rb', line 6

def local_store
  @local_store
end

Instance Method Details

#delete(key, delay = 0) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/local_memory.rb', line 37

def delete(key, delay = 0)
  if delay <= 0
    self.local_store.delete(key.to_s)
  else
    so = self.get(key)
    if so
      self.set(so.key, so.value, delay)
    end
  end
end

#expire_allObject



16
17
18
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/local_memory.rb', line 16

def expire_all
  self.local_store = {}
end

#get(key) ⇒ Object

See Cachetastic::Adapters::Base



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/local_memory.rb', line 21

def get(key)
  so = self.local_store[key.to_s]
  if so
    if so.invalid?
      self.delete(key)
      return nil
    end
    return so.value
  end
  return nil
end

#set(key, value, expiry = 0) ⇒ Object



33
34
35
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/local_memory.rb', line 33

def set(key, value, expiry = 0)
  self.local_store[key.to_s] = Cachetastic::Adapters::StoreObject.new(key.to_s, value, expiry)
end

#setupObject



12
13
14
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/local_memory.rb', line 12

def setup
  self.local_store = {}
end

#statsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/local_memory.rb', line 48

def stats
  super
  num_keys = self.local_store.size
  s = "Number of Entries: #{num_keys}\n"
  if num_keys > 0
    expiries = []
    keys = []
    self.local_store.each do |key,value|
      keys << key
      expiries << value.expires_at
    end
    expiries.sort! {|x, y| x <=> y}
    oldest_expiry = expiries.first
    newest_expiry = expiries.last
    s += "Oldest Entry: #{oldest_expiry}\nNewest Entry: #{newest_expiry}\nKeys: #{keys.inspect}\n"
  end
  puts s + "\n"
end

#valid?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/local_memory.rb', line 8

def valid?
  true
end