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(); }