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 = {}) ⇒ Object
-
#delete_matched(matcher, options = {}) ⇒ Object
-
#exist?(key) ⇒ Boolean
-
#get(key, options = {}) ⇒ Object
-
#get_i(key) ⇒ Object
-
#get_multi(keys, options = {}) ⇒ Object
-
#increment(key, val = 1) ⇒ Object
-
#initialize(options = {}) ⇒ LocalCache
constructor
Initialize a new LocalCache.
-
#put(key, val, options = {}) ⇒ Object
-
#read(name, options = {}) ⇒ Object
-
#write(name, value, options = {}) ⇒ 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 = {}) ⇒ Object
95
96
97
98
|
# File 'lib/local_cache.rb', line 95
def delete(name, options={})
@cache.delete(name)
end
|
#delete_matched(matcher, options = {}) ⇒ Object
100
101
102
103
|
# File 'lib/local_cache.rb', line 100
def delete_matched(matcher, options={})
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, options = {}) ⇒ 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, options={})
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, options = {}) ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/local_cache.rb', line 42
def get_multi(keys, options={})
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, options = {}) ⇒ Object
71
72
73
74
75
76
77
78
79
|
# File 'lib/local_cache.rb', line 71
def put(key, val, options={})
seconds_to_store = options[:expires_in] || options[:ttl] || @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 = {}) ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/local_cache.rb', line 81
def read(name, options={})
ret = get(name, options)
return ret
end
|
#write(name, value, options = {}) ⇒ Object
89
90
91
92
93
|
# File 'lib/local_cache.rb', line 89
def write(name, value, options={})
put(name, value, options)
end
|