nodejs读取memcache示例分享_javascript技巧

复制代码 代码如下:

var memcache = require('memcache')
    , http = require('http')
    , url = require('url')
    , qs = require('querystring')
    , memsettings = { port: 2000, host: '10.6.0.6' }
    , httpsettings = { port: 3000 }
    , cacheObject = {}
    , httpserver
    , client
    , requestHandler;

client = new memcache.Client(memsettings.port, memsettings.host);
//...
client.on('connect', function(){
    client.connected = true;
});

client.on('close', function(){
    // no arguments - connection has been closed
    client.connected = false;
});

function getData(key, wrapper, res){
    client.get(key, function(error, result){
            // all of the callbacks have two arguments.
            // 'result' may contain things which aren't great, but
            // aren't really errors, like 'NOT_STORED'
            if(!error){
                if(result){
                    cacheObject[key] = result;
                    res.write( wrapper + '(' + result + ')');
                } else {
                    res.write('no data');
                }
                res.end();
            }
     });
    /*if(cacheObject[key]){
        response.write(cacheObject[key]);
        response.end();
    } else  {
        client.get(key, function(error, result){

            // all of the callbacks have two arguments.
            // 'result' may contain things which aren't great, but
            // aren't really errors, like 'NOT_STORED'
            if(!error){
                cacheObject[key] = result;
                cacheObject[key]['timeoutId'] = setTimeout(function(){
                    client.get(key, function(error, result){
                        if(!error){
                            cacheObject[key] = result;
                        }
                    });
                });
                response.write(result);
                response.end();
            }
        });
    }*/
}

httpserver = http.createServer(function(req, res){
    var urlObj, queryObj;
    urlObj = url.parse(req.url);
    res.writeHead(200, {"Content-Type":"text/javascript; charset=utf-8"});
    queryObj = qs.parse(urlObj.query);
    path = queryObj.pathname;
    if(queryObj.gid){
        getData(queryObj.gid, queryObj.callback||'callback', res);
    } else {
        res.write('no input');
        res.end();
    }
    /*
    switch(urlObj.pathname){
        case '/pai':
            res.write(urlObj.pathname);
        break;
        case '/qiang':
            res.write(urlObj.pathname);
        break;
        default:
            res.write('default');
        break;
    }
    res.end();
    */
});

client.connect();
httpserver.listen(httpsettings.port);
console.log('http server listening on ' + httpsettings.port);

时间: 2024-09-29 22:17:04

nodejs读取memcache示例分享_javascript技巧的相关文章

JavaScript实现图片滑动切换的代码示例分享_javascript技巧

假设我们这里有1到5五张bmp图片,那么控制图片切换显示的核心代码可以为: <script> var i=1; var img = new Array(); img[0] = "1.bmp"; img[1] = "2.bmp"; img[2] = "3.bmp"; img[3] = "4.bmp"; img[4] = "5.bmp"; function playImg(){ i=i+1; var

yepnope.js使用详解及示例分享_javascript技巧

yepnope.js的一个典型实例: yepnope({ test : Modernizr.geolocation, yep : 'normal.js', nope : ['polyfill.js', 'wrapper.js'] }); 此实例表示如果Modernizr.geolocation为真的时候加载normal.js,为假则加载polyfill.js及wrapper.js. yepnope完整语法: yepnope([{ test : /* boolean(ish) 输入条件 */, y

固定背景实现的背景滚动特效示例分享_javascript技巧

分享一个来自corpse的固定背景滚动特效,使用background-attachment: fixed和导航菜单,页面会非常平滑的滚动. HTML 复制代码 代码如下: <div id="cbp-fbscroller" class="cbp-fbscroller"> <nav> <a href="#fbsection1" class="cbp-fbcurrent">Section 1<

javascript 事件处理示例分享_javascript技巧

废话少说,直接奉上示例代码: 复制代码 代码如下: <script type="text/javascript">     function EventUtil() {         var _self = this;         ///添加事件         var addEvent = (function () {             if (document.addEventListener) {                 return functi

自己实现ajax封装示例分享_javascript技巧

复制代码 代码如下:  //javascript Object: ajax Object//Created By RexLeefunction Ajax(url,data){    this.url=url;    this.data=data;    this.browser=(function(){          if(navigator.userAgent.indexOf("MSIE")>0) {              return "MSIE"

JavaScript阻止事件冒泡示例分享_javascript技巧

之前遇到事件冒泡的问题,也去网上搜索了一番,结果大部分都是一样的代码,贴来用时也不太顺利.涉及到FF时,可以用 e.stopPropagation(); ,不知怎地我没用成功.不过我发现FF支持e.cancelBubble = true;的写法,经测试可行.就把代码贴在这里吧,省得以后到处找.IE以前版本的兼容性还没测试,用到时再完善吧.   复制代码 代码如下: //取消事件冒泡 function stopBubble(e) {     var evt = (e) ? e : window.e

JavaScript原型链示例分享_javascript技巧

复制代码 代码如下: <mce:script type="text/javascript"><!--/*   每个对象实例都有个属性成员用于指向到它的instanceof 对象(暂称为父对象)的原型(prototype)   我们把这种层层指向父原型的关系称为[原型链 prototype chian]   原型也具有父原型,因为它往往也是一个对象实例,除非我们人为地去改变它   在JavaScript中,"一切都是对象,函数是第一型."   Fun

对table和ul实现js分页示例分享_javascript技巧

复制代码 代码如下: (function($) { $.fn.tablepage = function(oObj, dCountOfPage, fresh_id) {  var dPageIndex = 1;  var dNowIndex = 1;  var sPageStr = "";  var dCount = 0;  var oSource = $(this);  var sNoSelColor = "#CCCCCC";  var sSelColor = &q

可自定义速度的js图片无缝滚动示例分享_javascript技巧

思路: 一组图片 控制它的滚动条进行滚动 且此时对这组图片进行复制并添加进原图片组中,现在就有两组图片了.你可以想象一下,现在滚动条继续滚动,原来那组图片最后一张图片已经滚至顶端且消失,复制的那组图片的第一张跟在原图最后一张图片后出现,此时你就能感觉到无缝滚动了. 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/D