Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simple session trail logger (~2000 req/s)
- Loading branch information
Showing
4 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
= Simple Session trail database | ||
|
||
Tracks user ids and visited user resources by timestamping each interaction. The list of all visits can then be retrieved by querying for a user id. | ||
|
||
== Starting server with incr extension | ||
> ttserver -ext session-trail.lua test.tch | ||
|
||
== Executing from command line | ||
> tcrmgr ext localhost add 1 123 | ||
> tcrmgr ext localhost add 1 256 | ||
> tcrmgr ext localhost add 1 987 | ||
> tcrmgr ext localhost add 2 987 | ||
> tcrmgr ext localhost list 1 | ||
987 1247008220 | ||
256 1247008216 | ||
123 1247008123 | ||
|
||
== Executing via Ruby | ||
|
||
> ruby session-trail.rb | ||
1 | ||
2 | ||
3 | ||
1 | ||
343 1247008682 | ||
253 1247008681 | ||
123 1247008680 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
MAXPRINT = 60 | ||
|
||
function add(key, value) | ||
key = tonumber(key) | ||
value = tonumber(value) | ||
if not key or not value or key == value then | ||
return nil | ||
end | ||
local ksel = _pack("i", key) | ||
local time = os.time() | ||
local date = os.date("*t", time) | ||
date.hour = 0 | ||
date.min = 0 | ||
date.sec = 0 | ||
local mintime = os.time(date) | ||
local vsel | ||
local ary = _unpack("i*", _get(ksel)) | ||
local anum = 1 | ||
if ary and #ary > 0 then | ||
local nary = {} | ||
local nidx = 1 | ||
local nidxmax = MAXPRINT * 2 - 1 | ||
for i = 1, #ary, 2 do | ||
if ary[i] ~= value or ary[i+1] < mintime then | ||
nary[nidx] = ary[i] | ||
nary[nidx+1] = ary[i+1] | ||
nidx = nidx + 2 | ||
end | ||
end | ||
vsel = _pack("i*", nary, value, time) | ||
anum = (#nary / 2) + 1 | ||
if anum > MAXPRINT then | ||
vsel = string.sub(vsel, MAXPRINT * -8) | ||
anum = MAXPRINT | ||
end | ||
else | ||
vsel = _pack("ii", value, time) | ||
end | ||
if not _put(ksel, vsel) then | ||
return nil | ||
end | ||
return anum | ||
end | ||
|
||
function list(key, value) | ||
key = tonumber(key) | ||
value = tonumber(value) | ||
if not key then | ||
return nil | ||
end | ||
if not value or value < 1 then | ||
value = MAXPRINT | ||
end | ||
local result = "" | ||
local ksel = _pack("i", key) | ||
local ary = _unpack("i*", _get(ksel)) | ||
if ary and #ary > 0 then | ||
for i = #ary - 1, 0, -2 do | ||
if value < 1 then | ||
break | ||
end | ||
result = result .. ary[i] .. "\t" .. ary[i+1] .. "\n" | ||
value = value - 1 | ||
end | ||
end | ||
return result | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'rubygems' | ||
require 'rufus/tokyo/tyrant' # sudo gem install rufus-tokyo | ||
|
||
t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 1978) | ||
|
||
puts t.ext(:add, 1, 123); sleep 1 | ||
puts t.ext(:add, 1, 253); sleep 1 | ||
puts t.ext(:add, 1, 343); sleep 1 | ||
puts t.ext(:add, 2, 123); sleep 1 | ||
|
||
puts t.ext(:list, 1, '') | ||
|
||
t.close |