Skip to content

Instantly share code, notes, and snippets.

@jhbabon
Created June 2, 2012 11:12
Show Gist options
  • Save jhbabon/2857828 to your computer and use it in GitHub Desktop.
Save jhbabon/2857828 to your computer and use it in GitHub Desktop.
Extremely simple cache method system for ruby objects
Class cache
Output => -3400421267002374593 :: -1797544009241115821 :: 4
0.000000 0.000000 0.000000 ( 4.000965)
Output => -3400421267002374593 :: -1797544009241115821 :: 4
0.000000 0.000000 0.000000 ( 0.000068)
Output => -3400421267002374593 :: -1797544009241115821 :: 4
0.000000 0.000000 0.000000 ( 0.000069)
Output => -3400421267002374593 :: -2043295954666096465 :: 6
0.000000 0.000000 0.000000 ( 6.001122)
Output => -3400421267002374593 :: -2043295954666096465 :: 6
0.000000 0.000000 0.000000 ( 0.000061)
Output => -3400421267002374593 :: -2043295954666096465 :: 6
0.000000 0.000000 0.000000 ( 0.000080)
Instance cache
Output => -4338026385995737381 :: 1636374567177026755 :: 3
0.000000 0.000000 0.000000 ( 3.001247)
Output => -4338026385995737381 :: 1636374567177026755 :: 3
0.000000 0.010000 0.010000 ( 0.000084)
Output => -4338026385995737381 :: 1636374567177026755 :: 3
0.000000 0.000000 0.000000 ( 0.000070)
Output => -4338026385995737381 :: -129914481430179661 :: 5
0.000000 0.000000 0.000000 ( 5.001130)
Output => -4338026385995737381 :: -129914481430179661 :: 5
0.000000 0.000000 0.000000 ( 0.000087)
Output => -4338026385995737381 :: -129914481430179661 :: 5
0.000000 0.000000 0.000000 ( 0.000075)
# -*- encoding: utf-8 -*-
module SimpleCache
def self.included(base)
base.class_eval do
def self.cache
@_class_cache ||= Pool.new(self)
end
def cache
@_instance_cache ||= Pool.new(self)
end
end
end
class Pool
def initialize(host)
@host = host
@pool = Hash.new
end
def method_missing(method, *args)
args_key = Marshal.dump(args)
key = "#{method}__#{args_key.to_s}"
@pool[key] ||= @host.send(method.to_sym, *args)
end
end
end
# -*- encoding: utf-8 -*-
require File.expand_path(File.dirname(__FILE__) + '/simple_cache')
require 'benchmark'
class Output
attr_reader :time, :owner
def initialize(time, owner)
@time = time
@owner = owner
end
def to_s(*)
"Output => #{owner} :: #{hash} :: #{time}"
end
end
class Test
include SimpleCache
def self.expensive(time = 4)
sleep time
Output.new(time, hash)
end
def expensive(time = 3)
sleep time
Output.new(time, hash)
end
end
puts "Class cache"
puts Benchmark.measure { puts Test.cache.expensive }
puts Benchmark.measure { puts Test.cache.expensive }
puts Benchmark.measure { puts Test.cache.expensive }
puts Benchmark.measure { puts Test.cache.expensive(6) }
puts Benchmark.measure { puts Test.cache.expensive(6) }
puts Benchmark.measure { puts Test.cache.expensive(6) }
puts "\n"
puts "Instance cache"
t = Test.new
puts Benchmark.measure { puts t.cache.expensive }
puts Benchmark.measure { puts t.cache.expensive }
puts Benchmark.measure { puts t.cache.expensive }
puts Benchmark.measure { puts t.cache.expensive(5) }
puts Benchmark.measure { puts t.cache.expensive(5) }
puts Benchmark.measure { puts t.cache.expensive(5) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment