active_cache 0.0.0 → 0.0.1
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.rdoc +64 -1
- data/lib/action_controller/macros.rb +2 -2
- data/lib/active_cache.rb +2 -0
- data/lib/active_cache/store/dalli_store.rb +12 -2
- data/lib/active_cache/version.rb +1 -1
- metadata +52 -50
data/README.rdoc
CHANGED
@@ -1,3 +1,66 @@
|
|
1
1
|
= ActiveCache
|
2
2
|
|
3
|
-
|
3
|
+
Active cache may be the simple boost for your app. The idea is to store same pages in different states.
|
4
|
+
|
5
|
+
== Getting started
|
6
|
+
|
7
|
+
1. Install the gem
|
8
|
+
sudo gem install active_cache
|
9
|
+
2. Make sure that you have memcached installed and running
|
10
|
+
sudo apt-get install memcached
|
11
|
+
3. Set cache store in your 'config/application.rb' filea
|
12
|
+
config.active_cache_store = ActiveCache::Store::DalliStore
|
13
|
+
|
14
|
+
== Sample usage
|
15
|
+
|
16
|
+
Let us suppose that you have controller 'home' containing action 'index' which accepts page parameter do display different content.
|
17
|
+
|
18
|
+
Normally you would use
|
19
|
+
caches_page :index
|
20
|
+
But this will cause two problems:
|
21
|
+
- it will store only first requested page fe: /home/?page=3, so when user will visit /home/?page=4 he would not receive proper content.
|
22
|
+
- most time logged in users see pages slightly different, for example user 'john' may see information 'You are signed in as john'
|
23
|
+
|
24
|
+
So here comes active_cache, adding line:
|
25
|
+
class HomeController < ApplicationController
|
26
|
+
|
27
|
+
. . .
|
28
|
+
|
29
|
+
active_cache :index, :state => lambda{ |c| {:page => c.params[:page]} }
|
30
|
+
|
31
|
+
will cause to store pages dependent of :page. If user visited page /?page=1 - next time he will get this page directly from memory.
|
32
|
+
|
33
|
+
Also you may want to use cache only when some condition if satisfied (for example store pages in cache only for guests). This may be done by:
|
34
|
+
|
35
|
+
active_cache :index, :state => lambda{ |c| {:page => c.params[:page]} }, :if => lambda{ |c| !c.user_signed_in? }
|
36
|
+
|
37
|
+
Next option you can use is :expires_in parameter:
|
38
|
+
|
39
|
+
active_cache :index, :state => lambda{ |c| {:page => c.params[:page]} }, :expires_in => 100.seconds
|
40
|
+
|
41
|
+
== Profit
|
42
|
+
|
43
|
+
First action call will write page content into memory.
|
44
|
+
|
45
|
+
Example logfile output on first page visit:
|
46
|
+
Started GET "/?page=144" for 127.0.0.1 at Tue Nov 08 12:41:47 +0100 2011
|
47
|
+
Processing by HomeController#index as HTML
|
48
|
+
Parameters: {"page"=>"144"}
|
49
|
+
Rendered home/index.html.erb within layouts/application (0.1ms)
|
50
|
+
Completed 200 OK in 50ms (Views: 7.3ms | ActiveRecord: 0.0ms)
|
51
|
+
|
52
|
+
Second page visit:
|
53
|
+
Started GET "/?page=144" for 127.0.0.1 at Tue Nov 08 12:41:50 +0100 2011
|
54
|
+
Processing by HomeController#index as HTML
|
55
|
+
Parameters: {"page"=>"144"}
|
56
|
+
Rendered from memory
|
57
|
+
Rendered text template (0.0ms)
|
58
|
+
Completed 200 OK in 6ms (Views: 5.7ms | ActiveRecord: 0.0ms)
|
59
|
+
|
60
|
+
so page loading time has been reduced from 50ms to 6ms. Active cache will be cost-effective specially for heavy pages, where lot of content is loaded.
|
61
|
+
|
62
|
+
I hope you will find this gem useful.
|
63
|
+
|
64
|
+
PLEASE NOTE THAT THIS IS STILL GERM OF AN IDEA THAT MAY BECOME GOOD SOLUTION. I need to write proper tests, add few options like expiration time et caetera.
|
65
|
+
|
66
|
+
This project rocks and uses MIT-LICENSE.
|
@@ -2,7 +2,7 @@ class ActionController::Base
|
|
2
2
|
|
3
3
|
def self.active_cache(action, *args)
|
4
4
|
args = args.extract_options!
|
5
|
-
state, condition = args[:state], (args[:if] || lambda{ true})
|
5
|
+
state, condition, expires = args[:state], (args[:if] || lambda{ true}), args[:expires_in]
|
6
6
|
before_filter({:only => action}) do |c|
|
7
7
|
return unless condition.call(c)
|
8
8
|
if p = ActiveCache::cache_store.read(state.call(c).merge!({:action => action}))
|
@@ -13,7 +13,7 @@ class ActionController::Base
|
|
13
13
|
|
14
14
|
after_filter({:only => action}) do |c|
|
15
15
|
return unless condition.call(c)
|
16
|
-
ActiveCache::cache_store.write(state.call(c).merge!({:action => action})
|
16
|
+
ActiveCache::cache_store.write(state.call(c).merge!({:action => action}), response.body, expires)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
data/lib/active_cache.rb
CHANGED
@@ -9,16 +9,26 @@ module ActiveCache
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def self.read(key)
|
12
|
-
dc.get(key)
|
12
|
+
page = dc.get(key)
|
13
|
+
case
|
14
|
+
when page.nil?
|
15
|
+
nil
|
16
|
+
when page[:expires].nil?
|
17
|
+
page[:content]
|
18
|
+
else
|
19
|
+
page[:expires] > Time.now ? page[:content] : nil
|
20
|
+
end
|
13
21
|
end
|
14
22
|
|
15
23
|
def self.write(key, value, expire = nil)
|
16
|
-
|
24
|
+
expire = Time.now + expire unless expire.nil?
|
25
|
+
dc.set(key,{:expires => expire, :content => value})
|
17
26
|
end
|
18
27
|
|
19
28
|
def self.expire(key)
|
20
29
|
dc.delete(key)
|
21
30
|
end
|
31
|
+
|
22
32
|
end
|
23
33
|
|
24
34
|
end
|
data/lib/active_cache/version.rb
CHANGED
metadata
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
|
-
-
|
13
|
+
- Pawel Mikolajewski
|
14
14
|
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-11-
|
18
|
+
date: 2011-11-08 00:00:00 +01:00
|
19
|
+
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: rails
|
@@ -71,49 +72,50 @@ extensions: []
|
|
71
72
|
extra_rdoc_files: []
|
72
73
|
|
73
74
|
files:
|
74
|
-
- lib/active_cache.rb
|
75
75
|
- lib/action_controller/macros.rb
|
76
|
-
- lib/
|
76
|
+
- lib/active_cache/active_cache_store.rb
|
77
77
|
- lib/active_cache/store/dalli_store.rb
|
78
78
|
- lib/active_cache/version.rb
|
79
|
-
- lib/active_cache
|
79
|
+
- lib/active_cache.rb
|
80
|
+
- lib/tasks/active_cache_tasks.rake
|
80
81
|
- MIT-LICENSE
|
81
82
|
- Rakefile
|
82
83
|
- README.rdoc
|
83
84
|
- test/active_cache_store_test.rb
|
84
85
|
- test/dummy/config.ru
|
86
|
+
- test/dummy/log/test.log
|
87
|
+
- test/dummy/Rakefile
|
85
88
|
- test/dummy/script/rails
|
89
|
+
- test/dummy/app/views/layouts/application.html.erb
|
90
|
+
- test/dummy/app/controllers/application_controller.rb
|
91
|
+
- test/dummy/app/helpers/application_helper.rb
|
92
|
+
- test/dummy/app/assets/javascripts/application.js
|
93
|
+
- test/dummy/app/assets/stylesheets/application.css
|
86
94
|
- test/dummy/public/404.html
|
87
|
-
- test/dummy/public/favicon.ico
|
88
|
-
- test/dummy/public/500.html
|
89
95
|
- test/dummy/public/422.html
|
96
|
+
- test/dummy/public/500.html
|
97
|
+
- test/dummy/public/favicon.ico
|
90
98
|
- test/dummy/config/environment.rb
|
91
|
-
- test/dummy/config/
|
99
|
+
- test/dummy/config/application.rb
|
100
|
+
- test/dummy/config/database.yml
|
92
101
|
- test/dummy/config/boot.rb
|
93
|
-
- test/dummy/config/
|
94
|
-
- test/dummy/config/environments/development.rb
|
95
|
-
- test/dummy/config/environments/test.rb
|
96
|
-
- test/dummy/config/initializers/mime_types.rb
|
97
|
-
- test/dummy/config/initializers/session_store.rb
|
102
|
+
- test/dummy/config/locales/en.yml
|
98
103
|
- test/dummy/config/initializers/inflections.rb
|
99
104
|
- test/dummy/config/initializers/secret_token.rb
|
100
|
-
- test/dummy/config/initializers/wrap_parameters.rb
|
101
105
|
- test/dummy/config/initializers/backtrace_silencers.rb
|
102
|
-
- test/dummy/config/
|
103
|
-
- test/dummy/config/
|
104
|
-
- test/dummy/config/
|
106
|
+
- test/dummy/config/initializers/session_store.rb
|
107
|
+
- test/dummy/config/initializers/wrap_parameters.rb
|
108
|
+
- test/dummy/config/initializers/mime_types.rb
|
109
|
+
- test/dummy/config/routes.rb
|
110
|
+
- test/dummy/config/environments/test.rb
|
111
|
+
- test/dummy/config/environments/production.rb
|
112
|
+
- test/dummy/config/environments/development.rb
|
105
113
|
- test/dummy/db/test.sqlite3
|
106
|
-
- test/
|
107
|
-
- test/dummy/app/assets/javascripts/application.js
|
108
|
-
- test/dummy/app/views/layouts/application.html.erb
|
109
|
-
- test/dummy/app/helpers/application_helper.rb
|
110
|
-
- test/dummy/app/controllers/application_controller.rb
|
111
|
-
- test/dummy/Rakefile
|
112
|
-
- test/dummy/log/test.log
|
114
|
+
- test/action_controller_test.rb
|
113
115
|
- test/dalli_store_test.rb
|
114
116
|
- test/test_helper.rb
|
115
|
-
- test/action_controller_test.rb
|
116
117
|
- test/active_cache_test.rb
|
118
|
+
has_rdoc: true
|
117
119
|
homepage: https://github.com/dfens/active_cache
|
118
120
|
licenses: []
|
119
121
|
|
@@ -143,42 +145,42 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
143
145
|
requirements: []
|
144
146
|
|
145
147
|
rubyforge_project:
|
146
|
-
rubygems_version: 1.
|
148
|
+
rubygems_version: 1.3.7
|
147
149
|
signing_key:
|
148
150
|
specification_version: 3
|
149
151
|
summary: Conditional cache implementation
|
150
152
|
test_files:
|
151
153
|
- test/active_cache_store_test.rb
|
152
154
|
- test/dummy/config.ru
|
155
|
+
- test/dummy/log/test.log
|
156
|
+
- test/dummy/Rakefile
|
153
157
|
- test/dummy/script/rails
|
158
|
+
- test/dummy/app/views/layouts/application.html.erb
|
159
|
+
- test/dummy/app/controllers/application_controller.rb
|
160
|
+
- test/dummy/app/helpers/application_helper.rb
|
161
|
+
- test/dummy/app/assets/javascripts/application.js
|
162
|
+
- test/dummy/app/assets/stylesheets/application.css
|
154
163
|
- test/dummy/public/404.html
|
155
|
-
- test/dummy/public/favicon.ico
|
156
|
-
- test/dummy/public/500.html
|
157
164
|
- test/dummy/public/422.html
|
165
|
+
- test/dummy/public/500.html
|
166
|
+
- test/dummy/public/favicon.ico
|
158
167
|
- test/dummy/config/environment.rb
|
159
|
-
- test/dummy/config/
|
168
|
+
- test/dummy/config/application.rb
|
169
|
+
- test/dummy/config/database.yml
|
160
170
|
- test/dummy/config/boot.rb
|
161
|
-
- test/dummy/config/
|
162
|
-
- test/dummy/config/environments/development.rb
|
163
|
-
- test/dummy/config/environments/test.rb
|
164
|
-
- test/dummy/config/initializers/mime_types.rb
|
165
|
-
- test/dummy/config/initializers/session_store.rb
|
171
|
+
- test/dummy/config/locales/en.yml
|
166
172
|
- test/dummy/config/initializers/inflections.rb
|
167
173
|
- test/dummy/config/initializers/secret_token.rb
|
168
|
-
- test/dummy/config/initializers/wrap_parameters.rb
|
169
174
|
- test/dummy/config/initializers/backtrace_silencers.rb
|
170
|
-
- test/dummy/config/
|
171
|
-
- test/dummy/config/
|
172
|
-
- test/dummy/config/
|
175
|
+
- test/dummy/config/initializers/session_store.rb
|
176
|
+
- test/dummy/config/initializers/wrap_parameters.rb
|
177
|
+
- test/dummy/config/initializers/mime_types.rb
|
178
|
+
- test/dummy/config/routes.rb
|
179
|
+
- test/dummy/config/environments/test.rb
|
180
|
+
- test/dummy/config/environments/production.rb
|
181
|
+
- test/dummy/config/environments/development.rb
|
173
182
|
- test/dummy/db/test.sqlite3
|
174
|
-
- test/
|
175
|
-
- test/dummy/app/assets/javascripts/application.js
|
176
|
-
- test/dummy/app/views/layouts/application.html.erb
|
177
|
-
- test/dummy/app/helpers/application_helper.rb
|
178
|
-
- test/dummy/app/controllers/application_controller.rb
|
179
|
-
- test/dummy/Rakefile
|
180
|
-
- test/dummy/log/test.log
|
183
|
+
- test/action_controller_test.rb
|
181
184
|
- test/dalli_store_test.rb
|
182
185
|
- test/test_helper.rb
|
183
|
-
- test/action_controller_test.rb
|
184
186
|
- test/active_cache_test.rb
|