will_cache 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +2 -2
- data/lib/will_cache/cacheable.rb +29 -7
- data/lib/will_cache.rb +1 -0
- data/test/functional/will_cache_test.rb +15 -4
- metadata +4 -4
data/README.md
CHANGED
@@ -8,9 +8,9 @@ WillCache provides simple API for caching ActiveRecord objects that uses ActiveS
|
|
8
8
|
|
9
9
|
>> User.last.cached(:profile)
|
10
10
|
User Load (0.000581) (1 Row) SELECT * FROM `users` ORDER BY users.id DESC LIMIT 1
|
11
|
-
Cache miss:
|
11
|
+
Cache miss: User:65:profile
|
12
12
|
Profile Load (0.000454) (1 Row) SELECT * FROM `profiles` WHERE (`profiles`.user_id = 65) LIMIT 1
|
13
|
-
Cache write (will save 1.64ms):
|
13
|
+
Cache write (will save 1.64ms): User:65:profile
|
14
14
|
=> #<Profile id: 65, first_name: nil, last_name: nil, ...>
|
15
15
|
|
16
16
|
Example shows [inline log in Rails console](http://rors.org/2011/07/17/inline-logging-in-rails-console.html).
|
data/lib/will_cache/cacheable.rb
CHANGED
@@ -1,20 +1,42 @@
|
|
1
1
|
module WillCache
|
2
2
|
module Cacheable
|
3
|
+
|
4
|
+
def cache
|
5
|
+
Rails.cache
|
6
|
+
end
|
7
|
+
|
3
8
|
def expire_cache(method_name = nil, args = {})
|
4
9
|
with = args[:with]
|
5
|
-
|
10
|
+
cache.delete(method_cache_key(method_name, with))
|
11
|
+
true
|
6
12
|
end
|
7
|
-
|
13
|
+
alias :clear_cache :expire_cache
|
14
|
+
|
8
15
|
def cached(method_name, args = {})
|
9
16
|
with = args[:with]
|
10
|
-
|
11
|
-
|
12
|
-
|
17
|
+
|
18
|
+
# Rails.fetch is broken
|
19
|
+
# http://developingsimplicity.com/posts/rails-cache-fetch
|
20
|
+
key = method_cache_key(method_name, with)
|
21
|
+
if cache.exist?(key)
|
22
|
+
cache.read(key)
|
23
|
+
else
|
24
|
+
cache.write(key, do_send(method_name, with))
|
25
|
+
end
|
13
26
|
end
|
27
|
+
alias :caches :cached
|
14
28
|
|
15
29
|
def fetch_cache(method_name, args = {})
|
16
30
|
with = args[:with]
|
17
|
-
|
31
|
+
cache.read(method_cache_key(method_name, with))
|
32
|
+
end
|
33
|
+
|
34
|
+
def write_cache(key, value, ttl = nil)
|
35
|
+
cache.write(key, value, :expires_in => ttl)
|
36
|
+
end
|
37
|
+
|
38
|
+
def read_cache(key)
|
39
|
+
cache.read(key)
|
18
40
|
end
|
19
41
|
|
20
42
|
def do_send(method_name, with)
|
@@ -34,7 +56,7 @@ module WillCache
|
|
34
56
|
if with.blank?
|
35
57
|
base
|
36
58
|
else
|
37
|
-
"#{base}
|
59
|
+
"#{base}:#{with})"
|
38
60
|
end
|
39
61
|
end
|
40
62
|
end
|
data/lib/will_cache.rb
CHANGED
@@ -30,23 +30,34 @@ 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
|
|
42
53
|
def test_expire_cache
|
43
54
|
mock(Rails.cache).delete("User:1:articles")
|
44
|
-
@user.expire_cache(:articles)
|
55
|
+
assert @user.expire_cache(:articles)
|
45
56
|
end
|
46
57
|
|
47
58
|
def test_expire_cache2
|
48
59
|
mock(Rails.cache).delete("User:1:random:2")
|
49
|
-
@user.expire_cache('random:2')
|
60
|
+
assert @user.expire_cache('random:2')
|
50
61
|
end
|
51
62
|
|
52
63
|
def test_fetch_cache
|
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: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dejan Simic
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-07-
|
18
|
+
date: 2011-07-19 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|