will_cache 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/will_cache/cacheable.rb +7 -2
- data/test/functional/will_cache_test.rb +19 -0
- metadata +3 -3
data/lib/will_cache/cacheable.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module WillCache
|
2
2
|
module Cacheable
|
3
|
-
def expire_cache(method_name, args = {})
|
3
|
+
def expire_cache(method_name = nil, args = {})
|
4
4
|
with = args[:with]
|
5
5
|
Rails.cache.delete(method_cache_key(method_name, with))
|
6
6
|
end
|
@@ -12,6 +12,11 @@ module WillCache
|
|
12
12
|
}
|
13
13
|
end
|
14
14
|
|
15
|
+
def fetch_cache(method_name, args = {})
|
16
|
+
with = args[:with]
|
17
|
+
Rails.cache.read(method_cache_key(method_name, with))
|
18
|
+
end
|
19
|
+
|
15
20
|
def do_send(method_name, with)
|
16
21
|
if with.blank?
|
17
22
|
send(method_name)
|
@@ -22,7 +27,7 @@ module WillCache
|
|
22
27
|
|
23
28
|
def method_cache_key(method_name, with)
|
24
29
|
if self.is_a?(ActiveRecord::Base)
|
25
|
-
base =
|
30
|
+
base = [self.class, id, method_name].compact.join(':').gsub(' ', '_')
|
26
31
|
else
|
27
32
|
base = "#{self}:#{method_name}"
|
28
33
|
end
|
@@ -8,6 +8,8 @@ end
|
|
8
8
|
class User < ActiveRecord::Base
|
9
9
|
has_many :articles
|
10
10
|
acts_as_cached
|
11
|
+
|
12
|
+
after_update :expire_cache
|
11
13
|
end
|
12
14
|
|
13
15
|
class Article < ActiveRecord::Base
|
@@ -41,4 +43,21 @@ class WillCacheTest < Test::Unit::TestCase
|
|
41
43
|
mock(Rails.cache).delete("User:1:articles")
|
42
44
|
@user.expire_cache(:articles)
|
43
45
|
end
|
46
|
+
|
47
|
+
def test_expire_cache2
|
48
|
+
mock(Rails.cache).delete("User:1:random:2")
|
49
|
+
@user.expire_cache('random:2')
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_fetch_cache
|
53
|
+
mock(Rails.cache).read("User:1:articles")
|
54
|
+
@user.fetch_cache(:articles)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_expire_cache_after_update
|
58
|
+
mock(Rails.cache).delete("User:#{@user.id}")
|
59
|
+
@user.name = 'dejan'
|
60
|
+
@user.save!
|
61
|
+
end
|
62
|
+
|
44
63
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: will_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dejan Simic
|