model-cache 0.2.2 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/model-cache.rb +99 -1
- metadata +12 -6
- data/lib/model_cache.rb +0 -83
data/lib/model-cache.rb
CHANGED
@@ -1 +1,99 @@
|
|
1
|
-
|
1
|
+
module ModelCache
|
2
|
+
|
3
|
+
DEFAULT_TIME = 12.hours unless const_defined?(:DEFAULT_TIME)
|
4
|
+
NIL_OBJECT = :__i_have_no_idea_how_to_do_this_without_an_ugly_symbol unless const_defined?(:NIL_OBJECT)
|
5
|
+
|
6
|
+
def self.included(klass)
|
7
|
+
klass.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
def cache(key, time = DEFAULT_TIME, &block)
|
11
|
+
ModelCache::cache([self.cache_key, key], time, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.cache(ckey, time = DEFAULT_TIME, &block)
|
15
|
+
cache_hit = false
|
16
|
+
if CACHE.class.name == 'Memcached'
|
17
|
+
begin
|
18
|
+
result = CACHE.get(ckey.hash.to_s)
|
19
|
+
cache_hit = true
|
20
|
+
rescue Memcached::NotFound => e
|
21
|
+
end
|
22
|
+
elsif CACHE.class.name == 'MemCache'
|
23
|
+
result = CACHE.get(ckey.hash.to_s)
|
24
|
+
if result
|
25
|
+
cache_hit = true
|
26
|
+
end
|
27
|
+
if result == NIL_OBJECT
|
28
|
+
result = nil
|
29
|
+
end
|
30
|
+
else
|
31
|
+
raise "CACHE object not configured #{CACHE.inspect}!"
|
32
|
+
end
|
33
|
+
unless cache_hit
|
34
|
+
result = block.call
|
35
|
+
if CACHE.class.name == 'MemCache'
|
36
|
+
if result
|
37
|
+
CACHE.set(ckey.hash.to_s, result, time)
|
38
|
+
else
|
39
|
+
CACHE.set(ckey.hash.to_s, NIL_OBJECT, time)
|
40
|
+
end
|
41
|
+
elsif CACHE.class.name == 'Memcached'
|
42
|
+
CACHE.set(ckey.hash.to_s, result, time)
|
43
|
+
else
|
44
|
+
raise "CACHE object not configured #{CACHE.inspect}!"
|
45
|
+
end
|
46
|
+
result
|
47
|
+
end
|
48
|
+
result
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
module ClassMethods
|
53
|
+
def cache_method(*args)
|
54
|
+
opts = args.extract_options!
|
55
|
+
args.each do |sym|
|
56
|
+
cache_method_for_time(sym, (opts[:time] || DEFAULT_TIME))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def cache_method_for_time(sym, time)
|
61
|
+
alias_method :"__noncached_#{sym}", sym
|
62
|
+
define_method sym do |*args|
|
63
|
+
ckey = [self.cache_key, sym, *args]
|
64
|
+
ModelCache.cache(ckey, time) do
|
65
|
+
self.send(:"__noncached_#{sym}", *args)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
define_method :"__is_cached_#{sym}?" do |*args|
|
69
|
+
ckey = [self.cache_key, sym, *args]
|
70
|
+
if CACHE.class.name == 'Memcached'
|
71
|
+
begin
|
72
|
+
result = CACHE.get(ckey.hash.to_s)
|
73
|
+
cache_hit = true
|
74
|
+
rescue Memcached::NotFound => e
|
75
|
+
end
|
76
|
+
elsif CACHE.class.name == 'MemCache'
|
77
|
+
result = CACHE.get(ckey.hash.to_s)
|
78
|
+
if result
|
79
|
+
cache_hit = true
|
80
|
+
end
|
81
|
+
if result == NIL_OBJECT
|
82
|
+
result = nil
|
83
|
+
end
|
84
|
+
else
|
85
|
+
raise "CACHE object not configured #{CACHE.inspect}!"
|
86
|
+
end
|
87
|
+
cache_hit
|
88
|
+
end
|
89
|
+
define_method :"__flush_#{sym}" do |*args|
|
90
|
+
ckey = [self.cache_key, sym, *args]
|
91
|
+
CACHE.delete(ckey)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
# include the library
|
99
|
+
ActiveRecord::Base.send(:include, ModelCache)
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 4
|
9
|
+
version: 0.2.4
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Frantisek Havluj
|
@@ -9,7 +14,7 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-06-11 00:00:00 +02:00
|
13
18
|
default_executable:
|
14
19
|
dependencies: []
|
15
20
|
|
@@ -23,7 +28,6 @@ extra_rdoc_files: []
|
|
23
28
|
|
24
29
|
files:
|
25
30
|
- lib/model-cache.rb
|
26
|
-
- lib/model_cache.rb
|
27
31
|
- init.rb
|
28
32
|
- README.textile
|
29
33
|
- MIT-LICENSE
|
@@ -41,18 +45,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
41
45
|
requirements:
|
42
46
|
- - ">="
|
43
47
|
- !ruby/object:Gem::Version
|
48
|
+
segments:
|
49
|
+
- 0
|
44
50
|
version: "0"
|
45
|
-
version:
|
46
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
52
|
requirements:
|
48
53
|
- - ">="
|
49
54
|
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
50
57
|
version: "0"
|
51
|
-
version:
|
52
58
|
requirements: []
|
53
59
|
|
54
60
|
rubyforge_project:
|
55
|
-
rubygems_version: 1.3.
|
61
|
+
rubygems_version: 1.3.6
|
56
62
|
signing_key:
|
57
63
|
specification_version: 3
|
58
64
|
summary: Rails plugin for caching in models.
|
data/lib/model_cache.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
module ModelCache
|
2
|
-
|
3
|
-
DEFAULT_TIME = 12.hours unless const_defined?(:DEFAULT_TIME)
|
4
|
-
NIL_OBJECT = :__i_have_no_idea_how_to_do_this_without_an_ugly_symbol unless const_defined?(:NIL_OBJECT)
|
5
|
-
|
6
|
-
def self.included(klass)
|
7
|
-
klass.extend ClassMethods
|
8
|
-
end
|
9
|
-
|
10
|
-
def cache(key, time = DEFAULT_TIME, &block)
|
11
|
-
ModelCache::cache([self.cache_key, key], time, &block)
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.cache(ckey, time = DEFAULT_TIME, &block)
|
15
|
-
cache_hit = false
|
16
|
-
if Rails.configuration.action_controller.perform_caching
|
17
|
-
if CACHE.class.name == 'Memcached'
|
18
|
-
begin
|
19
|
-
result = CACHE.get(ckey.hash.to_s)
|
20
|
-
cache_hit = true
|
21
|
-
rescue Memcached::NotFound => e
|
22
|
-
end
|
23
|
-
elsif CACHE.class.name == 'MemCache'
|
24
|
-
result = CACHE.get(ckey.hash.to_s)
|
25
|
-
if result
|
26
|
-
cache_hit = true
|
27
|
-
end
|
28
|
-
if result == NIL_OBJECT
|
29
|
-
result = nil
|
30
|
-
end
|
31
|
-
else
|
32
|
-
raise "CACHE object not configured #{CACHE.inspect}!"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
unless cache_hit
|
36
|
-
result = block.call
|
37
|
-
if Rails.configuration.action_controller.perform_caching
|
38
|
-
if CACHE.class.name == 'MemCache'
|
39
|
-
if result
|
40
|
-
CACHE.set(ckey.hash.to_s, result, time)
|
41
|
-
else
|
42
|
-
CACHE.set(ckey.hash.to_s, NIL_OBJECT, time)
|
43
|
-
end
|
44
|
-
elsif CACHE.class.name == 'Memcached'
|
45
|
-
CACHE.set(ckey.hash.to_s, result, time)
|
46
|
-
else
|
47
|
-
raise "CACHE object not configured #{CACHE.inspect}!"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
result
|
51
|
-
end
|
52
|
-
result
|
53
|
-
end
|
54
|
-
|
55
|
-
|
56
|
-
module ClassMethods
|
57
|
-
def cache_method(*args)
|
58
|
-
opts = args.extract_options!
|
59
|
-
args.each do |sym|
|
60
|
-
cache_method_for_time(sym, (opts[:time] || DEFAULT_TIME))
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def cache_method_for_time(sym, time)
|
65
|
-
alias_method :"__noncached_#{sym}", sym
|
66
|
-
define_method sym do |*args|
|
67
|
-
ckey = [self.cache_key, sym, *args]
|
68
|
-
ModelCache.cache(ckey, time) do
|
69
|
-
self.send(:"__noncached_#{sym}", *args)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
define_method :"__is_cached_#{sym}?" do |*args|
|
73
|
-
ckey = [self.cache_key, sym, *args]
|
74
|
-
!!( Rails.configuration.action_controller.perform_caching && CACHE.get(ckey) )
|
75
|
-
end
|
76
|
-
define_method :"__flush_#{sym}" do |*args|
|
77
|
-
ckey = [self.cache_key, sym, *args]
|
78
|
-
CACHE.delete(ckey)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|