my_stuff-cache 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
data/lib/my_stuff/cache/base.rb
CHANGED
@@ -9,9 +9,11 @@ module MyStuff
|
|
9
9
|
# - MyStuff::Cache::MemoryCache
|
10
10
|
# - MyStuff::Cache::MemcachedCache
|
11
11
|
class Base
|
12
|
+
attr_reader :prefix
|
12
13
|
attr_reader :logger
|
13
14
|
def initialize options = {}
|
14
15
|
@logger = options[:logger] || create_logger
|
16
|
+
@prefix = options[:prefix]
|
15
17
|
end
|
16
18
|
|
17
19
|
# Try to get ids from cache, falling back to the given block.
|
@@ -18,7 +18,12 @@ module MyStuff
|
|
18
18
|
|
19
19
|
def get keys, options = {}
|
20
20
|
begin
|
21
|
-
|
21
|
+
if prefix.nil? || prefix.empty?
|
22
|
+
mc_keys = keys
|
23
|
+
else
|
24
|
+
mc_keys = keys.map{|x| prefix + x}
|
25
|
+
end
|
26
|
+
results = @mc.get(mc_keys) unless keys.empty?
|
22
27
|
keys.map{|k| results[k]}
|
23
28
|
rescue Memcached::Error => e
|
24
29
|
logger.warn e.inspect
|
@@ -30,7 +35,8 @@ module MyStuff
|
|
30
35
|
options[:ttl] ||= 0
|
31
36
|
begin
|
32
37
|
values.each do |k,v|
|
33
|
-
|
38
|
+
prefix = self.prefix || ''
|
39
|
+
@mc.set(prefix + k, v, options[:ttl])
|
34
40
|
end
|
35
41
|
rescue Memcached::Error => e
|
36
42
|
logger.warn e
|