You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-09 00:22:08 +03:00
doc: remove outdated examples
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const redis = require('redis')
|
||||
// The client stashes the password and will re-authenticate on every connect.
|
||||
redis.createClient({
|
||||
password: 'some pass'
|
||||
})
|
@@ -1,14 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const redis = require('../index')
|
||||
const client = redis.createClient()
|
||||
|
||||
client.eval('return 100.5', 0, (err, res) => {
|
||||
console.dir(err)
|
||||
console.dir(res)
|
||||
})
|
||||
|
||||
client.eval([ 'return 100.5', 0 ], (err, res) => {
|
||||
console.dir(err)
|
||||
console.dir(res)
|
||||
})
|
@@ -1,27 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const redis = require('redis')
|
||||
const client = redis.createClient()
|
||||
|
||||
// Extend the RedisClient prototype to add a custom method
|
||||
// This one converts the results from 'INFO' into a JavaScript Object
|
||||
|
||||
redis.RedisClient.prototype.parseInfo = function (callback) {
|
||||
this.info((err, res) => {
|
||||
if (err) throw err
|
||||
const lines = res.toString().split('\r\n').sort()
|
||||
const obj = {}
|
||||
lines.forEach((line) => {
|
||||
const parts = line.split(':')
|
||||
if (parts[1]) {
|
||||
obj[parts[0]] = parts[1]
|
||||
}
|
||||
})
|
||||
callback(obj)
|
||||
})
|
||||
}
|
||||
|
||||
client.parseInfo((info) => {
|
||||
console.dir(info)
|
||||
client.quit()
|
||||
})
|
@@ -1,38 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
// Read a file from disk, store it in Redis, then read it back from Redis.
|
||||
|
||||
const redis = require('redis')
|
||||
const client = redis.createClient({
|
||||
returnBuffers: true
|
||||
})
|
||||
const fs = require('fs')
|
||||
const assert = require('assert')
|
||||
const filename = 'grumpyCat.jpg'
|
||||
|
||||
// Get the file I use for testing like this:
|
||||
// curl http://media4.popsugar-assets.com/files/2014/08/08/878/n/1922507/caef16ec354ca23b_thumb_temp_cover_file32304521407524949.xxxlarge/i/Funny-Cat-GIFs.jpg -o grumpyCat.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, (err, data) => {
|
||||
if (err) throw err
|
||||
console.log(`Read ${data.length} bytes from filesystem.`)
|
||||
|
||||
client.set(filename, data, console.log) // set entire file
|
||||
client.get(filename, (err, reply) => { // get entire file
|
||||
if (err) {
|
||||
console.log(`Get error: ${err}`)
|
||||
} else {
|
||||
assert.strictEqual(data.inspect(), reply.inspect())
|
||||
fs.writeFile(`duplicate_${filename}`, reply, (err) => {
|
||||
if (err) {
|
||||
console.log(`Error on write: ${err}`)
|
||||
} else {
|
||||
console.log('File written.')
|
||||
}
|
||||
client.end()
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
@@ -1,8 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const client = require('redis').createClient()
|
||||
|
||||
client.mget(['sessions started', 'sessions started', 'foo'], (err, res) => {
|
||||
if (err) throw err
|
||||
console.dir(res)
|
||||
})
|
@@ -1,13 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const client = require('../index').createClient()
|
||||
const util = require('util')
|
||||
|
||||
client.monitor((err, res) => {
|
||||
if (err) throw err
|
||||
console.log('Entering monitoring mode.')
|
||||
})
|
||||
|
||||
client.on('monitor', (time, args) => {
|
||||
console.log(`${time }: ${util.inspect(args)}`)
|
||||
})
|
@@ -1,53 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const redis = require('redis')
|
||||
const client = redis.createClient()
|
||||
let setSize = 20
|
||||
|
||||
client.sadd('bigset', 'a member')
|
||||
client.sadd('bigset', 'another member')
|
||||
|
||||
while (setSize > 0) {
|
||||
client.sadd('bigset', `member ${setSize}`)
|
||||
setSize -= 1
|
||||
}
|
||||
|
||||
// multi chain with an individual callback
|
||||
client.multi()
|
||||
.scard('bigset')
|
||||
.smembers('bigset')
|
||||
.keys('*', (err, replies) => {
|
||||
if (err) throw err
|
||||
client.mget(replies, console.log)
|
||||
})
|
||||
.dbsize()
|
||||
.exec((err, replies) => {
|
||||
if (err) throw err
|
||||
console.log(`MULTI got ${replies.length} replies`)
|
||||
replies.forEach((reply, index) => {
|
||||
console.log(`Reply ${index}: ${reply.toString()}`)
|
||||
})
|
||||
})
|
||||
|
||||
client.mset('incr thing', 100, 'incr other thing', 1, console.log)
|
||||
|
||||
// start a separate multi command queue
|
||||
const multi = client.multi()
|
||||
multi.incr('incr thing', console.log)
|
||||
multi.incr('incr other thing', console.log)
|
||||
|
||||
// runs immediately
|
||||
client.get('incr thing', console.log) // 100
|
||||
|
||||
// drains multi queue and runs atomically
|
||||
multi.exec((err, replies) => {
|
||||
if (err) throw err
|
||||
console.log(replies) // 101, 2
|
||||
})
|
||||
|
||||
// you can re-run the same transaction if you like
|
||||
multi.exec((err, replies) => {
|
||||
if (err) throw err
|
||||
console.log(replies) // 102, 3
|
||||
client.quit()
|
||||
})
|
@@ -1,34 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const redis = require('redis')
|
||||
const client = redis.createClient()
|
||||
|
||||
// start a separate command queue for multi
|
||||
const multi = client.multi()
|
||||
multi.incr('incr thing', console.log)
|
||||
multi.incr('incr other thing', console.log)
|
||||
|
||||
// runs immediately
|
||||
client.mset('incr thing', 100, 'incr other thing', 1, console.log)
|
||||
|
||||
// drains multi queue and runs atomically
|
||||
multi.exec((err, replies) => {
|
||||
if (err) throw err
|
||||
console.log(replies) // 101, 2
|
||||
})
|
||||
|
||||
// you can re-run the same transaction if you like
|
||||
multi.exec((err, replies) => {
|
||||
if (err) throw err
|
||||
console.log(replies) // 102, 3
|
||||
client.quit()
|
||||
})
|
||||
|
||||
client.multi([
|
||||
['mget', 'multifoo', 'multibar', console.log],
|
||||
['incr', 'multifoo'],
|
||||
['incr', 'multibar']
|
||||
]).exec((err, replies) => {
|
||||
if (err) throw err
|
||||
console.log(replies.toString())
|
||||
})
|
@@ -1,33 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const redis = require('redis')
|
||||
const client1 = redis.createClient()
|
||||
const client2 = redis.createClient()
|
||||
const client3 = redis.createClient()
|
||||
const client4 = redis.createClient()
|
||||
let msgCount = 0
|
||||
|
||||
client1.on('psubscribe', (pattern, count) => {
|
||||
console.log(`client1 psubscribed to ${pattern}, ${count} total subscriptions`)
|
||||
client2.publish('channeltwo', 'Me!')
|
||||
client3.publish('channelthree', 'Me too!')
|
||||
client4.publish('channelfour', 'And me too!')
|
||||
})
|
||||
|
||||
client1.on('punsubscribe', (pattern, count) => {
|
||||
console.log(`client1 punsubscribed from ${pattern}, ${count} total subscriptions`)
|
||||
client4.end()
|
||||
client3.end()
|
||||
client2.end()
|
||||
client1.end()
|
||||
})
|
||||
|
||||
client1.on('pmessage', (pattern, channel, message) => {
|
||||
console.log(`(${pattern}) client1 received message on ${channel}: ${message}`)
|
||||
msgCount += 1
|
||||
if (msgCount === 3) {
|
||||
client1.punsubscribe()
|
||||
}
|
||||
})
|
||||
|
||||
client1.psubscribe('channel*')
|
@@ -1,42 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const redis = require('redis')
|
||||
const client1 = redis.createClient()
|
||||
let msgCount = 0
|
||||
const client2 = redis.createClient()
|
||||
|
||||
// Most clients probably don't do much on 'subscribe'. This example uses it to coordinate things within one program.
|
||||
client1.on('subscribe', (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', (channel, count) => {
|
||||
console.log(`client1 unsubscribed from ${channel}, ${count} total subscriptions`)
|
||||
if (count === 0) {
|
||||
client2.end()
|
||||
client1.end()
|
||||
}
|
||||
})
|
||||
|
||||
client1.on('message', (channel, message) => {
|
||||
console.log(`client1 channel ${channel}: ${message}`)
|
||||
msgCount += 1
|
||||
if (msgCount === 3) {
|
||||
client1.unsubscribe()
|
||||
}
|
||||
})
|
||||
|
||||
client1.on('ready', () => {
|
||||
// if you need auth, do it here
|
||||
client1.incr('did a thing')
|
||||
client1.subscribe('a nice channel', 'another one')
|
||||
})
|
||||
|
||||
client2.on('ready', () => {
|
||||
// if you need auth, do it here
|
||||
})
|
@@ -1,51 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const redis = require('redis')
|
||||
const client = redis.createClient()
|
||||
|
||||
let cursor = '0'
|
||||
|
||||
function scan () {
|
||||
client.scan(
|
||||
cursor,
|
||||
'MATCH', 'q:job:*',
|
||||
'COUNT', '10',
|
||||
(err, res) => {
|
||||
if (err) throw err
|
||||
|
||||
// Update the cursor position for the next scan
|
||||
cursor = res[0]
|
||||
// get the SCAN result for this iteration
|
||||
const keys = res[1]
|
||||
|
||||
// Remember: more or less than COUNT or no keys may be returned
|
||||
// See http://redis.io/commands/scan#the-count-option
|
||||
// Also, SCAN may return the same key multiple times
|
||||
// See http://redis.io/commands/scan#scan-guarantees
|
||||
// Additionally, you should always have the code that uses the keys
|
||||
// before the code checking the cursor.
|
||||
if (keys.length > 0) {
|
||||
console.log('Array of matching keys', keys)
|
||||
}
|
||||
|
||||
// It's important to note that the cursor and returned keys
|
||||
// vary independently. The scan is never complete until redis
|
||||
// returns a non-zero cursor. However, with MATCH and large
|
||||
// collections, most iterations will return an empty keys array.
|
||||
|
||||
// Still, a cursor of zero DOES NOT mean that there are no keys.
|
||||
// A zero cursor just means that the SCAN is complete, but there
|
||||
// might be one last batch of results to process.
|
||||
|
||||
// From <http://redis.io/commands/scan>:
|
||||
// 'An iteration starts when the cursor is set to 0,
|
||||
// and terminates when the cursor returned by the server is 0.'
|
||||
if (cursor === '0') {
|
||||
return console.log('Iteration complete')
|
||||
}
|
||||
|
||||
return scan()
|
||||
}
|
||||
)
|
||||
}
|
||||
scan()
|
@@ -1,27 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const redis = require('redis')
|
||||
const client = redis.createClient()
|
||||
|
||||
client.on('error', (err) => {
|
||||
console.log(`error event - ${client.host}:${client.port} - ${err}`)
|
||||
})
|
||||
|
||||
client.set('string key', 'string val', console.log)
|
||||
client.hset('hash key', 'hashtest 1', 'some value', console.log)
|
||||
client.hset(['hash key', 'hashtest 2', 'some other value'], console.log)
|
||||
client.hkeys('hash key', (err, replies) => {
|
||||
if (err) {
|
||||
return console.error(`error response - ${err}`)
|
||||
}
|
||||
|
||||
console.log(`${replies.length } replies:`)
|
||||
replies.forEach((reply, i) => {
|
||||
console.log(` ${i}: ${reply}`)
|
||||
})
|
||||
})
|
||||
|
||||
client.quit((err, res) => {
|
||||
if (err) throw err
|
||||
console.log('Exiting from quit command.')
|
||||
})
|
@@ -1,19 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const redis = require('redis')
|
||||
const client = redis.createClient()
|
||||
|
||||
client.sadd('mylist', 1)
|
||||
client.sadd('mylist', 2)
|
||||
client.sadd('mylist', 3)
|
||||
|
||||
client.set('weight_1', 5)
|
||||
client.set('weight_2', 500)
|
||||
client.set('weight_3', 1)
|
||||
|
||||
client.set('object_1', 'foo')
|
||||
client.set('object_2', 'bar')
|
||||
client.set('object_3', 'qux')
|
||||
|
||||
client.sort('mylist', 'by', 'weight_*', 'get', 'object_*', console.log)
|
||||
// Prints Reply: qux,foo,bar
|
@@ -1,19 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
// Sending commands in response to other commands.
|
||||
// This example runs 'type' against every key in the database
|
||||
//
|
||||
const client = require('redis').createClient()
|
||||
|
||||
client.keys('*', (err, keys) => {
|
||||
if (err) throw err
|
||||
keys.forEach((key, pos) => {
|
||||
client.type(key, (err, keytype) => {
|
||||
if (err) throw err
|
||||
console.log(`${key } is ${keytype}`)
|
||||
if (pos === (keys.length - 1)) {
|
||||
client.quit()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
@@ -1,19 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const client = require('redis').createClient()
|
||||
|
||||
// build a map of all keys and their types
|
||||
client.keys('*', (err, allKeys) => {
|
||||
if (err) throw err
|
||||
const keyTypes = {}
|
||||
|
||||
allKeys.forEach((key, pos) => { // use second arg of forEach to get pos
|
||||
client.type(key, (err, type) => {
|
||||
if (err) throw err
|
||||
keyTypes[key] = type
|
||||
if (pos === allKeys.length - 1) { // callbacks all run in order
|
||||
console.dir(keyTypes)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
@@ -1,33 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const redis = require('redis')
|
||||
const client = redis.createClient('/tmp/redis.sock')
|
||||
const profiler = require('v8-profiler')
|
||||
|
||||
client.on('connect', () => {
|
||||
console.log('Got Unix socket connection.')
|
||||
})
|
||||
|
||||
client.on('error', (err) => {
|
||||
console.log(err.message)
|
||||
})
|
||||
|
||||
client.set('space chars', 'space value')
|
||||
|
||||
setInterval(() => {
|
||||
client.get('space chars')
|
||||
}, 100)
|
||||
|
||||
function done () {
|
||||
client.info((err, reply) => {
|
||||
if (err) throw err
|
||||
console.log(reply.toString())
|
||||
client.quit()
|
||||
})
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
console.log('Taking snapshot.')
|
||||
profiler.takeSnapshot()
|
||||
done()
|
||||
}, 5000)
|
@@ -1,36 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
// A simple web server that generates dyanmic content based on responses from Redis
|
||||
|
||||
const http = require('http')
|
||||
const redisClient = require('redis').createClient()
|
||||
|
||||
http.createServer((request, response) => { // The server
|
||||
response.writeHead(200, {
|
||||
'Content-Type': 'text/plain'
|
||||
})
|
||||
|
||||
let redisInfo, totalRequests
|
||||
|
||||
redisClient.info((err, reply) => {
|
||||
if (err) throw err
|
||||
redisInfo = reply // stash response in outer scope
|
||||
})
|
||||
redisClient.incr('requests', (err, reply) => {
|
||||
if (err) throw err
|
||||
totalRequests = reply // stash response in outer scope
|
||||
})
|
||||
redisClient.hincrby('ip', request.connection.remoteAddress, 1)
|
||||
redisClient.hgetall('ip', (err, reply) => {
|
||||
if (err) throw err
|
||||
// This is the last reply, so all of the previous replies must have completed already
|
||||
response.write(`${'This page was generated after talking to redis.\n\n' +
|
||||
'Redis info:\n'}${redisInfo}\n` +
|
||||
`Total requests: ${totalRequests}\n\n` +
|
||||
`IP count: \n`)
|
||||
Object.keys(reply).forEach((ip) => {
|
||||
response.write(` ${ip}: ${reply[ip]}\n`)
|
||||
})
|
||||
response.end()
|
||||
})
|
||||
}).listen(80)
|
Reference in New Issue
Block a user