# Is it possible to set the cache extension dynamically?

**URL:** <https://www.ruby-forum.com/t/is-it-possible-to-set-the-cache-extension-dynamically/85822>\
**Category:** Rails\
**Created:** [February 16, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-set-the-cache-extension-dynamically/85822 "2007-02-16T16:48:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [February 16, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-set-the-cache-extension-dynamically/85822/1 "2007-02-16T16:48:23Z")

</div>

I want to cache images I have in the database along with the html that  
is being put out.  
But the only way I have encountered to alter the cache extension from  
the default “.html” is to add  
ActionController::Base.page\_cache\_extension = “.jpg” to config/  
environment.rb.  
Naturally, what I need is save the HTML as “.html” and the JPGs as  
“.jpg”.  
Does anybody know how to do that?  
Thanks.  
-Sebastian

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [February 16, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-set-the-cache-extension-dynamically/85822/2 "2007-02-16T17:47:35Z")

</div>

> I want to cache images I have in the database along with the html that  
> is being put out.  
> But the only way I have encountered to alter the cache extension from  
> the default “.html” is to add  
> ActionController::Base.page\_cache\_extension = “.jpg” to config/  
> environment.rb.  
> Naturally, what I need is save the HTML as “.html” and the JPGs as  
> “.jpg”.  
> Does anybody know how to do that?

What version of Rails?

In 1.2 you can set a route like this:

map.photo ‘cache/photo/:id.jpg’, :controller =\> ‘home’, :action =\>  
‘photo’

Which would result in cache/photo/123.jpg being cached to disk.

In 1.16 you can do this:

map.photo ‘cache/photo/:id/image.jpg’, :controller =\> ‘home’, :action =\>  
‘photo’

Which would result in cache/photo/123/image.jpg being cached to disk.

The key is to use a named route so in your views you can use  
photo\_url(:id =\> 123) to generate the right url.

-philip

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [February 16, 2007, 6:14pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-set-the-cache-extension-dynamically/85822/3 "2007-02-16T18:14:37Z")

</div>

Philip, is it possible to use a sweeper to clean up these kinds of  
cached  
images? If so, this might be something that’d eliminate a good bit of  
wacky  
code I’ve got in one of my models. Or at least move it out to a Sweeper  
where it makes a little more sense.

RSL

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [February 17, 2007, 1:35am UTC](https://www.ruby-forum.com/t/is-it-possible-to-set-the-cache-extension-dynamically/85822/4 "2007-02-17T01:35:32Z")

</div>

On Feb 16, 4:55 pm, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> map.photo ‘cache/photo/:id/image.jpg’, :controller =\> ‘home’, :action =\> ‘photo’
> 
> Which would result in cache/photo/123/image.jpg being cached to disk.
> 
> The key is to use a named route so in your views you can use  
> photo\_url(:id =\> 123) to generate the right url.
> 
> -philip

That’s definitely a lot more flexible.  
It still stops short of being able to determine the MIME type and save  
the cache file with the according extension.  
But it is already much better.  
Thanks a lot!

-sebastian

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [March 8, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-set-the-cache-extension-dynamically/85822/5 "2007-03-08T18:46:38Z")

</div>

Well, it took me a while to get around to test it, but (of course, I  
might say) what Phillip suggested is much more flexible than I first  
thought:

You can declare your route like this:  
map.file ‘serve\_file/:id.:ext’

And then send your file like this:  
file\_url(:action =\> ‘serve\_file’, :id =\> target.id, :ext =\>  
target.extension)

Exactly what I needed!  
Thanks again.

Sebastian

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [February 16, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-set-the-cache-extension-dynamically/85822/6 "2007-02-16T18:25:39Z")

</div>

> Philip, is it possible to use a sweeper to clean up these kinds of cached  
> images? If so, this might be something that’d eliminate a good bit of wacky  
> code I’ve got in one of my models. Or at least move it out to a Sweeper  
> where it makes a little more sense.

Don’t know… never tried it 🙂 In my case I don’t want them to expire  
though so didn’t look into it.
