Permalink
Browse files

recipe for dynamically loading extensions at runtime

  • Loading branch information...
1 parent 3cc1202 commit b0ff1d971d2c9626fdb1bfbc7577595905f1920d @igrigorik committed Jul 31, 2009
Showing with 44 additions and 0 deletions.
  1. +22 −0 dynamic-extension/README.rdoc
  2. +12 −0 dynamic-extension/dynamic.rb
  3. +10 −0 dynamic-extension/eval.lua
@@ -0,0 +1,22 @@
+= Dynamically loading extensions into Tokyo Cabinet
+
+Guide for loading extensions at runtime, instead of specifying them at startup time.
+
+Extensions can be eval'ed at runtime. In this example we'll define and redefine a hello function.
+
+== Starting server with eval extension
+ > ttserver -ext eval.lua test.tch
+
+== Executing from command line
+ > tcrmgr ext localhost eval 'function hello() return "hello" end'
+ > tcrmgr ext localhost hello
+ hello
+ > tcrmgr ext localhost eval 'function hello() return "bye" end'
+ > tcrmgr ext localhost hello
+ bye
+
+== Executing via Ruby
+
+ > ruby dynamic.rb
+ > hello
+ > bye
@@ -0,0 +1,12 @@
+require 'rubygems'
+require 'rufus/tokyo/tyrant' # sudo gem install rufus-tokyo
+
+t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 1978)
+
+t.ext('eval', 'function hello() return "hello" end', '')
+puts t.ext(:hello, '', '')
+
+t.ext('eval', 'function hello() return "bye" end', '')
+puts t.ext(:hello, '', '')
+
+t.close
View
@@ -0,0 +1,10 @@
+--
+-- eval.lua
+--
+
+function eval(key, value)
+ if not _eval(key) then
+ return nil
+ end
+ return "ok"
+end

0 comments on commit b0ff1d9

Please sign in to comment.