interlock 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/CHANGELOG +2 -0
  2. data/LICENSE +184 -0
  3. data/Manifest +89 -0
  4. data/README +168 -0
  5. data/TODO +3 -0
  6. data/examples/memcached.yml +10 -0
  7. data/init.rb +2 -0
  8. data/interlock.gemspec +52 -0
  9. data/lib/interlock/action_controller.rb +123 -0
  10. data/lib/interlock/action_view.rb +34 -0
  11. data/lib/interlock/active_record.rb +29 -0
  12. data/lib/interlock/config.rb +78 -0
  13. data/lib/interlock/core_extensions.rb +64 -0
  14. data/lib/interlock/interlock.rb +112 -0
  15. data/lib/interlock/memcached.rb +30 -0
  16. data/lib/interlock.rb +25 -0
  17. data/test/integration/app/README +203 -0
  18. data/test/integration/app/Rakefile +10 -0
  19. data/test/integration/app/app/controllers/application.rb +10 -0
  20. data/test/integration/app/app/controllers/eval_controller.rb +7 -0
  21. data/test/integration/app/app/controllers/items_controller.rb +32 -0
  22. data/test/integration/app/app/helpers/application_helper.rb +3 -0
  23. data/test/integration/app/app/helpers/eval_helper.rb +2 -0
  24. data/test/integration/app/app/helpers/items_helper.rb +2 -0
  25. data/test/integration/app/app/models/item.rb +2 -0
  26. data/test/integration/app/app/views/items/list.html.erb +10 -0
  27. data/test/integration/app/app/views/items/recent.html.erb +14 -0
  28. data/test/integration/app/app/views/items/show.html.erb +8 -0
  29. data/test/integration/app/app/views/shared/_related.html.erb +6 -0
  30. data/test/integration/app/config/boot.rb +108 -0
  31. data/test/integration/app/config/database.yml +24 -0
  32. data/test/integration/app/config/environment.rb +25 -0
  33. data/test/integration/app/config/environments/development.rb +8 -0
  34. data/test/integration/app/config/environments/production.rb +18 -0
  35. data/test/integration/app/config/environments/test.rb +22 -0
  36. data/test/integration/app/config/initializers/inflections.rb +10 -0
  37. data/test/integration/app/config/initializers/mime_types.rb +5 -0
  38. data/test/integration/app/config/memcached.yml +9 -0
  39. data/test/integration/app/config/routes.rb +35 -0
  40. data/test/integration/app/coverage/cache-43041 +0 -0
  41. data/test/integration/app/coverage/index.html +414 -0
  42. data/test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-action_controller_rb.html +733 -0
  43. data/test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-action_view_rb.html +644 -0
  44. data/test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-active_record_rb.html +639 -0
  45. data/test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-config_rb.html +688 -0
  46. data/test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-core_extensions_rb.html +674 -0
  47. data/test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-interlock_rb.html +722 -0
  48. data/test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-memcached_rb.html +640 -0
  49. data/test/integration/app/coverage/vendor-plugins-interlock-lib-interlock_rb.html +635 -0
  50. data/test/integration/app/db/migrate/001_create_items.rb +13 -0
  51. data/test/integration/app/db/schema.rb +21 -0
  52. data/test/integration/app/doc/README_FOR_APP +2 -0
  53. data/test/integration/app/public/404.html +30 -0
  54. data/test/integration/app/public/422.html +30 -0
  55. data/test/integration/app/public/500.html +30 -0
  56. data/test/integration/app/public/dispatch.cgi +10 -0
  57. data/test/integration/app/public/dispatch.fcgi +24 -0
  58. data/test/integration/app/public/dispatch.rb +10 -0
  59. data/test/integration/app/public/favicon.ico +0 -0
  60. data/test/integration/app/public/images/rails.png +0 -0
  61. data/test/integration/app/public/index.html +277 -0
  62. data/test/integration/app/public/javascripts/application.js +2 -0
  63. data/test/integration/app/public/javascripts/controls.js +963 -0
  64. data/test/integration/app/public/javascripts/dragdrop.js +972 -0
  65. data/test/integration/app/public/javascripts/effects.js +1120 -0
  66. data/test/integration/app/public/javascripts/prototype.js +4200 -0
  67. data/test/integration/app/public/robots.txt +5 -0
  68. data/test/integration/app/script/about +3 -0
  69. data/test/integration/app/script/console +3 -0
  70. data/test/integration/app/script/destroy +3 -0
  71. data/test/integration/app/script/generate +3 -0
  72. data/test/integration/app/script/performance/benchmarker +3 -0
  73. data/test/integration/app/script/performance/profiler +3 -0
  74. data/test/integration/app/script/performance/request +3 -0
  75. data/test/integration/app/script/plugin +3 -0
  76. data/test/integration/app/script/process/inspector +3 -0
  77. data/test/integration/app/script/process/reaper +3 -0
  78. data/test/integration/app/script/process/spawner +3 -0
  79. data/test/integration/app/script/runner +3 -0
  80. data/test/integration/app/script/server +3 -0
  81. data/test/integration/app/test/fixtures/items.yml +13 -0
  82. data/test/integration/app/test/functional/eval_controller_test.rb +10 -0
  83. data/test/integration/app/test/functional/items_controller_test.rb +10 -0
  84. data/test/integration/app/test/test_helper.rb +9 -0
  85. data/test/integration/app/test/unit/item_test.rb +8 -0
  86. data/test/integration/server_test.rb +161 -0
  87. data/test/setup.rb +15 -0
  88. data/test/teardown.rb +4 -0
  89. data/test/test_helper.rb +13 -0
  90. data/test/unit/memcached_test.rb +45 -0
  91. data.tar.gz.sig +0 -0
  92. metadata +172 -0
  93. metadata.gz.sig +1 -0
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+
2
+ v1.0. First release.
data/LICENSE ADDED
@@ -0,0 +1,184 @@
1
+ Academic Free License (AFL) v. 3.0
2
+
3
+ This Academic Free License (the "License") applies to any original work
4
+ of authorship (the "Original Work") whose owner (the "Licensor") has
5
+ placed the following licensing notice adjacent to the copyright notice
6
+ for the Original Work:
7
+
8
+ Licensed under the Academic Free License version 3.0
9
+
10
+ 1) Grant of Copyright License. Licensor grants You a worldwide,
11
+ royalty-free, non-exclusive, sublicensable license, for the duration of
12
+ the copyright, to do the following:
13
+
14
+ a) to reproduce the Original Work in copies, either alone or as part of
15
+ a collective work;
16
+
17
+ b) to translate, adapt, alter, transform, modify, or arrange the
18
+ Original Work, thereby creating derivative works ("Derivative Works")
19
+ based upon the Original Work;
20
+
21
+ c) to distribute or communicate copies of the Original Work and
22
+ Derivative Works to the public, under any license of your choice that
23
+ does not contradict the terms and conditions, including Licensor's
24
+ reserved rights and remedies, in this Academic Free License;
25
+
26
+ d) to perform the Original Work publicly; and
27
+
28
+ e) to display the Original Work publicly.
29
+
30
+ 2) Grant of Patent License. Licensor grants You a worldwide,
31
+ royalty-free, non-exclusive, sublicensable license, under patent claims
32
+ owned or controlled by the Licensor that are embodied in the Original
33
+ Work as furnished by the Licensor, for the duration of the patents, to
34
+ make, use, sell, offer for sale, have made, and import the Original Work
35
+ and Derivative Works.
36
+
37
+ 3) Grant of Source Code License. The term "Source Code" means the
38
+ preferred form of the Original Work for making modifications to it and
39
+ all available documentation describing how to modify the Original Work.
40
+ Licensor agrees to provide a machine-readable copy of the Source Code of
41
+ the Original Work along with each copy of the Original Work that
42
+ Licensor distributes. Licensor reserves the right to satisfy this
43
+ obligation by placing a machine-readable copy of the Source Code in an
44
+ information repository reasonably calculated to permit inexpensive and
45
+ convenient access by You for as long as Licensor continues to distribute
46
+ the Original Work.
47
+
48
+ 4) Exclusions From License Grant. Neither the names of Licensor, nor the
49
+ names of any contributors to the Original Work, nor any of their
50
+ trademarks or service marks, may be used to endorse or promote products
51
+ derived from this Original Work without express prior permission of the
52
+ Licensor. Except as expressly stated herein, nothing in this License
53
+ grants any license to Licensor's trademarks, copyrights, patents, trade
54
+ secrets or any other intellectual property. No patent license is granted
55
+ to make, use, sell, offer for sale, have made, or import embodiments of
56
+ any patent claims other than the licensed claims defined in Section 2.
57
+ No license is granted to the trademarks of Licensor even if such marks
58
+ are included in the Original Work. Nothing in this License shall be
59
+ interpreted to prohibit Licensor from licensing under terms different
60
+ from this License any Original Work that Licensor otherwise would have a
61
+ right to license.
62
+
63
+ 5) External Deployment. The term "External Deployment" means the use,
64
+ distribution, or communication of the Original Work or Derivative Works
65
+ in any way such that the Original Work or Derivative Works may be used
66
+ by anyone other than You, whether those works are distributed or
67
+ communicated to those persons or made available as an application
68
+ intended for use over a network. As an express condition for the grants
69
+ of license hereunder, You must treat any External Deployment by You of
70
+ the Original Work or a Derivative Work as a distribution under section
71
+ 1(c).
72
+
73
+ 6) Attribution Rights. You must retain, in the Source Code of any
74
+ Derivative Works that You create, all copyright, patent, or trademark
75
+ notices from the Source Code of the Original Work, as well as any
76
+ notices of licensing and any descriptive text identified therein as an
77
+ "Attribution Notice." You must cause the Source Code for any Derivative
78
+ Works that You create to carry a prominent Attribution Notice reasonably
79
+ calculated to inform recipients that You have modified the Original
80
+ Work.
81
+
82
+ 7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants
83
+ that the copyright in and to the Original Work and the patent rights
84
+ granted herein by Licensor are owned by the Licensor or are sublicensed
85
+ to You under the terms of this License with the permission of the
86
+ contributor(s) of those copyrights and patent rights. Except as
87
+ expressly stated in the immediately preceding sentence, the Original
88
+ Work is provided under this License on an "AS IS" BASIS and WITHOUT
89
+ WARRANTY, either express or implied, including, without limitation, the
90
+ warranties of non-infringement, merchantability or fitness for a
91
+ particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL
92
+ WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential
93
+ part of this License. No license to the Original Work is granted by this
94
+ License except under this disclaimer.
95
+
96
+ 8) Limitation of Liability. Under no circumstances and under no legal
97
+ theory, whether in tort (including negligence), contract, or otherwise,
98
+ shall the Licensor be liable to anyone for any indirect, special,
99
+ incidental, or consequential damages of any character arising as a
100
+ result of this License or the use of the Original Work including,
101
+ without limitation, damages for loss of goodwill, work stoppage,
102
+ computer failure or malfunction, or any and all other commercial damages
103
+ or losses. This limitation of liability shall not apply to the extent
104
+ applicable law prohibits such limitation.
105
+
106
+ 9) Acceptance and Termination. If, at any time, You expressly assented
107
+ to this License, that assent indicates your clear and irrevocable
108
+ acceptance of this License and all of its terms and conditions. If You
109
+ distribute or communicate copies of the Original Work or a Derivative
110
+ Work, You must make a reasonable effort under the circumstances to
111
+ obtain the express assent of recipients to the terms of this License.
112
+ This License conditions your rights to undertake the activities listed
113
+ in Section 1, including your right to create Derivative Works based upon
114
+ the Original Work, and doing so without honoring these terms and
115
+ conditions is prohibited by copyright law and international treaty.
116
+ Nothing in this License is intended to affect copyright exceptions and
117
+ limitations (including "fair use" or "fair dealing"). This License shall
118
+ terminate immediately and You may no longer exercise any of the rights
119
+ granted to You by this License upon your failure to honor the conditions
120
+ in Section 1(c).
121
+
122
+ 10) Termination for Patent Action. This License shall terminate
123
+ automatically and You may no longer exercise any of the rights granted
124
+ to You by this License as of the date You commence an action, including
125
+ a cross-claim or counterclaim, against Licensor or any licensee alleging
126
+ that the Original Work infringes a patent. This termination provision
127
+ shall not apply for an action alleging patent infringement by
128
+ combinations of the Original Work with other software or hardware.
129
+
130
+ 11) Jurisdiction, Venue and Governing Law. Any action or suit relating
131
+ to this License may be brought only in the courts of a jurisdiction
132
+ wherein the Licensor resides or in which Licensor conducts its primary
133
+ business, and under the laws of that jurisdiction excluding its
134
+ conflict-of-law provisions. The application of the United Nations
135
+ Convention on Contracts for the International Sale of Goods is expressly
136
+ excluded. Any use of the Original Work outside the scope of this License
137
+ or after its termination shall be subject to the requirements and
138
+ penalties of copyright or patent law in the appropriate jurisdiction.
139
+ This section shall survive the termination of this License.
140
+
141
+ 12) Attorneys' Fees. In any action to enforce the terms of this License
142
+ or seeking damages relating thereto, the prevailing party shall be
143
+ entitled to recover its costs and expenses, including, without
144
+ limitation, reasonable attorneys' fees and costs incurred in connection
145
+ with such action, including any appeal of such action. This section
146
+ shall survive the termination of this License.
147
+
148
+ 13) Miscellaneous. If any provision of this License is held to be
149
+ unenforceable, such provision shall be reformed only to the extent
150
+ necessary to make it enforceable.
151
+
152
+ 14) Definition of "You" in This License. "You" throughout this License,
153
+ whether in upper or lower case, means an individual or a legal entity
154
+ exercising rights under, and complying with all of the terms of, this
155
+ License. For legal entities, "You" includes any entity that controls, is
156
+ controlled by, or is under common control with you. For purposes of this
157
+ definition, "control" means (i) the power, direct or indirect, to cause
158
+ the direction or management of such entity, whether by contract or
159
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
160
+ outstanding shares, or (iii) beneficial ownership of such entity.
161
+
162
+ 15) Right to Use. You may use the Original Work in all ways not
163
+ otherwise restricted or conditioned by this License or by law, and
164
+ Licensor promises not to interfere with or be responsible for such uses
165
+ by You.
166
+
167
+ 16) Modification of This License. This License is Copyright (c) 2005
168
+ Lawrence Rosen. Permission is granted to copy, distribute, or
169
+ communicate this License without modification. Nothing in this License
170
+ permits You to modify this License as applied to the Original Work or to
171
+ Derivative Works. However, You may modify the text of this License and
172
+ copy, distribute or communicate your modified version (the "Modified
173
+ License") and apply it to other original works of authorship subject to
174
+ the following conditions: (i) You may not indicate in any way that your
175
+ Modified License is the "Academic Free License" or "AFL" and you may not
176
+ use those names in the name of your Modified License; (ii) You must
177
+ replace the notice specified in the first paragraph above with the
178
+ notice "Licensed under <insert your license name here>" or with a notice
179
+ of your own that is not confusingly similar to the notice in this
180
+ License; and (iii) You may not claim that your original works are open
181
+ source software unless your Modified License has been approved by Open
182
+ Source Initiative (OSI) and You comply with its license review and
183
+ certification process.
184
+
data/Manifest ADDED
@@ -0,0 +1,89 @@
1
+ CHANGELOG
2
+ examples/memcached.yml
3
+ init.rb
4
+ lib/interlock/action_controller.rb
5
+ lib/interlock/action_view.rb
6
+ lib/interlock/active_record.rb
7
+ lib/interlock/config.rb
8
+ lib/interlock/core_extensions.rb
9
+ lib/interlock/interlock.rb
10
+ lib/interlock/memcached.rb
11
+ lib/interlock.rb
12
+ LICENSE
13
+ Manifest
14
+ README
15
+ test/integration/app/app/controllers/application.rb
16
+ test/integration/app/app/controllers/eval_controller.rb
17
+ test/integration/app/app/controllers/items_controller.rb
18
+ test/integration/app/app/helpers/application_helper.rb
19
+ test/integration/app/app/helpers/eval_helper.rb
20
+ test/integration/app/app/helpers/items_helper.rb
21
+ test/integration/app/app/models/item.rb
22
+ test/integration/app/app/views/items/list.html.erb
23
+ test/integration/app/app/views/items/recent.html.erb
24
+ test/integration/app/app/views/items/show.html.erb
25
+ test/integration/app/app/views/shared/_related.html.erb
26
+ test/integration/app/config/boot.rb
27
+ test/integration/app/config/database.yml
28
+ test/integration/app/config/environment.rb
29
+ test/integration/app/config/environments/development.rb
30
+ test/integration/app/config/environments/production.rb
31
+ test/integration/app/config/environments/test.rb
32
+ test/integration/app/config/initializers/inflections.rb
33
+ test/integration/app/config/initializers/mime_types.rb
34
+ test/integration/app/config/memcached.yml
35
+ test/integration/app/config/routes.rb
36
+ test/integration/app/coverage/cache-43041
37
+ test/integration/app/coverage/index.html
38
+ test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-action_controller_rb.html
39
+ test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-action_view_rb.html
40
+ test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-active_record_rb.html
41
+ test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-config_rb.html
42
+ test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-core_extensions_rb.html
43
+ test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-interlock_rb.html
44
+ test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-memcached_rb.html
45
+ test/integration/app/coverage/vendor-plugins-interlock-lib-interlock_rb.html
46
+ test/integration/app/db/migrate/001_create_items.rb
47
+ test/integration/app/db/schema.rb
48
+ test/integration/app/doc/README_FOR_APP
49
+ test/integration/app/public/404.html
50
+ test/integration/app/public/422.html
51
+ test/integration/app/public/500.html
52
+ test/integration/app/public/dispatch.cgi
53
+ test/integration/app/public/dispatch.fcgi
54
+ test/integration/app/public/dispatch.rb
55
+ test/integration/app/public/favicon.ico
56
+ test/integration/app/public/images/rails.png
57
+ test/integration/app/public/index.html
58
+ test/integration/app/public/javascripts/application.js
59
+ test/integration/app/public/javascripts/controls.js
60
+ test/integration/app/public/javascripts/dragdrop.js
61
+ test/integration/app/public/javascripts/effects.js
62
+ test/integration/app/public/javascripts/prototype.js
63
+ test/integration/app/public/robots.txt
64
+ test/integration/app/Rakefile
65
+ test/integration/app/README
66
+ test/integration/app/script/about
67
+ test/integration/app/script/console
68
+ test/integration/app/script/destroy
69
+ test/integration/app/script/generate
70
+ test/integration/app/script/performance/benchmarker
71
+ test/integration/app/script/performance/profiler
72
+ test/integration/app/script/performance/request
73
+ test/integration/app/script/plugin
74
+ test/integration/app/script/process/inspector
75
+ test/integration/app/script/process/reaper
76
+ test/integration/app/script/process/spawner
77
+ test/integration/app/script/runner
78
+ test/integration/app/script/server
79
+ test/integration/app/test/fixtures/items.yml
80
+ test/integration/app/test/functional/eval_controller_test.rb
81
+ test/integration/app/test/functional/items_controller_test.rb
82
+ test/integration/app/test/test_helper.rb
83
+ test/integration/app/test/unit/item_test.rb
84
+ test/integration/server_test.rb
85
+ test/setup.rb
86
+ test/teardown.rb
87
+ test/test_helper.rb
88
+ test/unit/memcached_test.rb
89
+ TODO
data/README ADDED
@@ -0,0 +1,168 @@
1
+
2
+ Interlock
3
+
4
+ An optimal-efficiency caching plugin for Rails.
5
+
6
+ == License
7
+
8
+ Copyright 2007 Cloudburst, LLC. Licensed under the AFL 3; see the included LICENSE file. Portions copyright 2006 Chris Wanstrath and used with permission.
9
+
10
+ The public certificate for the gem is at http://rubyforge.org/frs/download.php/25331/evan_weaver-original-public_cert.pem.
11
+
12
+ == Requirements
13
+
14
+ * Memcached (http://www.danga.com/memcached)
15
+ * memcache-client gem
16
+
17
+ == What it does
18
+
19
+ Interlock makes your view fragments and associated controller blocks march along together. If a fragment is fresh, the controller behavior won't run. This eliminates duplicate effort from your request cycle. Your controller blocks run so infrequently that you can use regular ActiveRecord finders and not worry about object caching at all.
20
+
21
+ Interlock automatically tracks invalidation dependencies based on the model lifecyle, and supports arbitrary levels of scoping per-block.
22
+
23
+ == Installation
24
+
25
+ First, compile and install memcached itself. Get a memcached server running.
26
+
27
+ You also need the <tt>memcache-client</tt> gem:
28
+ sudo gem install memcache-client
29
+
30
+ Then, install the plugin:
31
+ script/plugin install -x svn://rubyforge.org/var/svn/fauna/interlock/trunk
32
+
33
+ Lastly, configure your Rails app for memcached by creating a <tt>config/memcached.yml</tt> file. The format is compatible with Cache_fu:
34
+
35
+ defaults:
36
+ namespace: myapp
37
+ sessions: false
38
+ development:
39
+ servers:
40
+ - localhost:11211 # Default port
41
+ production:
42
+ servers
43
+ - 10.12.128.1
44
+ - 10.12.128.2
45
+
46
+ Now you're ready to go.
47
+
48
+ == Usage
49
+
50
+ Interlock provides two similar caching methods: <tt>behavior_cache</tt> for controllers and <tt>view_cache</tt> for views. The both accept an optional list or hash of model dependencies, and an optional <tt>:tag</tt> keypair. <tt>view_cache</tt> also accepts a <tt>ttl</tt>.
51
+
52
+ The simplest usage doesn't require any parameters. In the controller:
53
+
54
+ class ItemsController < ActionController::Base
55
+
56
+ def slow_action
57
+ behavior_cache do
58
+ @items = Item.find(:all, :conditions => "be slow")
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+ Now, in the view, wrap the largest section of ERB you can find that uses data from <tt>@items</tt> (but not from any other instance variables) in a <tt>view_cache</tt> block.
65
+
66
+ <% @title = "My Sweet Items" %>
67
+
68
+ <% view_cache do %>
69
+ <% @items.each do |item| %>
70
+ <h1><%= item.name %></h1>
71
+ <% end %>
72
+ <% end %>
73
+
74
+ You have to do them both.
75
+
76
+ This automatically registers a caching dependency on Item for <tt>slow_action</tt>. The controller block won't run if the <tt>slow_action</tt> view fragment is fresh, and the view fragment will only get invalidated when an Item is changed.
77
+
78
+ You can use multiple instance variables in one block, of course. Just make sure the <tt>behavior_cache</tt> provides whatever the <tt>view_cache</tt> uses.
79
+
80
+ == Declaring dependencies
81
+
82
+ You can declare non-default invalidation dependencies by passing models to <tt>behavior_cache</tt> (you can also pass them to <tt>view_cache</tt>, but you should only do that if you are caching a fragment without an associated behavior block in the controller).
83
+
84
+ <b>No dependencies (cache never invalidates):</b>
85
+ behavior_cache nil do
86
+ end
87
+
88
+ <b>Invalidate on any Media change:</b>
89
+ behavior_cache Media do
90
+ end
91
+
92
+ <b>Invalidate on any Media or Item change:</b>
93
+ behavior_cache Media, Item do
94
+ end
95
+
96
+ <b>Invalidate on Item changes if the Item <tt>id</tt> matches the current <tt>params[:id]</tt> value:</b>
97
+ behavior_cache Item => :id do
98
+ end
99
+
100
+ You do not have to pass the same dependencies to <tt>behavior_cache</tt> and <tt>view_cache</tt> even for the same action. The set union of both dependency lists will be used.
101
+
102
+ == Narrowing scope and caching multiple blocks
103
+
104
+ Sometimes you need to cache multiple blocks in a controller, or otherwise get a more fine-grained scope. Interlock provides the <tt>:tag</tt> key for this purpose. <tt>:tag</tt> accepts either an array of symbols, which are mapped to <tt>params</tt> values, or an arbitrary object, which is converted to a string identifier. <b>Your corresponding behavior caches and view caches must have identical <tt>:tag</tt> values for the interlocking to take effect.</b>
105
+
106
+ Note that <tt>:tag</tt> can be used to scope caches. You can simultaneously cache different versions of the same block, differentiating based on params or other logic. This is great for caching per-user, for example:
107
+
108
+ def profile
109
+ @user = current_user
110
+ behavior_cache :tag => @user do
111
+ @items = Item.find(:all, :conditions => "be slow")
112
+ end
113
+ end
114
+
115
+ In the view, use the same <tt>:tag</tt> value (<tt>@user</tt>). Note that <tt>@user</tt> must be set outside of the behavior block in the controller, because its contents are used to decide whether to run the block in the first place.
116
+
117
+ This way each user will see only their own cache. Pretty neat.
118
+
119
+ == Broadening scope
120
+
121
+ Sometimes the default scope (<tt>controller</tt>, <tt>action</tt>, <tt>params[:id]</tt>) is too narrow. For example, you might share a partial across actions, and set up its data via a filter. By default, Interlock will cache a separate version of it for each action. To avoid this, you can use the <tt>:ignore</tt> key, which lets you list parts of the default scope to ignore:
122
+
123
+ before_filter :recent
124
+
125
+ private
126
+
127
+ def recent
128
+ behavior_cache :ignore => :action do
129
+ @recent = Item.find(:all, :limit => 5, :order => 'updated_at DESC')
130
+ end
131
+ end
132
+
133
+ Valid values for <tt>:ignore</tt> are <tt>:controller</tt>, <tt>:action</tt>, <tt>:id</tt>, and <tt>:all</tt>. You can pass an array of multiple values. <b>Just like with <tt>:tag</tt>, your corresponding behavior caches and view caches must have identical <tt>:ignore</tt> values.</b> Note that cache blocks with <tt>:ignore</tt> values still obey the regular invalidation rules.
134
+
135
+ A good way to get started is to just use the default scope. Then <tt>grep</tt> in the production log for <tt>interlock</tt> and see what keys are being set and read. If you see lots of different keys go by for data that you know is the same, then set some <tt>:ignore</tt> values.
136
+
137
+ == View-only caching
138
+
139
+ It's fine to use a <tt>view_cache</tt> block without a <tt>behavior_cache</tt> block. For example, to mimic regular fragment cache behavior, but take advantage of Memcached's <tt>:ttl</tt> support, call:
140
+
141
+ <% view_cache nil, :ignore => :all, :tag => 'sidebar', :ttl => 5.minutes %>
142
+ <h1>On the side</h1>
143
+ <% end %>
144
+
145
+ Remember that <tt>nil</tt> disables invalidation rules. This is a nice trick for keeping your caching strategy unified.
146
+
147
+ == Gotchas
148
+
149
+ You will not see any actual cache reuse in development mode unless you set <tt>config.action_controller.perform_caching = true</tt> in <tt>config/environments/development.rb</tt>.
150
+
151
+ <b>If you have custom <tt>render</tt> calls in the controller, they must be outside the <tt>behavior_cache</tt> blocks.</b> No exceptions. For example:
152
+
153
+ def profile
154
+ behavior_cache do
155
+ @items = Item.find(:all, :conditions => "be slow")
156
+ end
157
+ render :action => 'home'
158
+ end
159
+
160
+ You can write custom invalidation rules if you really want to, but try hard to avoid it; it has a significant cost in long-term maintainability.
161
+
162
+ Also, Interlock obeys the <tt>ENV['RAILS_ASSET_ID']</tt> setting, so if you need to blanket-invalidate all your caches, just change <tt>RAILS_ASSET_ID</tt> (for example, you could have it increment on every deploy).
163
+
164
+ == Reporting problems
165
+
166
+ * http://rubyforge.org/forum/forum.php?forum_id=18926
167
+
168
+ Patches and contributions are very welcome. Please note that contributors are required to assign copyright for their additions to Cloudburst, LLC.
data/TODO ADDED
@@ -0,0 +1,3 @@
1
+
2
+ * Figure out how to cache Builder blocks
3
+ * Figure out how to cache RJS
@@ -0,0 +1,10 @@
1
+ defaults:
2
+ namespace: myapp
3
+ sessions: false
4
+ development:
5
+ servers:
6
+ - localhost:11211
7
+ production:
8
+ servers
9
+ - 10.12.128.1
10
+ - 10.12.128.2
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+
2
+ require 'interlock'
data/interlock.gemspec ADDED
@@ -0,0 +1,52 @@
1
+
2
+ # Gem::Specification for Interlock-1.0
3
+ # Originally generated by Echoe
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{interlock}
7
+ s.version = "1.0"
8
+
9
+ s.specification_version = 2 if s.respond_to? :specification_version=
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = [""]
13
+ s.date = %q{2007-12-13}
14
+ s.description = %q{An optimal-efficiency caching plugin for Rails.}
15
+ s.email = %q{}
16
+ s.files = ["CHANGELOG", "examples/memcached.yml", "init.rb", "lib/interlock/action_controller.rb", "lib/interlock/action_view.rb", "lib/interlock/active_record.rb", "lib/interlock/config.rb", "lib/interlock/core_extensions.rb", "lib/interlock/interlock.rb", "lib/interlock/memcached.rb", "lib/interlock.rb", "LICENSE", "Manifest", "README", "test/integration/app/app/controllers/application.rb", "test/integration/app/app/controllers/eval_controller.rb", "test/integration/app/app/controllers/items_controller.rb", "test/integration/app/app/helpers/application_helper.rb", "test/integration/app/app/helpers/eval_helper.rb", "test/integration/app/app/helpers/items_helper.rb", "test/integration/app/app/models/item.rb", "test/integration/app/app/views/items/list.html.erb", "test/integration/app/app/views/items/recent.html.erb", "test/integration/app/app/views/items/show.html.erb", "test/integration/app/app/views/shared/_related.html.erb", "test/integration/app/config/boot.rb", "test/integration/app/config/database.yml", "test/integration/app/config/environment.rb", "test/integration/app/config/environments/development.rb", "test/integration/app/config/environments/production.rb", "test/integration/app/config/environments/test.rb", "test/integration/app/config/initializers/inflections.rb", "test/integration/app/config/initializers/mime_types.rb", "test/integration/app/config/memcached.yml", "test/integration/app/config/routes.rb", "test/integration/app/coverage/cache-43041", "test/integration/app/coverage/index.html", "test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-action_controller_rb.html", "test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-action_view_rb.html", "test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-active_record_rb.html", "test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-config_rb.html", "test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-core_extensions_rb.html", "test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-interlock_rb.html", "test/integration/app/coverage/vendor-plugins-interlock-lib-interlock-memcached_rb.html", "test/integration/app/coverage/vendor-plugins-interlock-lib-interlock_rb.html", "test/integration/app/db/migrate/001_create_items.rb", "test/integration/app/db/schema.rb", "test/integration/app/doc/README_FOR_APP", "test/integration/app/public/404.html", "test/integration/app/public/422.html", "test/integration/app/public/500.html", "test/integration/app/public/dispatch.cgi", "test/integration/app/public/dispatch.fcgi", "test/integration/app/public/dispatch.rb", "test/integration/app/public/favicon.ico", "test/integration/app/public/images/rails.png", "test/integration/app/public/index.html", "test/integration/app/public/javascripts/application.js", "test/integration/app/public/javascripts/controls.js", "test/integration/app/public/javascripts/dragdrop.js", "test/integration/app/public/javascripts/effects.js", "test/integration/app/public/javascripts/prototype.js", "test/integration/app/public/robots.txt", "test/integration/app/Rakefile", "test/integration/app/README", "test/integration/app/script/about", "test/integration/app/script/console", "test/integration/app/script/destroy", "test/integration/app/script/generate", "test/integration/app/script/performance/benchmarker", "test/integration/app/script/performance/profiler", "test/integration/app/script/performance/request", "test/integration/app/script/plugin", "test/integration/app/script/process/inspector", "test/integration/app/script/process/reaper", "test/integration/app/script/process/spawner", "test/integration/app/script/runner", "test/integration/app/script/server", "test/integration/app/test/fixtures/items.yml", "test/integration/app/test/functional/eval_controller_test.rb", "test/integration/app/test/functional/items_controller_test.rb", "test/integration/app/test/test_helper.rb", "test/integration/app/test/unit/item_test.rb", "test/integration/server_test.rb", "test/setup.rb", "test/teardown.rb", "test/test_helper.rb", "test/unit/memcached_test.rb", "TODO", "interlock.gemspec"]
17
+ s.has_rdoc = true
18
+ s.homepage = %q{http://blog.evanweaver.com/files/doc/fauna/interlock/}
19
+ s.require_paths = ["lib"]
20
+ s.rubyforge_project = %q{fauna}
21
+ s.rubygems_version = %q{0.9.5}
22
+ s.summary = %q{An optimal-efficiency caching plugin for Rails.}
23
+ s.test_files = ["test/integration/server_test.rb", "test/unit/memcached_test.rb"]
24
+
25
+ s.add_dependency(%q<memcache_client>, [">= 1.5.0"])
26
+ end
27
+
28
+
29
+ # # Original Rakefile source (requires the Echoe gem):
30
+ #
31
+ #
32
+ # require 'echoe'
33
+ #
34
+ # Echoe.new("interlock") do |p|
35
+ # p.project = "fauna"
36
+ # p.summary = "An optimal-efficiency caching plugin for Rails."
37
+ # p.url = "http://blog.evanweaver.com/files/doc/fauna/interlock/"
38
+ # p.docs_host = "blog.evanweaver.com:~/www/bax/public/files/doc/"
39
+ # p.dependencies = "memcache_client >=1.5.0"
40
+ # p.test_pattern = ["test/integration/*.rb", "test/unit/*.rb"]
41
+ # p.rdoc_pattern = ["README", "CHANGELOG", "TODO", "LICENSE", "lib/interlock/memcached.rb", "lib/interlock/interlock.rb", "lib/interlock/action_controller.rb", "lib/interlock/active_record.rb", "lib/interlock/action_view.rb", "lib/interlock/config.rb"]
42
+ # end
43
+ #
44
+ # desc "Run all the tests in production and development mode both"
45
+ # task "test_all" do
46
+ # STDERR.puts "#{'='*80}\nDevelopment mode\n#{'='*80}"
47
+ # system("rake test:multi_rails:all")
48
+ #
49
+ # ENV['PRODUCTION'] = '1'
50
+ # STDERR.puts "#{'='*80}\nProduction mode\n#{'='*80}"
51
+ # system("rake test:multi_rails:all")
52
+ # end
@@ -0,0 +1,123 @@
1
+
2
+ module ActionController #:nodoc:
3
+ class Base
4
+
5
+ #
6
+ # Build the fragment key from a particular context. This must be deterministic
7
+ # and stateful except for the tag. We can't scope the key to arbitrary params
8
+ # because the view doesn't have access to which are relevant and which are
9
+ # not.
10
+ #
11
+ # Note that the tag can be pretty much any object. Define #to_interlock_tag
12
+ # if you need custom tagging for some class. ActiveRecord::Base already
13
+ # has it defined appropriately.
14
+ #
15
+ # If you pass an Array of symbols as the tag, it will get value-mapped onto
16
+ # params and sorted. This makes granular scoping easier, although it doesn't
17
+ # sidestep the normal blanket invalidations.
18
+ #
19
+ def caching_key(ignore = nil, tag = nil)
20
+ ignore = Array(ignore)
21
+ ignore = Interlock::SCOPE_KEYS if ignore.include? :all
22
+
23
+ if (Interlock::SCOPE_KEYS - ignore).empty? and !tag
24
+ raise UsageError, "You must specify a :tag if you are ignoring the entire default scope."
25
+ end
26
+
27
+ if tag.is_a? Array and tag.all? {|x| x.is_a? Symbol}
28
+ tag = tag.sort_by do |key|
29
+ key.to_s
30
+ end.map do |key|
31
+ params[key].to_interlock_tag
32
+ end.join(";")
33
+ end
34
+
35
+ Interlock.caching_key(
36
+ ignore.include?(:controller) ? 'any' : controller_name,
37
+ ignore.include?(:action) ? 'any' : action_name,
38
+ ignore.include?(:id) ? 'all' : params[:id],
39
+ tag
40
+ )
41
+ end
42
+
43
+ #
44
+ # Mark a controller block for caching. Accepts a list of class dependencies for
45
+ # invalidation, as well as a :tag key for explicit fragment scoping.
46
+ #
47
+ def behavior_cache(*args)
48
+ conventional_class = begin; controller_name.classify.constantize; rescue NameError; end
49
+ options, dependencies = Interlock.extract_options_and_dependencies(args, conventional_class)
50
+
51
+ raise UsageError, ":ttl has no effect in a behavior_cache block" if options[:ttl]
52
+
53
+ key = caching_key(options.value_for_indifferent_key(:ignore), options.value_for_indifferent_key(:tag))
54
+ Interlock.register_dependencies(dependencies, key)
55
+
56
+ # See if the fragment exists, and run the block if it doesn't.
57
+ unless read_fragment(key)
58
+ Interlock.say key, "is running the controller block"
59
+ yield
60
+ end
61
+ end
62
+
63
+ #:stopdoc:
64
+ alias :caching :behavior_cache # Deprecated
65
+ #:startdoc:
66
+
67
+ private
68
+
69
+ #
70
+ # Callback to reset the local cache.
71
+ #
72
+ def clear_interlock_local_cache
73
+ Interlock.local_cache = ActionController::Base::MemoryStore.new
74
+ RAILS_DEFAULT_LOGGER.warn "** cleared interlock local cache"
75
+ end
76
+
77
+ # Should be registered first in the chain
78
+ prepend_before_filter :clear_interlock_local_cache
79
+
80
+ end
81
+
82
+ module Caching #:nodoc:
83
+ module Fragments
84
+
85
+ #
86
+ # Replaces Rail's write_fragment method. Avoids extra checks for regex keys,
87
+ # which are unsupported, adds more detailed logging information, and stores
88
+ # writes in the local process cache too to avoid duplicate memcached requests.
89
+ #
90
+ def write_fragment(key, content, options = nil)
91
+ return unless perform_caching
92
+
93
+ fragment_cache_store.write(key, content, options)
94
+ Interlock.local_cache.write(key, content, options)
95
+
96
+ Interlock.say key, "wrote"
97
+
98
+ content
99
+ end
100
+
101
+ #
102
+ # Replaces Rail's read_fragment method. Avoids checks for regex keys,
103
+ # which are unsupported, adds more detailed logging information, and
104
+ # checks the local process cache before hitting memcached. Hits on
105
+ # memcached are then stored back locally to avoid duplicate requests.
106
+ #
107
+ def read_fragment(key, options = nil)
108
+ return unless perform_caching
109
+
110
+ if content = Interlock.local_cache.read(key, options)
111
+ # Interlock.say key, "read from local cache"
112
+ elsif content = fragment_cache_store.read(key, options)
113
+ Interlock.say key, "read from memcached"
114
+ Interlock.local_cache.write(key, content, options)
115
+ else
116
+ # Interlock.say key, "not found"
117
+ end
118
+ content
119
+ end
120
+
121
+ end
122
+ end
123
+ end