forked from chuyeow/can_has_cached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
can_has_cached.rb
111 lines (91 loc) · 3.14 KB
/
can_has_cached.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
$:.unshift File.dirname(__FILE__)
require 'rubygems'
require 'memcached'
require 'set'
module CanHasCached
# Stolen from ActiveSupport to support Hash#slice
Hash.class_eval do
# Returns a new hash with only the given keys.
def slice(*keys)
allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
reject { |key,| !allowed.include?(key) }
end
def slice!(*keys)
replace(slice(*keys))
end
end
def self.included(base)
base.extend(ClassMethods)
base.send(:include, InstanceMethods)
end
module ClassMethods
@ttl = nil
@cache_config = nil
def cache
raise ArgumentError, "cache_config must be set before you can use CanHasCached" if cache_config.nil?
@cache ||= Memcached.new(cache_config[:servers], cache_config.slice(*allowed_options))
end
def cache_config
@cache_config
end
def cache_config=(config)
raise ArgumentError, "cache_config for CanHasCached must be a Hash" unless config.is_a?(Hash)
# Set the config now
@cache_config = config
end
def ttl
@ttl
end
def ttl=(new_ttl)
raise ArgumentError, "ttl for CanHasCached must be a Fixnum" unless new_ttl.is_a?(Fixnum)
@ttl = new_ttl
end
# Returns the cache key to use for a given ID. This generally includes the class name and cache version, if any.
def cache_key(cache_id)
version = cache_config[:version] unless cache_config.nil?
[self.name, version, cache_id].compact.join(':').gsub(' ', '_')
end
# Sets value into cache, with the given optional TTL (in seconds). If <code>ttl</code> is not given, it's taken from
# the @ttl class variable, failing which, from cache_config[:ttl].
def set_cache(cache_id, value, ttl = nil)
cache.set(cache_key(cache_id), value, ttl || self.ttl || cache_config[:ttl] || 0)
end
# Gets cached value from cache, auto-magically loading any missing constants if needed for unmarshalling.
# Warning: this method rescues from Memcached::NotFound and returns nil if the key does not exist!
def get_cache(cache_id)
begin
autoload_missing_constants do
cache.get(cache_key(cache_id))
end
rescue Memcached::NotFound
nil
end
end
private
# Stolen from cache_fu's implementation.
def autoload_missing_constants
yield
rescue ArgumentError => 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] then retry
else raise error end
end
def allowed_options
[:namespace, :hash, :distribution, :support_cas, :tcp_nodelay, :no_block, :buffer_request, :show_not_found_backtraces]
end
end
module InstanceMethods
def cache_config
self.class.cache_config
end
def set_cache(cache_id, value = self, ttl = nil)
self.class.set_cache(cache_id, value, ttl)
end
def get_cache(cache_id)
self.class.get_cache(cache_id(key), options, &block)
end
def cache
self.class.cache
end
end
end