@@ -8,7 +8,9 @@ Installation
88
99Add this line to your application's Gemfile:
1010
11- gem "actionpack-action_caching"
11+ ``` ruby
12+ gem ' actionpack-action_caching'
13+ ```
1214
1315And then execute:
1416
@@ -28,12 +30,14 @@ that filters run before the cache is served, which allows for
2830authentication and other restrictions on whether someone is allowed
2931to execute such action.
3032
31- class ListsController < ApplicationController
32- before_action :authenticate, except: :public
33+ ``` ruby
34+ class ListsController < ApplicationController
35+ before_action :authenticate , except: :public
3336
34- caches_page :public
35- caches_action :index, :show
36- end
37+ caches_page :public
38+ caches_action :index , :show
39+ end
40+ ```
3741
3842In this example, the ` public ` action doesn't require authentication
3943so it's possible to use the faster page caching. On the other hand
@@ -72,36 +76,38 @@ interval (in seconds) to schedule expiration of the cached item.
7276
7377The following example depicts some of the points made above:
7478
75- class ListsController < ApplicationController
76- before_action :authenticate, except: :public
79+ ``` ruby
80+ class ListsController < ApplicationController
81+ before_action :authenticate , except: :public
7782
78- # simple fragment cache
79- caches_action :current
83+ # simple fragment cache
84+ caches_action :current
8085
81- # expire cache after an hour
82- caches_action :archived, expires_in: 1.hour
86+ # expire cache after an hour
87+ caches_action :archived , expires_in: 1 .hour
8388
84- # cache unless it's a JSON request
85- caches_action :index, unless: -> { request.format.json? }
89+ # cache unless it's a JSON request
90+ caches_action :index , unless: -> { request.format .json? }
8691
87- # custom cache path
88- caches_action :show, cache_path: { project: 1 }
92+ # custom cache path
93+ caches_action :show , cache_path: { project: 1 }
8994
90- # custom cache path with a proc
91- caches_action :history, cache_path: -> { request.domain }
95+ # custom cache path with a proc
96+ caches_action :history , cache_path: -> { request.domain }
9297
93- # custom cache path with a symbol
94- caches_action :feed, cache_path: :user_cache_path
98+ # custom cache path with a symbol
99+ caches_action :feed , cache_path: :user_cache_path
95100
96- protected
97- def user_cache_path
98- if params[:user_id]
99- user_list_url(params[:user_id], params[:id])
100- else
101- list_url(params[:id])
102- end
103- end
101+ protected
102+ def user_cache_path
103+ if params[:user_id ]
104+ user_list_url(params[:user_id ], params[:id ])
105+ else
106+ list_url(params[:id ])
107+ end
104108 end
109+ end
110+ ```
105111
106112If you pass ` layout: false ` , it will only cache your action
107113content. That's useful when your layout has dynamic information.
0 commit comments