will_cache 0.0.6 → 0.0.7
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/lib/will_cache/cacheable.rb +19 -8
- data/lib/will_cache/simple_memoize.rb +44 -0
- metadata +4 -3
data/lib/will_cache/cacheable.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require File.expand_path("../simple_memoize", __FILE__)
|
2
|
+
|
1
3
|
module WillCache
|
2
4
|
module Cacheable
|
3
5
|
|
@@ -5,6 +7,7 @@ module WillCache
|
|
5
7
|
with = args[:with]
|
6
8
|
delete_cache(method_cache_key(method_name, with))
|
7
9
|
end
|
10
|
+
memoize :expire_cache
|
8
11
|
alias :clear_cache :expire_cache
|
9
12
|
|
10
13
|
def cached(method_name, args = {})
|
@@ -19,38 +22,36 @@ module WillCache
|
|
19
22
|
write_cache(key, do_send(method_name, with))
|
20
23
|
end
|
21
24
|
end
|
25
|
+
memoize :cached
|
22
26
|
alias :caches :cached
|
23
27
|
|
24
28
|
def fetch_cache(method_name, args = {})
|
25
29
|
with = args[:with]
|
26
30
|
read_cache(method_cache_key(method_name, with))
|
27
31
|
end
|
32
|
+
memoize :fetch_cache
|
28
33
|
|
29
34
|
def write_cache(key, value)
|
30
35
|
Rails.cache.write(key, value)
|
31
36
|
value
|
32
37
|
end
|
38
|
+
memoize :write_cache
|
33
39
|
|
34
40
|
def read_cache(key)
|
35
41
|
Rails.cache.read(key)
|
36
42
|
end
|
43
|
+
memoize :read_cache
|
37
44
|
|
38
45
|
def delete_cache(key)
|
39
46
|
Rails.cache.delete(key)
|
40
47
|
true
|
41
48
|
end
|
49
|
+
memoize :delete_cache
|
42
50
|
|
43
51
|
def cache_exist?(key)
|
44
52
|
Rails.cache.exist?(key)
|
45
53
|
end
|
46
|
-
|
47
|
-
def do_send(method_name, with)
|
48
|
-
if with.blank?
|
49
|
-
send(method_name)
|
50
|
-
else
|
51
|
-
send(method_name, with)
|
52
|
-
end
|
53
|
-
end
|
54
|
+
memoize :cache_exist?
|
54
55
|
|
55
56
|
def method_cache_key(method_name, with)
|
56
57
|
if self.is_a?(ActiveRecord::Base)
|
@@ -64,6 +65,16 @@ module WillCache
|
|
64
65
|
"#{base}:#{with}"
|
65
66
|
end
|
66
67
|
end
|
68
|
+
memoize :method_cache_key
|
69
|
+
|
70
|
+
def do_send(method_name, with)
|
71
|
+
if with.blank?
|
72
|
+
send(method_name)
|
73
|
+
else
|
74
|
+
send(method_name, with)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
67
78
|
end
|
68
79
|
end
|
69
80
|
|
@@ -0,0 +1,44 @@
|
|
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
|
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: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 7
|
10
|
+
version: 0.0.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dejan Simic
|
@@ -30,6 +30,7 @@ extra_rdoc_files: []
|
|
30
30
|
files:
|
31
31
|
- Rakefile
|
32
32
|
- lib/will_cache/cacheable.rb
|
33
|
+
- lib/will_cache/simple_memoize.rb
|
33
34
|
- lib/will_cache.rb
|
34
35
|
- test/fixtures/database.yml
|
35
36
|
- test/fixtures/schema.rb
|