Reputation: 989
I use Memcached (actually, Memcachier on Heroku) for action caching in a Rails 3 app like so:
caches_action :index, :expires_in => 14.days
The problem is that my app is accessed from a number of different domains and the content needs to vary. Currently, it is cached the same for all domains. How do I achieve this?
Upvotes: 0
Views: 222
Reputation: 31467
The Rails caches_action
documentation says:
Action caching uses fragment caching internally and an around filter to do the job. The fragment cache is named according to the host and path of the request. A page that is accessed at http://david.example.com/lists/show/1 will result in a fragment named david.example.com/lists/show/1. This allows the cacher to differentiate between david.example.com/lists/ and jamis.example.com/lists/ – which is a helpful way of assisting the subdomain-as-account-key pattern.
So the generated keys should be differ for different domains.
To check it I even created an application with :mem_cache_store
and started a memcached server in verbose mode (-vv
).
The requests/responses looked like this for http://localhost:3000/
:
<21 get views/localhost:3000/index
>21 END
<21 set views/localhost:3000/index 0 0 7123
>21 STORED
With a different domain http://foobar:3000/
:
<21 get views/foobar:3000/index
>21 END
<21 set views/foobar:3000/index 0 0 7123
>21 STORED
If you want to create different cache keys depending on the request and the default is not enough for you, then you can use the :cache_path
option of caches_action
.
You can find examples in this SO question.
Upvotes: 1