Hash_cache a bogus function that never worked?

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

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-

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.

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. [email protected]

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

:slight_smile:

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. [email protected]