Skip to content

Instantly share code, notes, and snippets.

@smly
Created February 29, 2012 00:45
Show Gist options
  • Select an option

  • Save smly/1936603 to your computer and use it in GitHub Desktop.

Select an option

Save smly/1936603 to your computer and use it in GitHub Desktop.
LOL_redis_scripting.rb
require 'redis'
r = Redis.new
LuaScript = <<EOF
function fib(n)
if n < 2 then return n end
local a = redis.call('hget',KEYS[1],n-1) or fib(n-1)
local b = redis.call('hget',KEYS[1],n-2) or fib(n-2)
local ret = a + b
redis.call('hset',KEYS[1],n,ret)
return ret
end
return fib(tonumber(ARGV[1]))
EOF
puts r.eval(LuaScript,1,:fibcache,ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment