Last active
November 22, 2021 07:30
-
-
Save danielrvt/91765654445006bb28e2 to your computer and use it in GitHub Desktop.
Using sorted sets with redis client for NodeJS. In this example I retrieve the list of elements ordered by score in descending order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var redis = require("redis"), | |
client = redis.createClient(); | |
// if you'd like to select database 3, instead of 0 (default), call | |
// client.select(3, function() { /* ... */ }); | |
client.on("error", function (err) { | |
console.log("Error " + err); | |
}); | |
// Crea elementos de score 1 en el conjunto | |
client.zadd("myzset", 1, "i1"); | |
client.zadd("myzset", 1, "i2"); | |
client.zadd("myzset", 1, "i3"); | |
// Incrementa el primer elemento varias veces | |
client.zincrby("myzset", 1, "i1"); | |
client.zincrby("myzset", 1, "i1"); | |
client.zincrby("myzset", 1, "i1"); | |
client.zincrby("myzset", 1, "i1"); | |
// Obtiene un rango de elementos ordenados por score de mayor a menor | |
client.zrevrange("myzset", 0, 10, function (err, list) { | |
if (err) throw err; | |
console.log("plain range:", list); | |
}); | |
// Obtiene un rango de elementos, junto con sus scores, ordenados de | |
// mayor a menor | |
client.zrevrange("myzset", 0, 10, "withscores", function (err, listwithscores) { | |
if (err) throw err; | |
console.log("with scores:", listwithscores); | |
}); | |
client.quit(function (err, res) { | |
console.log("Exiting from quit command."); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment