This repository has been archived by the owner on Jun 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREADME
88 lines (68 loc) · 2.61 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
MethodCache
===========
A Rails (v2.1.0) plugin that tries to solve the problem of having to call a method repetitively. That method is expensive to be recalculated everytime.
The solution is to cache the method results and we've always done that by caching a method's result in instance variables.
method_cache plugin frees you from the trouble of worrying about writing caching code and introduces a couple of utility methods
in ActiveRecord objects that make your life easier.
In its current form, the plugin provides the following extensions to ActiveRecord Objects
caches_method :method_name
caches_class_method :method_name
and for expiring the cache
instance.expire_method :method_name
Class.expire_instance_method :method_name, id
Class.expire_class_method :method_name
Introducing the plugin blog entry
=================================
http://humanzz.spaces.live.com/blog/cns!82322F9506CB0449!713.entry
Setup
=====
ruby script/plugin install git://github.com/humanzz/method_cache.git
Examples
========
For caching instance methods
class User < ActiveRecord::Base
def costy_method
#some real heavy calculation that takes alot of resources
#to complete
#this method is called regularly and its results rarely change
end
caches_method :costy_method
end
class User < ActiveRecord::Base
def costy_method
#some real heavy calculation that takes alot of resources
#to complete
#this method is called regularly and its results rarely change
end
caches_method :costy_method, :for => 20.minutes
end
class User < ActiveRecord::Base
def costy_method
#some real heavy calculation that takes alot of resources
#to complete
#this method is called regularly and its results rarely change
end
caches_method :costy_method, :until => :midnight
end
:until also accepts Time and date objects but I'm considering removing until or have it
support things like :midnight because otherwise it would cause errors after the specified
date or time have passed.
To expire cached instance methods
@user.expire_method :costy_method
User.expire_instance_method :costy_method, user_id
For caching class methods
class User < ActiveRecord::Base
def self.costy_class_method
#some real heavy calculation that takes alot of resources
#to complete
#this method is called regularly and its results rarely change
end
caches_class_method :costy_class_method
end
caches_class_method also supports the :for and :until options
To expire class methods
User.expire_class_method :costy_class_method
Test
====
rake test:plugins DB=sqlite3
Copyright (c) 2008 humanzz (Ahmed Sobhi), released under the MIT license