# Hash\_cache a bogus function that never worked?

**URL:** <https://www.ruby-forum.com/t/hash-cache-a-bogus-function-that-never-worked/113582>\
**Category:** Rails\
**Created:** [September 2, 2007, 6:27am UTC](https://www.ruby-forum.com/t/hash-cache-a-bogus-function-that-never-worked/113582 "2007-09-02T06:27:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [September 2, 2007, 6:27am UTC](https://www.ruby-forum.com/t/hash-cache-a-bogus-function-that-never-worked/113582/1 "2007-09-02T06:27:18Z")

</div>

Hi,

I’ve been investigating various caching methods provided by Rail. I  
first looked at the hash\_cache module and function. In testing it, I  
noticed it wasn’t actually caching anything. Then looking at the source  
code, I noticed that it attempted to hold its cache in a class variable

- which won’t actually be saved from page request to page because of the  
way that rails works.

Hash\_cache seems to be a complete bogus, non-functional function which  
has been carried and documented for a while. Am I missing something?

Anyway, my test functions (within a descendant of  
application\_controller):

def cacheTest  
i = params[:id]  
res = methodToCache\_cache[i]  
print “value returned #{i}\n\n”  
render\_text res  
end  
def methodToCache i  
print “\n\nmethod actually called #{i}\n\n”  
return “test text \<%print #{i}%\>”  
end  
hash\_cache :methodToCache

The log results:  
…  
method actually called 67

value returned 67

127.0.0.1 - - [01/Sep/2007:16:15:19 Pacific Daylight Time] “GET  
/posts/cacheTest  
/67 HTTP/1.1” 200 22

- -\> /posts/cacheTest/67

method actually called 67

value returned 67

127.0.0.1 - - [01/Sep/2007:16:15:20 Pacific Daylight Time] “GET  
/posts/cacheTest  
/67 HTTP/1.1” 200 22

===\> Calling the function with the same values twice resulted in the  
being called twice, IE, not cached.

Has anyone tested this or other caching functionality of rails?

Joe

---

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [September 2, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/hash-cache-a-bogus-function-that-never-worked/113582/2 "2007-09-02T23:19:54Z")

</div>

I never used this, but I am wondering that maybe it only works in  
production environment instead of development and test envs?

On Sep 2, 12:27 am, “H. joseph Solbrig” \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [September 3, 2007, 2:08am UTC](https://www.ruby-forum.com/t/hash-cache-a-bogus-function-that-never-worked/113582/3 "2007-09-03T02:08:22Z")

</div>

Jack Zhan wrote:

> I never used this, but I am wondering that maybe it only works in  
> production environment instead of development and test envs?
> 
> On Sep 2, 12:27 am, “H. joseph Solbrig” \<rails-mailing-l…@andreas-

Definitely this is the problem. Classes are reloaded every request when  
in development. Besides, a hash-based cache would be an in-process cache  
only, and if you’re deploying like most people, you’re using multiple  
processes, so you could never share the cache.

---

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [September 3, 2007, 8:46am UTC](https://www.ruby-forum.com/t/hash-cache-a-bogus-function-that-never-worked/113582/4 "2007-09-03T08:46:39Z")

</div>

hash\_cache is not intended as a response caching mechanism. Rails has  
a number of solutions for this such as page, action, and fragment  
caching. There is a nice peepcode video on caching in rails, btw, and  
a number of other resources available.

In answer to the rails environment question, hash\_cache doesn’t work  
in development for the reason stated: the classes are reloaded, wiping  
out the class variable holding the cache. Similarly, normal caching in  
rails is turned off in the development environment as well.

Rein

On Sep 2, 7:08 pm, Bryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [September 6, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/hash-cache-a-bogus-function-that-never-worked/113582/5 "2007-09-06T18:05:31Z")

</div>

On Mon, 3 Sep 2007 21:53:10 +0200, H. joseph Solbrig wrote:

> But considering that it seem development and  
> production are different, could someone point me to any documentation  
> describing this or other differences?

~/myrailsapp $ cd config/environments  
~/myrailsapp/config/environments $ diff development.rb production.rb

🙂

---

<div class="post-metadata">

**Author:** ![HSjoseph\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@HSjoseph\_S](https://www.ruby-forum.com/u/HSjoseph_S)\
**Post date:** [September 3, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/hash-cache-a-bogus-function-that-never-worked/113582/6 "2007-09-03T21:53:10Z")

</div>

Thank you all for information.

Sorry if my original post came off as a bit hostile.

It seems a little odd to me that development and production environments  
would have different configurations. The point of development IMHO is to  
test everything that will go into production, with assert and debugging  
added but nothing removed. But considering that it seem development and  
production are different, could someone point me to any documentation  
describing this or other differences?

- On the subject of caching, I’m caching a lot of my page calculation  
but there some fields, spread through the page, that need to be  
calculated on-the-fly with page request. Thus neither caching a whole  
raw page nor caching a whole partial will work easily. Instead, I want  
to a page and then do some more calculations on it with each request.  
The crude functionality of hash\_cache seemed suited for this. It there  
another way folks could suggest?

Thanks again for all the useful advice so-far.

Joe

unknown wrote:

> hash\_cache is not intended as a response caching mechanism. Rails has  
> a number of solutions for this such as page, action, and fragment  
> caching. There is a nice peepcode video on caching in rails, btw, and  
> a number of other resources available.
> 
> In answer to the rails environment question, hash\_cache doesn’t work  
> in development for the reason stated: the classes are reloaded, wiping  
> out the class variable holding the cache. Similarly, normal caching in  
> rails is turned off in the development environment as well.
> 
> Rein
> 
> On Sep 2, 7:08 pm, Bryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
