Class: LocalCache
- Inherits:
-
ActiveSupport::Cache::Store
- Object
- ActiveSupport::Cache::Store
- LocalCache
show all
- Defined in:
- lib/local_cache.rb
Instance Method Summary
collapse
-
#delete(name, options = nil) ⇒ Object
-
#delete_matched(matcher, options = nil) ⇒ Object
-
#exist?(key) ⇒ Boolean
-
#get(key) ⇒ Object
-
#get_i(key) ⇒ Object
-
#get_multi(keys, raw = false) ⇒ Object
-
#increment(key, val = 1) ⇒ Object
-
#initialize(options = {}) ⇒ LocalCache
constructor
Initialize a new LocalCache.
-
#put(key, val, seconds_to_store = 999999, raw = false) ⇒ Object
-
#read(name, options = nil) ⇒ Object
-
#write(name, value, options = nil) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ LocalCache
Initialize a new LocalCache.
Options:
:size => 1000 - max number of objects to store
:ttl => 60 - default ttl (can be overridden on any PUT/WRITE)
11
12
13
14
15
16
17
18
19
|
# File 'lib/local_cache.rb', line 11
def initialize(options={})
puts 'Creating new LocalCache'
@size = options[:size] || 1000
@default_expires_in = options[:ttl] || 60
@cache = {}
@cache_list = []
end
|
Instance Method Details
#delete(name, options = nil) ⇒ Object
96
97
98
99
|
# File 'lib/local_cache.rb', line 96
def delete(name, options = nil)
super
@cache.delete(name)
end
|
#delete_matched(matcher, options = nil) ⇒ Object
101
102
103
104
|
# File 'lib/local_cache.rb', line 101
def delete_matched(matcher, options = nil)
super
raise "delete_matched not supported by LocalCache"
end
|
#exist?(key) ⇒ Boolean
57
58
59
|
# File 'lib/local_cache.rb', line 57
def exist?(key)
return !get(key).nil?
end
|
#get(key) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/local_cache.rb', line 21
def get(key)
if @cache.has_key? key
expires = @cache[key][0]
if expires - Time.now > 0
return @cache[key][1]
else
delete(key)
end
else
end
return nil
end
|
#get_i(key) ⇒ Object
51
52
53
54
55
|
# File 'lib/local_cache.rb', line 51
def get_i(key)
val = get(key)
return nil if val.nil?
return val.to_i
end
|
#get_multi(keys, raw = false) ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/local_cache.rb', line 42
def get_multi(keys, raw=false)
ret = {}
keys.each do |k|
val = get(k)
ret[k] = val unless val.nil?
end
ret
end
|
#increment(key, val = 1) ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'lib/local_cache.rb', line 61
def increment(key, val=1)
ret = get(key)
if ret.is_a?(Fixnum)
ret += val
else
ret = val
put(key, ret)
end
end
|
#put(key, val, seconds_to_store = 999999, raw = false) ⇒ Object
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/local_cache.rb', line 71
def put(key, val, seconds_to_store=999999, raw=false)
seconds_to_store = seconds_to_store || @default_expires_in
@cache[key] = [Time.now+seconds_to_store, val]
@cache_list << key
while @cache.size > @size && @cache_list.size > 0
to_remove = @cache_list.pop
@cache.delete(to_remove) unless to_remove.nil?
end
end
|
#read(name, options = nil) ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/local_cache.rb', line 82
def read(name, options = nil)
super
ret = get(name)
return ret
end
|
#write(name, value, options = nil) ⇒ Object
90
91
92
93
94
|
# File 'lib/local_cache.rb', line 90
def write(name, value, options = nil)
super
put(name, value, options.nil? ? nil : options[:expires_in])
end
|