# Caching question

**URL:** <https://www.ruby-forum.com/t/caching-question/115862>\
**Category:** Rails\
**Created:** [September 29, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/caching-question/115862 "2007-09-29T13:07:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RSElliott\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@RSElliott\_M](https://www.ruby-forum.com/u/RSElliott_M)\
**Post date:** [September 29, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/caching-question/115862/1 "2007-09-29T13:07:58Z")

</div>

My application has content that I’d like to cache, however that content  
varies from user to user. More specifically, the application is  
something like a forum, and the appearance of posts changes if the user  
who made the post is viewing it. (So theoretically there’s only two  
variations of the way the view is rendered, one for the author and one  
for any other user.) This combined with the fact that I am using  
relative dates has made it difficult for me to find a caching solution.  
Although I haven’t actually tried any of them, having read about them, I  
don’t think any of them fit my needs.

What I am really trying to avoid is unneeded database queries. A post  
doesn’t change after its been made unless its edited, which is rare.  
However every time a user loads a page full of posts, queries are run to  
get the same data, over and over again. Not only that, but the way I am  
verifying whether or not a user did or didn’t make a post is using  
cookies - if the user has a cookie that matches him or her to the post  
he or she sees edit/delete icons in that post, so the way of verifying  
if the user owns a post is independent of the database and the  
controller and is something I reserve for the view/helper. So I really  
would like to just cut the controller/model out of the whole deal.

Is there a way to cache the unchanging parts of a view and then update  
that view if necessary during rendering? Or, is there a way to cache  
just the results of the database query?

---

<div class="post-metadata">

**Author:** ![RSElliott\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@RSElliott\_M](https://www.ruby-forum.com/u/RSElliott_M)\
**Post date:** [September 29, 2007, 2:53pm UTC](https://www.ruby-forum.com/t/caching-question/115862/2 "2007-09-29T14:53:08Z")

</div>

> Is there a way to cache the unchanging parts of a view and then update  
> that view if necessary during rendering? Or, is there a way to cache  
> just the results of the database query?

Hi,

I’m also studding caching mechanisms in RoR.  
If you need to cache a part of view you can use \<% cache do %\> \<% end  
%\>.

Example(i cache a part of view for each language in my view):  
\<% cache “langbar-#{get\_current\_lang}” do %\>  
\<%= render :partial =\> ‘/langbar’ %\>  
\<% end %\>

It generates \*.cache files in tmp/cache directory, and i get more  
reqs/sec.

---

<div class="post-metadata">

**Author:** ![RSElliott\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@RSElliott\_M](https://www.ruby-forum.com/u/RSElliott_M)\
**Post date:** [September 30, 2007, 12:11am UTC](https://www.ruby-forum.com/t/caching-question/115862/3 "2007-09-30T00:11:27Z")

</div>

Thanks, but I don’t think that will be helpful in my case. Since I’ve  
already pre-loaded all the records before the view is even rendered,  
simply caching the views wouldn’t do me that much.

---

<div class="post-metadata">

**Author:** ![RSElliott\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@RSElliott\_M](https://www.ruby-forum.com/u/RSElliott_M)\
**Post date:** [September 30, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/caching-question/115862/4 "2007-09-30T17:36:07Z")

</div>

Also, you may want to take a look at beast - their forum solution is  
pretty efficient and its open sourced.

On Sep 29, 4:07 am, “R. Elliott M.” \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![RSElliott\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@RSElliott\_M](https://www.ruby-forum.com/u/RSElliott_M)\
**Post date:** [September 30, 2007, 9:47am UTC](https://www.ruby-forum.com/t/caching-question/115862/5 "2007-09-30T09:47:11Z")

</div>

You may will be interested in “Ambition” :[http://errtheblog.com/post/](http://errtheblog.com/post/)  
10722  
It could resolve the cache problem in view.

On Sep 30, 6:11 am, “R. Elliott M.” \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![RSElliott\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@RSElliott\_M](https://www.ruby-forum.com/u/RSElliott_M)\
**Post date:** [October 1, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/caching-question/115862/6 "2007-10-01T22:15:04Z")

</div>

Well I think cache\_fu or something along those lines would be the best  
option theoretically, but the presentation for it says you don’t need it  
unless your site is already huge. Plus I’m wondering, since it uses  
memory, if it will even be good for my shared hosting environment.

I guess I’ll just put off caching.

---

<div class="post-metadata">

**Author:** ![RSElliott\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@RSElliott\_M](https://www.ruby-forum.com/u/RSElliott_M)\
**Post date:** [September 30, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/caching-question/115862/7 "2007-09-30T19:32:56Z")

</div>

You could check if the cache exists and then run the queries only if  
it doesn’t? (using fragment caching)

Also, there are options to cache queries using something like  
memcached - check out [http://errtheblog.com/post/4872](http://errtheblog.com/post/4872)

On Sep 30, 6:11 am, “R. Elliott M.” \<rails-mailing-l…@andreas-
