nodejs gearman redis

export NODE_PATH=/root/gearman-1.1.2/node_modules
npm install gearmanode
npm install redis


w.js
var redis = require("redis");
var gearmanode = require('gearmanode');
var worker = gearmanode.worker();

//添加gearman函數syncToRedis
//當MySQL表記錄更改時,此函數會被調用
worker.addFunction('syncToRedis', function (job) {
    job.sendWorkData(job.payload);
    console.log("-------job.payload: " + job.payload.toString());
    //將字符串轉換成json object, 而後調用更新redis
    updateRedis(eval('(' + job.payload.toString() + ')'));
    job.workComplete("Successed!");
});

//些函數只是簡單的將MySQL表中的一行的記錄按單個字段更新到redis中。可根據實際狀況自行擴展
function updateRedis(json)
{
    var client = redis.createClient(6379,"192.168.1.20",{});
    client.on("error", function (err) {
        console.log("Error " + err);
    });
    for(var key in json)
    {
        client.set(key, json[key], redis.print);
	console.log(key + ":"+json[key]);
    }
    client.quit();
}
相關文章
相關標籤/搜索