Skip to content

Commit 900e4ad

Browse files
committed
Correct syntax of examples and show new to_proc option
1 parent 309b429 commit 900e4ad

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

README.md

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ You can modify the default action cache path by passing a
6161
`:cache_path` option. This will be passed directly to
6262
`ActionCachePath.new`. This is handy for actions with
6363
multiple possible routes that should be cached differently. If a
64-
block is given, it is called with the current controller instance.
64+
proc (or an object that responds to `to_proc`) is given, it is
65+
called with the current controller instance.
6566

66-
And you can also use `:if` (or `:unless`) to pass a
67-
proc that specifies when the action should be cached.
67+
And you can also use `:if` (or `:unless`) to control when the action
68+
should be cached, similar to how you use them with `before_action`.
6869

6970
As of Rails 3.0, you can also pass `:expires_in` with a time
7071
interval (in seconds) to schedule expiration of the cached item.
@@ -74,22 +75,32 @@ The following example depicts some of the points made above:
7475
class ListsController < ApplicationController
7576
before_action :authenticate, except: :public
7677

77-
caches_page :public
78+
# simple fragment cache
79+
caches_action :current
7880

79-
caches_action :index, if: Proc.new do
80-
!request.format.json? # cache if is not a JSON request
81-
end
81+
# expire cache after an hour
82+
caches_action :archived, expires_in: 1.hour
8283

83-
caches_action :show, cache_path: { project: 1 },
84-
expires_in: 1.hour
84+
# cache unless it's a JSON request
85+
caches_action :index, unless: -> { request.format.json? }
8586

86-
caches_action :feed, cache_path: Proc.new do
87-
if params[:user_id]
88-
user_list_url(params[:user_id], params[:id])
89-
else
90-
list_url(params[:id])
87+
# custom cache path
88+
caches_action :show, cache_path: { project: 1 }
89+
90+
# custom cache path with a proc
91+
caches_action :history, cache_path: -> { request.domain }
92+
93+
# custom cache path with a symbol
94+
caches_action :feed, cache_path: :user_cache_path
95+
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
91103
end
92-
end
93104
end
94105

95106
If you pass `layout: false`, it will only cache your action

0 commit comments

Comments
 (0)