Class: Cash::LocalBuffer
- Inherits:
-
Object
show all
- Defined in:
- lib/cash/local.rb
Constant Summary
collapse
- MULTI_GET_RESULTS_THRESHOLD =
1
Instance Method Summary
collapse
Constructor Details
#initialize(remote_cache) ⇒ LocalBuffer
Returns a new instance of LocalBuffer.
42
43
44
45
|
# File 'lib/cash/local.rb', line 42
def initialize(remote_cache)
@local_cache = {}
@remote_cache = remote_cache
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
101
102
103
|
# File 'lib/cash/local.rb', line 101
def method_missing(method, *args, &block)
@remote_cache.send(method, *args, &block)
end
|
Instance Method Details
#add(key, value, *options) ⇒ Object
86
87
88
89
90
91
92
|
# File 'lib/cash/local.rb', line 86
def add(key, value, *options)
result = @remote_cache.add(key, value, *options)
if result == "STORED\r\n"
@local_cache[key] = value
end
result
end
|
#delete(key, *options) ⇒ Object
94
95
96
97
|
# File 'lib/cash/local.rb', line 94
def delete(key, *options)
@remote_cache.delete(key, *options)
@local_cache.delete(key)
end
|
#get(key, *options) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/cash/local.rb', line 47
def get(key, *options)
if @local_cache.has_key?(key)
@local_cache[key]
else
@local_cache[key] = @remote_cache.get(key, *options)
end
end
|
#get_multi(keys, *options) ⇒ Object
Model.find_by_index() uses get_multi to get the object after getting the id through the index. Simplest way of utilizing the local cache is to override get_multi and use a threshold of 1. Using a higher threshold could be an optimization for get_multi calls returning more than one value.
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/cash/local.rb', line 59
def get_multi(keys, *options)
results = {}
remaining_keys = []
keys.each do |key|
if @local_cache.has_key?(key)
results[key] = @local_cache[key]
else
remaining_keys << key
end
end
if !remaining_keys.empty?
multi = @remote_cache.get_multi(remaining_keys, *options)
if multi
multi.each do |k,v|
@local_cache[k] = v if multi.size() <= MULTI_GET_RESULTS_THRESHOLD
results[k] = v
end
end
end
results
end
|
#set(key, value, *options) ⇒ Object
81
82
83
84
|
# File 'lib/cash/local.rb', line 81
def set(key, value, *options)
@remote_cache.set(key, value, *options)
@local_cache[key] = value
end
|