@@ -8,7 +8,9 @@ Installation
8
8
9
9
Add this line to your application's Gemfile:
10
10
11
- gem "actionpack-action_caching"
11
+ ``` ruby
12
+ gem ' actionpack-action_caching'
13
+ ```
12
14
13
15
And then execute:
14
16
@@ -28,12 +30,14 @@ that filters run before the cache is served, which allows for
28
30
authentication and other restrictions on whether someone is allowed
29
31
to execute such action.
30
32
31
- class ListsController < ApplicationController
32
- before_action :authenticate, except: :public
33
+ ``` ruby
34
+ class ListsController < ApplicationController
35
+ before_action :authenticate , except: :public
33
36
34
- caches_page :public
35
- caches_action :index, :show
36
- end
37
+ caches_page :public
38
+ caches_action :index , :show
39
+ end
40
+ ```
37
41
38
42
In this example, the ` public ` action doesn't require authentication
39
43
so 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.
72
76
73
77
The following example depicts some of the points made above:
74
78
75
- class ListsController < ApplicationController
76
- before_action :authenticate, except: :public
79
+ ``` ruby
80
+ class ListsController < ApplicationController
81
+ before_action :authenticate , except: :public
77
82
78
- # simple fragment cache
79
- caches_action :current
83
+ # simple fragment cache
84
+ caches_action :current
80
85
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
83
88
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? }
86
91
87
- # custom cache path
88
- caches_action :show, cache_path: { project: 1 }
92
+ # custom cache path
93
+ caches_action :show , cache_path: { project: 1 }
89
94
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 }
92
97
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
95
100
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
104
108
end
109
+ end
110
+ ```
105
111
106
112
If you pass ` layout: false ` , it will only cache your action
107
113
content. That's useful when your layout has dynamic information.
0 commit comments