Working With Node.js and Redis

jieforest發表於2012-05-28
In my previous post I showed you how to install and use Redis with Node.js. Today I’m going to take that a step further and walk through some of the things you can do with node_redis using Redis’s TTL and EXPIRE commands.

Note: If you haven’t gone through my previous article make sure to do that now as I’ll assume you have Node.js and Redis up and running.

Create a new folder and put a new text file in it called: app.js

Inside the app.js file we will add some simple code to set a value that doesn’t have a time to live (or expiration on it):

CODE:

var redis = require("redis")
    , client = redis.createClient();

client.on("error", function (err) {
    console.log("Error " + err);
});

client.on("connect", runSample);

function runSample() {
    // Set a value
    client.set("string key", "Hello World", function (err, reply) {
        console.log(reply.toString());
    });
    // Get a value
    client.get("string key", function (err, reply) {
        console.log(reply.toString());
    });
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-731262/,如需轉載,請註明出處,否則將追究法律責任。

相關文章