# How rails check and fetch Page Cache file?

**URL:** <https://www.ruby-forum.com/t/how-rails-check-and-fetch-page-cache-file/104490>\
**Category:** Rails\
**Created:** [May 18, 2007, 5:42pm UTC](https://www.ruby-forum.com/t/how-rails-check-and-fetch-page-cache-file/104490 "2007-05-18T17:42:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [May 18, 2007, 5:42pm UTC](https://www.ruby-forum.com/t/how-rails-check-and-fetch-page-cache-file/104490/1 "2007-05-18T17:42:15Z")

</div>

After posting this question  
page cache more than 100K files inside a single directory?  
[http://www.ruby-forum.com/topic/108450#new](http://www.ruby-forum.com/topic/108450#new)  
I’ve been look into how page caching works.

Now I’ve rewritten page\_cache\_file(path) (Module  
ActionController::Caching::Pages::ClassMethods). now it will divide  
page\_cache files into subdirectories to avoid more than 10K files to be  
stored inside a single directory. (788.html-\> 0/788.html; 79780.html-\>  
7/9780.html).

But since the default save path being changed, rails generate the  
content from database ignoring the cached files.  
so, I need to let rails know that cache files’ location are changed

Digging for hours, I couldn’t get any idea on how rails check and fetch  
page cache files.

Can anyone give me a clue?

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [May 18, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/how-rails-check-and-fetch-page-cache-file/104490/2 "2007-05-18T19:06:53Z")

</div>

On 5/18/07, Nanyang Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 7/9780.html).
> 
> But since the default save path being changed, rails generate the  
> content from database ignoring the cached files.  
> so, I need to let rails know that cache files’ location are changed
> 
> Digging for hours, I couldn’t get any idea on how rails check and fetch  
> page cache files.
> 
> Can anyone give me a clue?

It doesn’t. Your web server’s rewrite routes need to be written in a  
way that it checks for the existence of a file and serves it before  
even bothering rails. This is static-heavy sites like the main Rails  
site and weblog are blazing fast. Apache’s doing the heavy lifting.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [May 18, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/how-rails-check-and-fetch-page-cache-file/104490/3 "2007-05-18T19:59:00Z")

</div>

On 5/18/07, Nanyang Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> still knew the cache files. It let me believe that the job was done  
> inside the rails…  
> so I was wrong…

Mongrel does the same thing too. When in doubt, watch your logs for  
rails requests.

> Now I’ll try to use apache’s httpd.conf file. It surely can redirect to  
> cache file.  
> Before that, I need to learn complex rewrite rule for mod\_rewrite…  
> If you’ve done this before, can you give me a quick help?  
> I need a apache mod\_rewrite rule to rewrite  
> /data/98 to /data/0/98.html  
> and  
> /data/89798 to /data/8/9798.html  
> (/data/:id, ids are divided into two parts, four digitals for file name,  
> left will be dir name.)

Why not just use the partitioned IDs in the rails routes themselves?

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [May 18, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/how-rails-check-and-fetch-page-cache-file/104490/4 "2007-05-18T20:14:24Z")

</div>

Rick O. wrote:

> Why not just use the partitioned IDs in the rails routes themselves?

sorry, I don’t understand.

You mean rails know partitioned ids?  
or you suggest instead of calling /data/89798, but using /data/8/9798 to  
fetch the data?

I think apache rewrite is perfect to do this rewrite job. Faster at  
least.

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [May 18, 2007, 7:33pm UTC](https://www.ruby-forum.com/t/how-rails-check-and-fetch-page-cache-file/104490/5 "2007-05-18T19:33:32Z")

</div>

Rick O. wrote:

> Your web server’s rewrite routes need to be written in a  
> way that it checks for the existence of a file and serves it before  
> even bothering rails.

I’d tried to remove  
RewriteRule ^([^.]+)$ $1.html [QSA]  
from public/.htaccess, but mongrel(development env, without apache)  
still knew the cache files. It let me believe that the job was done  
inside the rails…  
so I was wrong…

Now I’ll try to use apache’s httpd.conf file. It surely can redirect to  
cache file.  
Before that, I need to learn complex rewrite rule for mod\_rewrite…  
If you’ve done this before, can you give me a quick help?  
I need a apache mod\_rewrite rule to rewrite  
/data/98 to /data/0/98.html  
and  
/data/89798 to /data/8/9798.html  
(/data/:id, ids are divided into two parts, four digitals for file name,  
left will be dir name.)

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [May 18, 2007, 8:33pm UTC](https://www.ruby-forum.com/t/how-rails-check-and-fetch-page-cache-file/104490/6 "2007-05-18T20:33:42Z")

</div>

finally, I come up with these rewrite rule:  
RewriteRule ^([^.]+)/(\d{1,4})$ /$1/0/$2.html [QSA]  
RewriteRule ^([^.]+)/(\d+)(\d{4,})$ /$1/$2/$3.html [QSA]

will them work?

> I need a apache mod\_rewrite rule to rewrite  
> /data/98 to /data/0/98.html  
> and  
> /data/89798 to /data/8/9798.html  
> (/data/:id, ids are divided into two parts, four digitals for file name,  
> left will be dir name.)

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [May 18, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/how-rails-check-and-fetch-page-cache-file/104490/7 "2007-05-18T20:22:05Z")

</div>

> or you suggest instead of calling /data/89798, but using /data/8/9798 to  
> fetch the data?

Yup.

> I think apache rewrite is perfect to do this rewrite job. Faster at  
> least.

Ok.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [May 18, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/how-rails-check-and-fetch-page-cache-file/104490/8 "2007-05-18T20:41:19Z")

</div>

Rick O. wrote:

> > using /data/8/9798 to fetch the data?
> 
> Yup.

BTW,  
this url is ugly and misleading. I won’t use it.
