memodis

semi-transparent memoization; backed by redis;

writes are sent to the master, reads are sent to an approriate slave.

Two features:

  1. Memodis#memoize

  2. Memodis::DistCache

Example

def fib(num)
  return num if num < 2
  fib(num - 1) + fib(num - 2)
end

puts "Before memoize: "
puts fib(33) => 3524578 # after ~ 7 seconds

extend Memodis
memoize :fib, Memodis::DistCache.new({
  :key_gen => lambda { |k| "fib(#{k})" },
  :decoder => :integer,
  :expires => (10 * 60),
  :master  => '127.0.0.1:16379 127.0.0.1:16380'.split,
  :slaves  => '127.0.0.1:16389 127.0.0.1:16390 
               127.0.0.1:16391 127.0.0.1:16392
               127.0.0.1:16393 127.0.0.1:16394'.split
})

puts "After memoize: "
puts fib(33) => 3524578 # after ~ 0.03   seconds
puts fib(33) => 3524578 # after ~ 0.0001 seconds
puts fib(33) => 3524578 # after ~ 0.0001 seconds

Background

  1. blog.grayproductions.net/articles/caching_and_memoization

  2. code.google.com/p/redis & github.com/ezmobius/redis-rb

Important Moving Parts

  1. code.google.com/p/redis/wiki/GetCommand

  2. code.google.com/p/redis/wiki/SetCommand

  3. code.google.com/p/redis/wiki/SetnxCommand

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 [email protected]. See LICENSE for details.