cacher 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cacher.rb +61 -13
- data/lib/cacher/version.rb +1 -1
- metadata +1 -1
data/lib/cacher.rb
CHANGED
@@ -10,41 +10,78 @@ module Cacher
|
|
10
10
|
end
|
11
11
|
|
12
12
|
#### configuration methods ####
|
13
|
-
|
14
|
-
|
13
|
+
|
14
|
+
# sets "factory defaults"
|
15
|
+
def self.reset!
|
16
|
+
self.cache = nil
|
17
|
+
self.namespace = false
|
18
|
+
self.max_key_size = 250
|
19
|
+
# default to false because Rails.cache handles marshalling by default
|
20
|
+
self.marshal = false
|
21
|
+
disable!
|
22
|
+
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def configure
|
27
|
+
yield self
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(opts={}, &blk)
|
15
32
|
opts.each do |k, v|
|
16
33
|
send(:"#{k}=", v)
|
17
34
|
end
|
35
|
+
|
36
|
+
blk && configure(&blk)
|
18
37
|
end
|
19
38
|
|
39
|
+
####
|
40
|
+
# The backend cache. The backend cache should must implement:
|
41
|
+
#
|
42
|
+
# #get (or #read) - takes one argument and returns
|
43
|
+
# something from a cache.
|
44
|
+
# #set (or #write) - takes two arguments, a key and a value, and
|
45
|
+
# sets something in a cache.
|
20
46
|
attr_writer :cache
|
21
47
|
def cache
|
22
|
-
@cache
|
48
|
+
return @cache if instance_variable_defined? :@cache
|
49
|
+
|
50
|
+
Cacher.cache ||= if defined?(::Rails) and ::Rails.respond_to? :cache
|
51
|
+
Rails.cache
|
52
|
+
elsif defined?(::RAILS_CACHE)
|
53
|
+
RAILS_CACHE
|
54
|
+
else
|
55
|
+
raise ArgumentError.new <<-msg.strip
|
56
|
+
please define a cache backend for Cacher.
|
57
|
+
msg
|
58
|
+
end
|
59
|
+
|
60
|
+
@cache = Cacher.cache
|
61
|
+
end
|
62
|
+
|
63
|
+
attr_writer :namespace
|
64
|
+
def namespace
|
65
|
+
return @namespace if instance_variable_defined? :@namespace
|
66
|
+
@namespace = Cacher.namespace
|
23
67
|
end
|
24
68
|
|
25
|
-
attr_accessor :namespace
|
26
69
|
def namespaced?
|
27
70
|
!!namespace
|
28
71
|
end
|
29
72
|
|
30
73
|
attr_writer :max_key_size
|
31
74
|
def max_key_size
|
32
|
-
@max_key_size ||=
|
75
|
+
@max_key_size ||= Cacher.max_key_size
|
33
76
|
end
|
34
77
|
|
35
78
|
attr_writer :marshal
|
36
79
|
def marshal?
|
37
80
|
return @marshal if instance_variable_defined? :@marshal
|
38
|
-
|
39
|
-
# default to false because Rails.cache handles marshalling for us
|
40
|
-
@marshal = false
|
41
|
-
end
|
42
|
-
|
43
|
-
def enabled?
|
44
|
-
return @enabled if instance_variable_defined? :@enabled
|
45
|
-
@enabled = false
|
81
|
+
@marshal = Cacher.marshal?
|
46
82
|
end
|
47
83
|
|
84
|
+
attr_writer :enabled
|
48
85
|
def enable!
|
49
86
|
@enabled = true
|
50
87
|
end
|
@@ -53,7 +90,17 @@ module Cacher
|
|
53
90
|
@enabled = false
|
54
91
|
end
|
55
92
|
|
93
|
+
def enabled?
|
94
|
+
return @enabled if instance_variable_defined? :@enabled
|
95
|
+
@enabled = Cacher.enabled?
|
96
|
+
end
|
97
|
+
|
98
|
+
# set up Cacher with the defaults
|
99
|
+
reset!
|
100
|
+
|
101
|
+
##################
|
56
102
|
#### core api ####
|
103
|
+
##################
|
57
104
|
def key?(key)
|
58
105
|
return false unless enabled?
|
59
106
|
|
@@ -155,4 +202,5 @@ private
|
|
155
202
|
val
|
156
203
|
end
|
157
204
|
end
|
205
|
+
|
158
206
|
end
|
data/lib/cacher/version.rb
CHANGED