Permalink
Browse files

increment counter via Lua

  • Loading branch information...
0 parents commit 608565976bb86f68451602646565d851c081eaee @igrigorik committed Jul 7, 2009
Showing with 62 additions and 0 deletions.
  1. +23 −0 increment-command/README.rdoc
  2. +20 −0 increment-command/incr.lua
  3. +19 −0 increment-command/incr.rb
23 increment-command/README.rdoc
@@ -0,0 +1,23 @@
+= Increment command
+
+Implements a simple incr command on top of Tokyo Cabinet via a Lua Extension.
+
+== Starting server with incr extension
+ > ttserver -ext incr.lua test.tch
+
+== Executing from command line
+ > tcrmgr ext localhost incr keyname 2
+ > 2
+ > tcrmgr ext localhost incr keyname 2
+ > 4
+
+== Executing via Ruby
+
+ > ruby incr.rb
+ > 2
+ > 4
+ > 6
+ > 8
+ > 9
+ > 10
+
20 increment-command/incr.lua
@@ -0,0 +1,20 @@
+--
+-- incr.lua
+--
+function incr (key, i)
+ i = tonumber(i)
+ if not i then
+ return nil
+ end
+
+ local old = tonumber(_get(key))
+ if old then
+ i = old + i
+ end
+
+ if not _put(key, i) then
+ return nil
+ end
+
+ return i
+end
19 increment-command/incr.rb
@@ -0,0 +1,19 @@
+require 'rubygems'
+require 'rufus/tokyo/tyrant' # sudo gem install rufus-tokyo
+
+t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 1978)
+
+5.times do
+ puts t.ext(:incr, 'my-counter', 2).to_i
+end
+
+t.close
+
+__END__
+
+[ilya@igvita increment-command]# ruby ruby-incr.rb
+2
+4
+6
+8
+10

0 comments on commit 6085659

Please sign in to comment.