7.2 Simple Redis Commands
Let's start practicing using the redis key-value store from our node application.
Task 1:
Require the
Task 2:
Create a
Task 3:
On the
```js var redis = require('redis'); var client = redis.createClient(); client.set('name', 'Answer'); ```
7.3 Get A Key
We have already stored a value in the question key. Use the redis client to issue a
Task 1:
Use the redis client to issue a
Task 2:
Log the value retrieved with
```js var redis = require('redis'); var client = redis.createClient(); client.get('question', function(err, data){ console.log(data); }); ```
7.4 Working With Lists 1
As we saw in the video, redis can do more than just simple key-value pairs. We are going to be using redis' LISTS later to add persistence to our live-moderation app, so let's practice using them now.
Task 1:
Using the redis client's
Task 2:
Using the redis client's
```js var redis = require('redis'); var client = redis.createClient(); var question1 = "Where is the dog?"; var question2 = "Where is the cat?"; client.lpush('questions', question1, function(err, data){ console.log(data); }); client.lpush('questions', question2, function(err, data){ console.log(data); }); ```
7.5 Working With Lists 2
Now that we have seeded the
Task 1:
Use the
Task 2:
Now that we have called
```js var redis = require('redis'); var client = redis.createClient(); client.lrange('questions', 0, -1, function(err, data){ console.log(data); }); ```
7.6 Persisting Questions
Let's go back to our live-moderation app and add some persistence, first to the questions people ask.
Task:
Use the
```js var express = require('express'); var app = express(); var server = require('http').createServer(app); var socket = require('socket.io'); var io = socket.listen(server); var redis = require('redis'); var redisClient = redis.createClient(); io.sockets.on('connection', function(client) { client.on('answer', function(question, answer) { client.broadcast.emit('answer', question, answer); }); client.on('question', function(question) { if(!client.question_asked) { client.question_asked = true; client.broadcast.emit('question', question); redisClient.lpush('questions', question); } }); }); ```
7.7 Emitting Stored Questions
Now that we have questions stored in redis, let's emit them whenever a new client connects to the server through socket.io.
Task 1:
Use the
Task 2:
Inside of the
```js var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io').listen(server); var redis = require('redis'); var redisClient = redis.createClient(); io.sockets.on('connection', function(client) { redisClient.lrange('questions', 0, -1, function(err, questions){ questions.forEach(function(question){ client.emit("question", question); }); }); client.on('answer', function(question, answer) { client.broadcast.emit('answer', question, answer); }); client.on('question', function(question) { if(!client.question_asked) { client.question_asked = true; client.broadcast.emit('question', question); redisClient.lpush("questions", question); } }); }); ```
7.8 Limiting Questions Stored
Great work! One last thing though, since every time a new question comes in we store it in the questions list, we might run into a problem where there are just too many questions stored in that list.
Task 1:
Add a callback to
Task 2:
Use the
```js var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io').listen(server); var redis = require('redis'); var redisClient = redis.createClient(); io.sockets.on('connection', function(client) { redisClient.lrange("questions", 0, -1, function(err, questions) { questions.forEach(function(question) { client.emit("question", question); }); }); client.on('answer', function(question, answer) { client.broadcast.emit('answer', question, answer); }); client.on('question', function(question) { if(!client.question_asked) { client.question_asked = true; client.broadcast.emit('question', question); redisClient.lpush("questions", question, function(){ redisClient.ltrim('questions', 0, 19); }); } }); }); ```
Thanks For Your valuable posting, it was very informative...
回覆刪除Node js Developer
Quality SEO is a crucial investment when it comes to affordable SEO service In today's times almost every business is moving online and it pays to do so because with internet access gaining wider acceptability and penetration,
回覆刪除