LoginSignup
3
4

More than 5 years have passed since last update.

Redis上でlua scriptを利用して結果を利用して、別の値を取得する方法

Posted at

前提条件

  • Redis 3.2-rc3
  • Node.js 5.9.1
  • npm
    • node_redis 2.6.0-0

ソースコード

geo.lua
local keys = redis.call("GEORADIUS", KEYS[1], ARGV[1], ARGV[2], ARGV[3], ARGV[4])
local values = redis.call("MGET", unpack(keys))
local result = {}
for i, key in ipairs(keys) do
  table.insert(result, key)
  table.insert(result, values[i])
end
return result
index.js
'use strict'

let redis = require("redis")
  , fs = require('fs')
  , client = redis.createClient()
  ;

client.set('Palermo', 'PalermoValue');

client.set('Catania', 'CataniaValue');

client.geoadd('Sicily', 13.361389, 38.115556, 'Palermo', 15.087269, 37.502669, 'Catania');

let script = fs.readFileSync('./geo.lua');
client.eval(script, 1, 'Sicily', 15, 37, 200, 'km', function (err, reply) {
    console.log(reply.toString());
    client.quit();
});

実行結果

Palermo,PalermoValue,Catania,CataniaValue

参考資料

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4