will_cache 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/will_cache/cacheable.rb +13 -11
- data/test/functional/will_cache_test.rb +13 -2
- metadata +3 -3
data/lib/will_cache/cacheable.rb
CHANGED
@@ -1,36 +1,38 @@
|
|
1
1
|
module WillCache
|
2
2
|
module Cacheable
|
3
3
|
|
4
|
-
def cache
|
5
|
-
Rails.cache
|
6
|
-
end
|
7
|
-
|
8
4
|
def expire_cache(method_name = nil, args = {})
|
9
5
|
with = args[:with]
|
10
|
-
cache.delete(method_cache_key(method_name, with))
|
6
|
+
Rails.cache.delete(method_cache_key(method_name, with))
|
11
7
|
true
|
12
8
|
end
|
13
9
|
alias :clear_cache :expire_cache
|
14
10
|
|
15
11
|
def cached(method_name, args = {})
|
16
12
|
with = args[:with]
|
17
|
-
|
18
|
-
|
19
|
-
|
13
|
+
|
14
|
+
# Rails.fetch is broken
|
15
|
+
# http://developingsimplicity.com/posts/rails-cache-fetch
|
16
|
+
key = method_cache_key(method_name, with)
|
17
|
+
if Rails.cache.exist?(key)
|
18
|
+
Rails.cache.read(key)
|
19
|
+
else
|
20
|
+
Rails.cache.write(key, do_send(method_name, with))
|
21
|
+
end
|
20
22
|
end
|
21
23
|
alias :caches :cached
|
22
24
|
|
23
25
|
def fetch_cache(method_name, args = {})
|
24
26
|
with = args[:with]
|
25
|
-
cache.read(method_cache_key(method_name, with))
|
27
|
+
Rails.cache.read(method_cache_key(method_name, with))
|
26
28
|
end
|
27
29
|
|
28
30
|
def write_cache(key, value, ttl = nil)
|
29
|
-
cache.write(key, value, :expires_in => ttl)
|
31
|
+
Rails.cache.write(key, value, :expires_in => ttl)
|
30
32
|
end
|
31
33
|
|
32
34
|
def read_cache(key)
|
33
|
-
cache.read(key)
|
35
|
+
Rails.cache.read(key)
|
34
36
|
end
|
35
37
|
|
36
38
|
def do_send(method_name, with)
|
@@ -30,12 +30,23 @@ class WillCacheTest < Test::Unit::TestCase
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def test_cached_on_class_method
|
33
|
-
|
33
|
+
key = "User:count"
|
34
|
+
mock(Rails.cache).exist?(key) { false }
|
35
|
+
mock(Rails.cache).write(key, 1) { 1 }
|
34
36
|
assert_equal 1, User.cached(:count)
|
35
37
|
end
|
36
38
|
|
37
39
|
def test_cached_on_instance_method
|
38
|
-
|
40
|
+
key = "User:1:articles"
|
41
|
+
mock(Rails.cache).exist?(key) { false }
|
42
|
+
mock(Rails.cache).write(key, @user.articles) { @user.articles }
|
43
|
+
assert_equal @user.articles, @user.cached(:articles)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_cached_on_instance_method_on_hit
|
47
|
+
key = "User:1:articles"
|
48
|
+
mock(Rails.cache).exist?(key) { true }
|
49
|
+
mock(Rails.cache).read(key) { @user.articles }
|
39
50
|
assert_equal @user.articles, @user.cached(:articles)
|
40
51
|
end
|
41
52
|
|
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: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dejan Simic
|