2012-09-05 20 views
0

TI已排序的設置是這樣的:Redis的:「多批量回復」 - > LUA表

|key |score |member 
zadd mykey 100 event:1 
zadd mykey 101 event:2 
zadd mykey 102 event:3 

現在我用一個Lua腳本返回一個子集從給定的分數範圍

var result = redis.call('zrangebyscore', 'mykey', start, stop, 'WITHSCORES') 

什麼結果表看起來像在lua腳本? (因爲我不能調試,我不得不問)

redis-doc:Redis多批量答覆 - > Lua表(可能有其他Redis數據類型嵌套) - 這是我可以找到的所有信息。

它會像:

result = { 'event:1', 100, 'event:2', 101, 'event:3', 102 } 

或類似

result = { 100, 'event:1', 101, 'event:2', 102, 'event:3' } 

或不同?

回答

1

假設您正在使用的內容可以寫入標準輸出,您可以添加一個轉儲函數並輸出表格格式。

我用

function dump (tt, label,indent, done) 
    if debug == true then 
     if label == nil then 
      label = 'Dump' 
     end 
     done = done or {} 
     indent = indent or 0 
     if type(tt) == "table" then 
      if indent == 0 then 
       io.write(string.rep (" ", indent)) 
       io.write(label..'\n') 
      end 
      for key, value in pairs (tt) do 
       io.write(string.rep (" ", indent)) -- indent it 
       if type (value) == "table" and not done [value] then 
        done [value] = true 
        io.write(string.format("[%s] => table\n", tostring (key))); 
        io.write(string.rep (" ", indent+4)) -- indent it 
        io.write("(\n"); 
        dump (value, tostring(key),indent + 7, done) 
        io.write(string.rep (" ", indent+4)) -- indent it 
        io.write(")\n"); 
       else 
        io.write(string.format("[%s] => %s\n", 
        tostring (key), tostring(value))) 
       end 
      end 
     else 
      io.write(tostring(label)..':'..tostring(tt)) 
     end 
    else 
     return 
    end 
end 

因此,對於你的榜樣,你會簡單地添加

debug = true 
dump(var,'redis-table') 
+0

不幸的是,'io'模塊不可用。有'redis.log'和下面的庫:base,table,string,math,debug,cjson,cmsgpack。 –

+0

查看redis的幫助,應該可以用redis.log(redis.LOG_VERBOSE,value)替換io.write。雖然結果格式不會很好 –

+0

''redis.log(redis.LOG_VERBOSE,cjson.encode(result))''可能更容易......但是沒關係,因爲答案如下。但提問者似乎已經放棄了我們。 ;) –

2

結果從zrangebyscore ... withscores - 或從有序集合得分在任何讀,真的 - 會返回作爲一個盧阿表看起來像這樣:

result = { "event:1", "100", "event:2", "101" } 

價值第一,得分第二。價值觀和分數都是字符串。