model-cache 0.2.0 → 0.2.2
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.textile +27 -4
- data/lib/model_cache.rb +4 -5
- metadata +3 -3
data/README.textile
CHANGED
@@ -5,6 +5,8 @@ ModelCache is a simple caching plugin for Rails, using @memcached@. It provides
|
|
5
5
|
* cache your instance methods, optionally with a time-to-live setting
|
6
6
|
* cache some generic code (e.g. in your class methods)
|
7
7
|
|
8
|
+
Both memcached client gems, @memcache-client@ and @memcached@ are supported. You are just expected to create an instance of the client and store it in the @CACHE@ constant.
|
9
|
+
|
8
10
|
h1. Installation
|
9
11
|
|
10
12
|
p. As a gem:
|
@@ -21,11 +23,16 @@ bc. script/plugin install http://github.com/moskyt/model_cache.git
|
|
21
23
|
|
22
24
|
h1. Example
|
23
25
|
|
24
|
-
p. @
|
26
|
+
p. Create an initializer, for example @config/initializers/memcached.rb@, containing this code (if you are using @memcache-client@):
|
25
27
|
|
26
28
|
bc. require 'memcache'
|
27
29
|
CACHE = MemCache.new('127.0.0.1')
|
28
30
|
|
31
|
+
p. or, if using @memcached@ gem:
|
32
|
+
|
33
|
+
bc. require 'memcached'
|
34
|
+
CACHE = Memcached.new('127.0.0.1')
|
35
|
+
|
29
36
|
p. Your model:
|
30
37
|
|
31
38
|
bc.. class Stuff < ActiveRecord::Base
|
@@ -44,7 +51,7 @@ bc.. class Stuff < ActiveRecord::Base
|
|
44
51
|
...
|
45
52
|
end
|
46
53
|
|
47
|
-
|
54
|
+
cache_method :third_expensive_method, :time => 1.hour
|
48
55
|
|
49
56
|
def partially_expensive_method
|
50
57
|
...
|
@@ -61,6 +68,22 @@ bc.. class Stuff < ActiveRecord::Base
|
|
61
68
|
|
62
69
|
end
|
63
70
|
|
64
|
-
p.
|
71
|
+
p. If you want to cache somewhere else than in _ActiveRecord::Base_ classes, you need to include the module explicitly:
|
72
|
+
|
73
|
+
bc.. class Another
|
74
|
+
include ModelCache
|
75
|
+
|
76
|
+
def expensive_method
|
77
|
+
...
|
78
|
+
end
|
79
|
+
|
80
|
+
cache_method :expensive_method
|
81
|
+
end
|
82
|
+
|
83
|
+
p. @model-cache@ defines three methods for a cached method:
|
84
|
+
|
85
|
+
* @__uncached_@_method_name_ -- the original method without the caching wrapper
|
86
|
+
* @__is_cached_@_method_name_@?@ -- returns true if this method is cached (with respective arguments)
|
87
|
+
* @__flush_@_method_name_ -- removes the cache entry explicitly from the cache
|
65
88
|
|
66
|
-
Copyright (c) 2010 Frantisek Havluj, released under the MIT license
|
89
|
+
p. Copyright (c) 2010 Frantisek Havluj, released under the MIT license
|
data/lib/model_cache.rb
CHANGED
@@ -26,9 +26,7 @@ module ModelCache
|
|
26
26
|
cache_hit = true
|
27
27
|
end
|
28
28
|
if result == NIL_OBJECT
|
29
|
-
nil
|
30
|
-
else
|
31
|
-
result
|
29
|
+
result = nil
|
32
30
|
end
|
33
31
|
else
|
34
32
|
raise "CACHE object not configured #{CACHE.inspect}!"
|
@@ -57,8 +55,9 @@ module ModelCache
|
|
57
55
|
|
58
56
|
module ClassMethods
|
59
57
|
def cache_method(*args)
|
58
|
+
opts = args.extract_options!
|
60
59
|
args.each do |sym|
|
61
|
-
cache_method_for_time(sym, DEFAULT_TIME)
|
60
|
+
cache_method_for_time(sym, (opts[:time] || DEFAULT_TIME))
|
62
61
|
end
|
63
62
|
end
|
64
63
|
|
@@ -74,7 +73,7 @@ module ModelCache
|
|
74
73
|
ckey = [self.cache_key, sym, *args]
|
75
74
|
!!( Rails.configuration.action_controller.perform_caching && CACHE.get(ckey) )
|
76
75
|
end
|
77
|
-
define_method :"
|
76
|
+
define_method :"__flush_#{sym}" do |*args|
|
78
77
|
ckey = [self.cache_key, sym, *args]
|
79
78
|
CACHE.delete(ckey)
|
80
79
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frantisek Havluj
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-02-03 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -29,7 +29,7 @@ files:
|
|
29
29
|
- MIT-LICENSE
|
30
30
|
- CHANGELOG
|
31
31
|
has_rdoc: true
|
32
|
-
homepage: http://github.com/moskyt/
|
32
|
+
homepage: http://github.com/moskyt/model_cache
|
33
33
|
licenses: []
|
34
34
|
|
35
35
|
post_install_message:
|