# Dynamic page caching directory based on subdomain name

**URL:** <https://www.ruby-forum.com/t/dynamic-page-caching-directory-based-on-subdomain-name/139411>\
**Category:** Rails\
**Created:** [June 3, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/dynamic-page-caching-directory-based-on-subdomain-name/139411 "2008-06-03T17:23:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sucrenoir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sucrenoir](https://www.ruby-forum.com/u/sucrenoir)\
**Post date:** [June 3, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/dynamic-page-caching-directory-based-on-subdomain-name/139411/1 "2008-06-03T17:23:46Z")

</div>

I am looking for the best way to set the cache directory based on the  
subdomain.

Example :  
if the subdomain host is [fr.mydomain.com](http://fr.mydomain.com), i want to cache the file in  
ROOT/cache/fr and if it is [en.mydomain.com](http://en.mydomain.com), i want to cache the file in  
ROOT/cache/en

The problem is that the cache directory is set in the config part of  
rails and i dont know how to change it in realtime.

Any good advice ?

ps:  
In my nginx config, of course, i would rewrite the uri based on this  
subdomain. This part is already working.

---

<div class="post-metadata">

**Author:** ![sucrenoir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sucrenoir](https://www.ruby-forum.com/u/sucrenoir)\
**Post date:** [June 3, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/dynamic-page-caching-directory-based-on-subdomain-name/139411/2 "2008-06-03T18:23:47Z")

</div>

Here’s something I just posted on my blog yesterday about storing a  
cached file. This applies to both action and page caching.

I assume you could specify the path at runtime based off the subdomain  
you’re using.

Also, take a look at the documentation in  
action\_controller/caching/pages.rb file:  
[http://github.com/rails/rails/tree/master/actionpack/lib/action\_controller/caching/pages.rb](http://github.com/rails/rails/tree/master/actionpack/lib/action_controller/caching/pages.rb).

– Josh

Jm Goem wrote:

> I am looking for the best way to set the cache directory based on the  
> subdomain.
> 
> Example :  
> if the subdomain host is [fr.mydomain.com](http://fr.mydomain.com), i want to cache the file in  
> ROOT/cache/fr and if it is [en.mydomain.com](http://en.mydomain.com), i want to cache the file in  
> ROOT/cache/en
> 
> The problem is that the cache directory is set in the config part of  
> rails and i dont know how to change it in realtime.
> 
> Any good advice ?
> 
> ps:  
> In my nginx config, of course, i would rewrite the uri based on this  
> subdomain. This part is already working.

---

<div class="post-metadata">

**Author:** ![sucrenoir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sucrenoir](https://www.ruby-forum.com/u/sucrenoir)\
**Post date:** [June 4, 2008, 10:32am UTC](https://www.ruby-forum.com/t/dynamic-page-caching-directory-based-on-subdomain-name/139411/3 "2008-06-04T10:32:05Z")

</div>

Thank you for this info. I guess you meant this post  
[http://thereverend.tumblr.com/post/36941827/rails-2-1-action-caching](http://thereverend.tumblr.com/post/36941827/rails-2-1-action-caching)

Great blog by the way 🙂

I read the comments in the code of ror and understand better how it  
works. Actually it is quite simple and i will probably rewrite the right  
method.

---

<div class="post-metadata">

**Author:** ![sucrenoir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sucrenoir](https://www.ruby-forum.com/u/sucrenoir)\
**Post date:** [June 4, 2008, 1:28pm UTC](https://www.ruby-forum.com/t/dynamic-page-caching-directory-based-on-subdomain-name/139411/4 "2008-06-04T13:28:24Z")

</div>

OK so i rewrite cache\_page with something like :

class ApplicationController \< ActionController::Base  
…  
class \<\< self  
def cache\_page content, path  
super content, @lang + path  
end  
end  
end

where @lang is set in a before\_filter of my controller. But it does not  
work ! @lang is nil so i get an error like “The error occurred while  
evaluating nil.+):”

I am missing something? How do i access @lang ?

Thank you in advance
