# Caching method calls - Is this of interest to anyone?

**URL:** <https://www.ruby-forum.com/t/caching-method-calls-is-this-of-interest-to-anyone/74081>\
**Category:** Ruby\
**Created:** [October 29, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/caching-method-calls-is-this-of-interest-to-anyone/74081 "2006-10-29T21:39:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [October 29, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/caching-method-calls-is-this-of-interest-to-anyone/74081/1 "2006-10-29T21:39:49Z")

</div>

Hi folks,

I’ve been trying to improve my metaprogramming skills the past two  
weeks and came up with the following library which caches specific  
method calls transparently so you don’t have to explicitly save the  
result. Is it interesting to anyone out there? If so I’ll gemify it  
and release it. A quick example:

require ‘cachablemethods’

def timer(start,result,finish)  
puts “Computed value #{result} in #{finish - start} seconds”  
end

class Foo  
include CachableMethods  
def bar(num)  
sleep(5)  
rand(num)  
end

def baz  
sleep(5)  
return yield(rand(1000))  
end

def baq  
sleep(5)  
rand(1000)  
end  
cache\_method :bar,:baz,:baq  
end

foo = Foo.new

puts “Initial calls cached”  
timer(Time.new,foo.bar(1000),Time.new)  
timer(Time.new,foo.baz{|n| n\*2},Time.new)  
timer(Time.new,foo.baq,Time.new)

puts “Calling method without parameters/blocks return cached result”  
timer(Time.new,foo.bar,Time.new)  
timer(Time.new,foo.baz,Time.new)  
timer(Time.new,foo.baq,Time.new)

puts “Calling methods with parameters/blocks or ending with ! if it  
takes neither recaches values”  
timer(Time.new,foo.bar(1000),Time.new)  
timer(Time.new,foo.baz{|n| n\*3},Time.new)  
timer(Time.new,foo.baq!,Time.new)

farrel@nicodemus ~/Projects/cachable/lib $ ruby test.rb  
Initial calls cached  
Computed value 516 in 4.998986 seconds  
Computed value 1848 in 4.999224 seconds  
Computed value 200 in 4.999236 seconds  
Calling method without parameters/blocks return cached result  
Computed value 516 in 3.6e-05 seconds  
Computed value 1848 in 2.0e-05 seconds  
Computed value 200 in 2.1e-05 seconds  
Calling methods with parameters/blocks or ending with ! if it takes  
neither recaches values  
Computed value 935 in 4.998461 seconds  
Computed value 648 in 4.999118 seconds  
Computed value 83 in 4.99987 seconds

Farrel

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [October 29, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/caching-method-calls-is-this-of-interest-to-anyone/74081/2 "2006-10-29T21:55:51Z")

</div>

Farrel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve been trying to improve my metaprogramming skills the past two  
> weeks and came up with the following library which caches specific  
> method calls transparently so you don’t have to explicitly save the  
> result. Is it interesting to anyone out there? If so I’ll gemify it  
> and release it.

[http://www.google.com/search?q=ruby+memoize](http://www.google.com/search?q=ruby+memoize)

```
robert

```

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [November 7, 2006, 10:45am UTC](https://www.ruby-forum.com/t/caching-method-calls-is-this-of-interest-to-anyone/74081/3 "2006-11-07T10:45:15Z")

</div>

Robert K. wrote:

> Farrel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I’ve been trying to improve my metaprogramming skills the past two  
> > weeks and came up with the following library which caches specific  
> > method calls transparently so you don’t have to explicitly save the  
> > result. Is it interesting to anyone out there? If so I’ll gemify it  
> > and release it.
> 
> [ruby memoize - Google Search](http://www.google.com/search?q=ruby+memoize)

It would be of interest to compare implimentations. There are a few out  
there.

Also a vague notion, but would it be possible to create such a cache  
that is “per original call”? Let say I call #x and it calls #y, #y, and  
#z where #y is cached. So #y only happens once actually. But if I call  
another method #q that’s calls #y it would do it again for #q. Make  
sense? I _think_ that would be equivalent to task a dependency system  
(like Rake).

T.
