Chapter 7: initial samples

This commit is contained in:
Beth Griggs
2020-05-16 14:33:26 +01:00
parent bcf782f7dc
commit adb2130b38
140 changed files with 1777 additions and 1960 deletions
+42
View File
@@ -0,0 +1,42 @@
{
"name": "redis-app",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"denque": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz",
"integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ=="
},
"redis": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/redis/-/redis-3.0.2.tgz",
"integrity": "sha512-PNhLCrjU6vKVuMOyFu7oSP296mwBkcE6lrAjruBYG5LgdSqtRBoVQIylrMyVZD/lkF24RSNNatzvYag6HRBHjQ==",
"requires": {
"denque": "^1.4.1",
"redis-commands": "^1.5.0",
"redis-errors": "^1.2.0",
"redis-parser": "^3.0.0"
}
},
"redis-commands": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.5.0.tgz",
"integrity": "sha512-6KxamqpZ468MeQC3bkWmCB1fp56XL64D4Kf0zJSwDZbVLLm7KFkoIcHrgRvQ+sk8dnhySs7+yBg94yIkAK7aJg=="
},
"redis-errors": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",
"integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60="
},
"redis-parser": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz",
"integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=",
"requires": {
"redis-errors": "^1.0.0"
}
}
}
}
+15
View File
@@ -0,0 +1,15 @@
{
"name": "redis-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"redis": "^3.0.2"
}
}
+37
View File
@@ -0,0 +1,37 @@
const redis = require('redis')
const client = redis.createClient({
port: 6380,
password: "PASSWORD"
})
const task = process.argv[2]
client.on('error', (err) => {
console.log("Error:", err)
})
if (!task) {
listTasks()
} else {
addTask(task)
}
function addTask(task) {
const key = `Task: ${Math.random().toString(32).replace('.', '')}`
client.hmset(key, {
task
})
listTasks()
}
function listTasks() {
client.keys('Task:*', (err, keys) => {
if (err) throw err
keys.forEach(key => {
client.hgetall(key, (err, task) => {
if (err) throw err
console.log(task)
})
})
client.quit()
})
}
+34
View File
@@ -0,0 +1,34 @@
const redis = require('redis')
const client = redis.createClient()
const task = process.argv[2]
client.on('error', (err) => {
console.log("Error:", err)
})
if (!task) {
listTasks()
} else {
addTask(task)
}
function addTask(task) {
const key = `Task: ${Math.random().toString(32).replace('.', '')}`
client.hmset(key, {
task
})
listTasks()
}
function listTasks() {
client.keys('Task:*', (err, keys) => {
if (err) throw err
keys.forEach(key => {
client.hgetall(key, (err, task) => {
if (err) throw err
console.log(task)
})
})
client.quit()
})
}