Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

record expiry (TTL) with Lua and peridic script invocation

  • Loading branch information...
commit 7d6e8b1344429da56124b22bff2942a418731905 1 parent 2f1dbb3
@igrigorik authored
Showing with 45 additions and 0 deletions.
  1. +19 −0 expire/README.rdoc
  2. +12 −0 expire/expire.lua
  3. +14 −0 expire/expire.rb
View
19 expire/README.rdoc
@@ -0,0 +1,19 @@
+= Auto record expire based on TTL
+
+Implements TTL-based record expiry via a Lua script which is periodically executed by the TC runtime.
+ - The calling period is customizable via -extpc flag
+ - Data is stored inside a 'table' engine where key 'x' stores the unix timestamp for the expiry date
+ - Lua cleanup script is called periodically to scan the database for keys ready to be removed, which are then purged from the DB.
+
+== Starting server
+ # call expire script every 5 seconds
+ > ttserver -ext expire.lua -extpc expire 5 "casket.tct#idx=x:dec#bnum=2000000"
+
+== Executing via Ruby
+
+ > ruby expire.rb
+ > nil
+ > {"name"=>"bob", "x"=>"1247025691"}
+
+Source: http://alpha.mixi.co.jp/blog/?p=862
+Source: http://alpha.mixi.co.jp/blog/?p=318
View
12 expire/expire.lua
@@ -0,0 +1,12 @@
+function expire()
+ local args = {}
+ local cdate = string.format("%d", _time())
+ table.insert(args, "addcond\0x\0NUMLE\0" .. cdate)
+ table.insert(args, "out")
+ local res = _misc("search", args)
+ if not res then
+ _log("expiration was failed")
+ end
+ print("rnum=" .. _rnum() .. " size=" .. _size())
+end
+
View
14 expire/expire.rb
@@ -0,0 +1,14 @@
+require 'rubygems'
+require 'rufus/tokyo/tyrant' # sudo gem install rufus-tokyo
+
+t = Rufus::Tokyo::TyrantTable.new('127.0.0.1', 1978)
+
+t['key1'] = { 'name' => 'alfred', 'x' => (Time.now.to_i + 2).to_s }
+t['key2'] = { 'name' => 'bob', 'x' => (Time.now.to_i + 5).to_s }
+
+sleep(3)
+
+p t['key1'] # expired
+p t['key2'] # valid
+
+t.close
Please sign in to comment.
Something went wrong with that request. Please try again.