zoukankan      html  css  js  c++  java
  • 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();
    }
    

      

  • 相关阅读:
    fiximulator
    Maven POM 模板[z]
    Sentinel 实战-控制台篇[z]
    Sentinel 实战-限流篇[z]
    RPC 框架
    如何手写实现简易的Dubbo[z]
    降级、熔断、限流[z]
    Transfix
    Memcached和Memcache安装(64位win7)[z]
    Oracle分区表例子
  • 原文地址:https://www.cnblogs.com/ahuo/p/6046630.html
Copyright © 2011-2022 走看看