Class: Sir::Backends::RamCache

Inherits:
Base
  • Object
show all
Defined in:
lib/sir/backends/ram_cache.rb

Constant Summary collapse

META =
{
    name:   "RAM Cache",
    author: "Lyjia"
}
DEFAULTS =
{}
VALUE =
0
EXPIRY =
1
MASK =
"*"
@@config =
DEFAULTS
@@ram_cache =
{}

Constants inherited from Base

Base::EXPORTS

Class Method Summary collapse

Methods inherited from Base

arity, configure, flush, post_configure

Class Method Details

.able?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/sir/backends/ram_cache.rb', line 67

def self.able?
  return true
end

.dumpObject



84
85
86
87
# File 'lib/sir/backends/ram_cache.rb', line 84

def self.dump
  @@ram_cache.each { |k, v| $stderr.puts("#{k}: #{v}") }
  return true
end

.get(key) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sir/backends/ram_cache.rb', line 17

def self.get(key)
  invalid = self.valid?({ key: key })
  raise ArgumentError, invalid if invalid

  Sir.annoy("I have a block!") if block_given?

  if x = @@ram_cache[key]

    if x[EXPIRY].nil? || x[EXPIRY] > Time.now.to_i

      return x[VALUE]

    else

      # cache entry is stale
      Sir.debug("Cache entry <#{key}> expired at #{x[VALUE]}. Deleting...")
      @@ram_cache.delete(key)

      super

    end

  else

    super

  end

end

.keys(mask = MASK) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/sir/backends/ram_cache.rb', line 120

def self.keys(mask = MASK)
  if mask == MASK
    return @@ram_cache.keys
  else
    return @@ram_cache.keys.bsearch {|x| (x =~ /#{mask}/i) }
  end
end

.kill(key) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
80
81
# File 'lib/sir/backends/ram_cache.rb', line 72

def self.kill(key)
  invalid = self.valid?({ key: key })
  raise ArgumentError, invalid if invalid

  if @@ram_cache.has_key?(key)
    @@ram_cache.delete(key)
    return true
  end

end

.lengthObject



96
97
98
# File 'lib/sir/backends/ram_cache.rb', line 96

def self.length
  @@ram_cache.length
end

.nukeObject



90
91
92
93
# File 'lib/sir/backends/ram_cache.rb', line 90

def self.nuke
  @@ram_cache = {}
  return true
end

.put(key, value, expiry = Sir.config(:default_expiry)) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sir/backends/ram_cache.rb', line 48

def self.put(key, value, expiry = Sir.config(:default_expiry))
  invalid = self.valid?({ key: key, value: value, expiry: expiry })
  raise ArgumentError, invalid if invalid

  #### This code snippet needs to be DRYed
  expiry = expiry.to_i unless expiry.nil?

  # normalize relative/absolute times to absolute time, skip if expiry = nil
  if !expiry.nil? && Time.now.to_i > expiry
    expiry += Time.now.to_i
  end
  ####

  @@ram_cache[key] = [value, expiry]
  return value

end

.sweep(include_nil_expiry = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sir/backends/ram_cache.rb', line 101

def self.sweep(include_nil_expiry = nil)
  Sir.debug("Invalidating stale keys... (#{@@ram_cache.keys.length} keys) #{include_nil_expiry}")

  @@ram_cache.each_key do |k|

    if @@ram_cache[k][EXPIRY].nil? && include_nil_expiry
      @@ram_cache.delete(k)
      next
    end

    @@ram_cache.delete(k) if self.key_expired?(k)

  end

  Sir.debug("Finished! (now #{@@ram_cache.keys.length} keys)")
  return true
end