1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00

Organize things a little.

This commit is contained in:
Matt Ranney
2010-09-24 13:15:37 -07:00
parent 29f10cd593
commit 047d367f8c
7 changed files with 68 additions and 0 deletions

17
examples/example.js Normal file
View File

@@ -0,0 +1,17 @@
var redis = require("redis"),
client = redis.createClient();
client.on("error", function (err) {
console.log("Redis connection error to " + client.host + ":" + client.port + " - " + err);
});
client.set("string key", "string val", redis.print);
client.hset("hash key", "hashtest 1", "some value", redis.print);
client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
client.hkeys("hash key", function (err, replies) {
console.log(replies.length + " replies:");
replies.forEach(function (reply, i) {
console.log(" " + i + ": " + reply);
});
client.quit();
});

28
examples/example_auth.js Normal file
View File

@@ -0,0 +1,28 @@
var redis = require("./index"),
client = redis.createClient();
redis.debug_mode = true;
client.on("connect", function () {
client.auth("somepass", redis.print);
client.sadd("bigset", "some shit");
client.sadd("bigset", "some other shit");
client.sadd("bigset", 1);
client.sadd("bigset", 2);
client.sadd("bigset", 3);
client.sadd("bigset", 4);
client.multi([
["smembers", ["bigset"], function (err, res) {
console.log("1: " + res.toString());
}]// ,
// ["smembers", ["bigset"], function (err, res) {
// console.log("2: " + res.toString());
// }],
// ["smembers", ["bigset"], function (err, res) {
// console.log("3: " + res.toString());
// }]
]);
// client.smembers("bigset", redis.print);
});

30
examples/example_file.js Normal file
View File

@@ -0,0 +1,30 @@
var redis = require("redis"),
client = redis.createClient(),
fs = require("fs"),
filename = "kids_in_cart.jpg";
// Get the file I use for testing like this:
// curl http://ranney.com/kids_in_cart.jpg -o kids_in_cart.jpg
// or just use your own file.
// Read a file from fs, store it in Redis, get it back from Redis, write it back to fs.
fs.readFile(filename, function (err, data) {
if (err) throw err
console.log("Read " + data.length + " bytes from filesystem.");
client.set(filename, data, redis.print); // set entire file
client.get(filename, function (err, reply) { // get entire file
if (err) {
console.log("Get error: " + err);
} else {
fs.writeFile("duplicate_" + filename, reply, function (err) {
if (err) {
console.log("Error on write: " + err)
} else {
console.log("File written.");
}
client.end();
});
}
});
});

40
examples/example_multi.js Normal file
View File

@@ -0,0 +1,40 @@
var redis = require("./index"),
client = redis.createClient(), set_size = 20;
client.sadd("bigset", "a member");
client.sadd("bigset", "another member");
while (set_size > 0) {
client.sadd("bigset", "member " + set_size);
set_size -= 1;
}
// multi chain with an individual callback
client.multi()
.scard("bigset")
.smembers("bigset")
.keys("*", function (err, replies) {
client.mget(replies, redis.print);
})
.dbsize()
.exec(function (err, replies) {
console.log("MULTI got " + replies.length + " replies");
replies.forEach(function (reply, index) {
console.log("Reply " + index + ": " + reply.toString());
});
});
client.set("incr thing", 100);
// start a separate command queue for multie
var multi = client.multi();
multi.incr("incr thing", redis.print);
multi.incr("incr other thing", redis.print);
// runs immediately
client.get("incr thing", redis.print); // 100
// drains multi queue and runs atomically
multi.exec(function (err, replies) {
console.log(replies); // 101, 3
});

View File

@@ -0,0 +1,33 @@
var redis = require("redis"),
client1 = redis.createClient(), msg_count = 0,
client2 = redis.createClient();
redis.debug_mode = false;
client1.on("subscribe", function (channel, count) {
console.log("client1 subscribed to " + channel + ", " + count + " total subscriptions");
if (count === 2) {
client2.publish("a nice channel", "I am sending a message.");
client2.publish("another one", "I am sending a second message.");
client2.publish("a nice channel", "I am sending my last message.");
}
});
client1.on("unsubscribe", function (channel, count) {
console.log("client1 unsubscribed from " + channel + ", " + count + " total subscriptions");
if (count === 0) {
client2.end();
client1.end();
}
});
client1.on("message", function (channel, message) {
console.log("client1 channel " + channel + ": " + message);
msg_count += 1;
if (msg_count === 3) {
client1.unsubscribe();
}
});
client1.incr("did a thing");
client1.subscribe("a nice channel", "another one");