separate cache location per request

28 views
Skip to first unread message

Jim Gay

unread,
Jul 7, 2010, 1:10:48 PM7/7/10
to rack-cache
I'm crossing my fingers that there will be some more activity on this
list, so if there's a better place to post, let me know.

I'm curious about setting up separate cache locations according to the
request. Radiant CMS is built upon rack-cache and when we clear the
cache, we clear the whole thing (because who knows what content
appears in what cached responses). But this approach makes it
difficult to serve more than one site (with different content) and
maintain the cache where necessary.

I want to find a way to cache each domain in it's own directory like
this:

request to mysite.com pulls from tmp/mysite.com/cache/(entity or meta)
request to myothersite.com pulls from tmp/myothersite.com/cache/
(entity or meta)

Is there an existing way to do this, or could anyone point me to an
approach?

Ryan Tomayko

unread,
Jul 7, 2010, 1:45:27 PM7/7/10
to rack-...@googlegroups.com

It should be possible to set the 'rack-cache.metastore' and
'rack-cache.entitystore' Rack environment values somewhere before the
Rack::Cache middleware.

class CachePicker < Struct.new(:app)
def call(env)
domain = env['SERVER_NAME']
env['rack-cache.metastore'] = "file:/tmp/#{domain}/cache/meta"
env['rack-cache.entitystore'] = "file:/tmp/#{domain}/cache/entity"
app.call(env)
end
end

And then put that somewhere before Rack::Cache in config.ru (or
wherever you assemble the middleware pipeline):

use CachePicker
use Rack::Cache

It's worth noting that all of Rack::Cache's configuration values work
this way, so you can set them at boot time or at request time.

Thanks,
Ryan

Jim Gay

unread,
Jul 7, 2010, 2:10:12 PM7/7/10
to rack-...@googlegroups.com
Awsome! Thanks for the fast response, Ryan.

> --
> You received this message because you are subscribed to the Google
> Groups "rack-cache" group.
> To post to this group, send email to rack-...@googlegroups.com.
> To unsubscribe from this group, send email to rack-cache+...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/rack-cache?hl=en
> .
>

Reply all
Reply to author
Forward
0 new messages