will_cache 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
data/lib/will_cache/cacheable.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require File.expand_path("../simple_memoize", __FILE__)
|
2
|
-
|
3
1
|
module WillCache
|
4
2
|
module Cacheable
|
5
3
|
|
@@ -7,7 +5,6 @@ module WillCache
|
|
7
5
|
with = args[:with]
|
8
6
|
delete_cache(method_cache_key(method_name, with))
|
9
7
|
end
|
10
|
-
memoize :expire_cache
|
11
8
|
alias :clear_cache :expire_cache
|
12
9
|
|
13
10
|
def cached(method_name, args = {})
|
@@ -22,36 +19,35 @@ module WillCache
|
|
22
19
|
write_cache(key, do_send(method_name, with))
|
23
20
|
end
|
24
21
|
end
|
25
|
-
memoize :cached
|
26
22
|
alias :caches :cached
|
27
23
|
|
24
|
+
def refresh_cache(method_name = nil, args = {})
|
25
|
+
expire_cache(method_name, args)
|
26
|
+
cached(method_name, args)
|
27
|
+
end
|
28
|
+
|
28
29
|
def fetch_cache(method_name, args = {})
|
29
30
|
with = args[:with]
|
30
31
|
read_cache(method_cache_key(method_name, with))
|
31
32
|
end
|
32
|
-
memoize :fetch_cache
|
33
33
|
|
34
34
|
def write_cache(key, value)
|
35
35
|
Rails.cache.write(key, value)
|
36
36
|
value
|
37
37
|
end
|
38
|
-
memoize :write_cache
|
39
38
|
|
40
39
|
def read_cache(key)
|
41
40
|
Rails.cache.read(key)
|
42
41
|
end
|
43
|
-
memoize :read_cache
|
44
42
|
|
45
43
|
def delete_cache(key)
|
46
44
|
Rails.cache.delete(key)
|
47
45
|
true
|
48
46
|
end
|
49
|
-
memoize :delete_cache
|
50
47
|
|
51
48
|
def cache_exist?(key)
|
52
49
|
Rails.cache.exist?(key)
|
53
50
|
end
|
54
|
-
memoize :cache_exist?
|
55
51
|
|
56
52
|
def method_cache_key(method_name, with)
|
57
53
|
if self.is_a?(ActiveRecord::Base)
|
@@ -65,7 +61,6 @@ module WillCache
|
|
65
61
|
"#{base}:#{with.inspect}"
|
66
62
|
end
|
67
63
|
end
|
68
|
-
memoize :method_cache_key
|
69
64
|
|
70
65
|
def do_send(method_name, with)
|
71
66
|
if with.blank?
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
require 'active_support/cache'
|
3
|
-
|
3
|
+
require 'FileUtils'
|
4
4
|
class Rails
|
5
5
|
def self.cache
|
6
6
|
@@cache
|
@@ -37,7 +37,8 @@ class WillCacheTest < Test::Unit::TestCase
|
|
37
37
|
|
38
38
|
@user = User.create!
|
39
39
|
@user.articles << Article.new(:body => 'hey')
|
40
|
-
|
40
|
+
FileUtils.rm_rf('tmp/cache')
|
41
|
+
Rails.cache = ActiveSupport::Cache::FileStore.new('tmp/cache')
|
41
42
|
end
|
42
43
|
|
43
44
|
def test_cached_on_class_method
|
@@ -80,6 +81,15 @@ class WillCacheTest < Test::Unit::TestCase
|
|
80
81
|
@user.fetch_cache(:articles)
|
81
82
|
end
|
82
83
|
|
84
|
+
def test_refresh_cache
|
85
|
+
assert_equal 1, User.cached(:count)
|
86
|
+
assert_equal 1, User.fetch_cache(:count)
|
87
|
+
User.create!
|
88
|
+
assert_equal 1, User.fetch_cache(:count)
|
89
|
+
assert_equal 2, User.refresh_cache(:count)
|
90
|
+
assert_equal 2, User.fetch_cache(:count)
|
91
|
+
end
|
92
|
+
|
83
93
|
def test_expire_cache_after_update
|
84
94
|
mock(Rails.cache).delete("User:#{@user.id}")
|
85
95
|
@user.name = 'dejan'
|
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: 13
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 9
|
10
|
+
version: 0.0.9
|
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-
|
18
|
+
date: 2011-12-06 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -30,11 +30,9 @@ extra_rdoc_files: []
|
|
30
30
|
files:
|
31
31
|
- Rakefile
|
32
32
|
- lib/will_cache/cacheable.rb
|
33
|
-
- lib/will_cache/simple_memoize.rb
|
34
33
|
- lib/will_cache.rb
|
35
34
|
- test/fixtures/database.yml
|
36
35
|
- test/fixtures/schema.rb
|
37
|
-
- test/functional/tmp/68B/030/User%3Asum%3A%5B2%2C+3%5D
|
38
36
|
- test/functional/will_cache_test.rb
|
39
37
|
- test/test_helper.rb
|
40
38
|
- README.md
|
@@ -1,44 +0,0 @@
|
|
1
|
-
# https://github.com/bradediger/simple_memoize/blob/6cca4b99579566957ef5c045a3b0c21b6455cf59/lib/simple_memoize.rb
|
2
|
-
module SimpleMemoize
|
3
|
-
VERSION = '1.1.0'
|
4
|
-
|
5
|
-
module Module
|
6
|
-
def memoize(*method_names)
|
7
|
-
method_names.each do |method_name|
|
8
|
-
method_name = method_name.to_s
|
9
|
-
stripped_method_name = method_name.sub(/([!?])$/, '')
|
10
|
-
|
11
|
-
punctuation = $1
|
12
|
-
wordy_punctuation = (punctuation == '!' ? '_bang' : '_huh') if punctuation
|
13
|
-
ivar_name = "@#{stripped_method_name}#{wordy_punctuation}"
|
14
|
-
|
15
|
-
memoized_method_name = "#{stripped_method_name}_with_memo#{punctuation}"
|
16
|
-
regular_method_name = "#{stripped_method_name}_without_memo#{punctuation}"
|
17
|
-
|
18
|
-
unless (instance_methods + private_instance_methods).include?(method_name)
|
19
|
-
raise NoMethodError, "The Method '#{method_name}' cannot be memoized because it doesn't exist in #{self}"
|
20
|
-
end
|
21
|
-
return if self.method_defined?(memoized_method_name)
|
22
|
-
|
23
|
-
self.class_eval "
|
24
|
-
def #{memoized_method_name}(*args)
|
25
|
-
if defined?(#{ivar_name}) && #{ivar_name}.include?(args)
|
26
|
-
#{ivar_name}[args]
|
27
|
-
else
|
28
|
-
#{ivar_name} ||= Hash.new
|
29
|
-
#{ivar_name}[args] = #{regular_method_name}(*args)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
alias_method :#{regular_method_name}, :#{method_name}
|
34
|
-
alias_method :#{method_name}, :#{memoized_method_name}
|
35
|
-
|
36
|
-
protected :#{method_name} if protected_instance_methods.include?('#{regular_method_name}')
|
37
|
-
private :#{method_name} if private_instance_methods.include?('#{regular_method_name}')
|
38
|
-
"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
Module.send :include, SimpleMemoize::Module
|
Binary file
|