• node.js应用Redis数据库


    node.js下使用Redis,首先:

    1、有一台安装了Redis的服务器,当然,安装在本机也行

    2、本机,也就是客户端,要装node.js

    3、项目要安装nodejs_redis模块

    注意第 3 点,不是在本机安装就行了,而是说,要在项目中安装(引用)。

    方法是,DOS窗口,在项目目录下,输入

    npm install redis

    这样就将nodejs_redis下载一份,放到当前目录下了。看看,多了一个文件夹:node_modules edis

    编写以下代码,保存到当前目录下hello.js

    var redis = require("redis"),//召唤redis
    /*
    	连接redis数据库,createClient(port,host,options);
    	如果REDIS在本机,端口又是默认,直接写createClient()即可
    	redis.createClient() = redis.createClient(6379, '127.0.0.1', {})
    */
    client = redis.createClient(6379,'192.168.159.128',{});
    //如果需要验证,还要进行验证
    //client.auth(password, callback);
    
    // if you'd like to select database 3, instead of 0 (default), call
    // client.select(3, function() { /* ... */ });
    
    //错误监听?
    client.on("error", function (err) {
        console.log("Error " + err);
    });
    
    client.set("string key", "string val", redis.print);//set "string key" "string val"
    /*
    	redis.print,回调函数,将redis的返回值显示出来。上一句执行结果,将返回“OK”	
    */
    client.hset("hash key", "hashtest 1", "some value", redis.print);
    client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
    //遍历哈希表"hash key"
    client.hkeys("hash key", function (err, replies) {
        console.log(replies.length + " replies:");
        replies.forEach(function (reply, i) {
            console.log("    " + i + ": " + reply);
        });
    client.hget("hash key","hashtest 1",redis.print);    
    
    /*两种都可以断掉与redis的连接,
    end()很粗暴,不管3721,一下子退出来了,上面那句获取哈希表"hash key"的某个元素值的表达式将没有结果返回
    而quit()则是先将语句处理完毕再干净地退出,斯文得很
    */
    //client.end();
    client.quit();
    });
    

      

  • 相关阅读:
    UVA 11605 Lights inside a 3d Grid
    UVA 10288 Coupons
    UVA 11637 Garbage Remembering Exam
    阿里上市全解读【转载】
    C# 创建系统服务并定时执行【转载】
    Ehcache 整合Spring 使用页面、对象缓存
    详解 Tomcat 的连接数与线程池(转)
    Mysql主从热备
    centos上yum安装异常处理
    tomcat运行模式APR安装
  • 原文地址:https://www.cnblogs.com/pangguoming/p/4292181.html
Copyright © 2020-2023  润新知