forked from nkallen/cache-money
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlocal.rb
72 lines (61 loc) · 1.7 KB
/
local.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module Cash
class Local
delegate :respond_to?, :to => :@remote_cache
def initialize(remote_cache)
@remote_cache = remote_cache
end
def cache_locally
@remote_cache = LocalBuffer.new(original_cache = @remote_cache)
yield if block_given?
ensure
@remote_cache = original_cache
end
def method_missing(method, *args, &block)
autoload_missing_constants do
@remote_cache.send(method, *args, &block)
end
end
def autoload_missing_constants
yield if block_given?
rescue ArgumentError, MemCache::MemCacheError => error
lazy_load ||= Hash.new { |hash, hash_key| hash[hash_key] = true; false }
if error.to_s[/undefined class|referred/] && !lazy_load[error.to_s.split.last.constantize]
retry
else
raise error
end
end
end
class LocalBuffer
delegate :respond_to?, :to => :@remote_cache
def initialize(remote_cache)
@local_cache = {}
@remote_cache = remote_cache
end
def get(key, *options)
if @local_cache.has_key?(key)
@local_cache[key]
else
@local_cache[key] = @remote_cache.get(key, *options)
end
end
def set(key, value, *options)
@remote_cache.set(key, value, *options)
@local_cache[key] = value
end
def add(key, value, *options)
result = @remote_cache.add(key, value, *options)
if result == "STORED\r\n"
@local_cache[key] = value
end
result
end
def delete(key, *options)
@remote_cache.delete(key, *options)
@local_cache.delete(key)
end
def method_missing(method, *args, &block)
@remote_cache.send(method, *args, &block)
end
end
end