Skip to content

Instantly share code, notes, and snippets.

@awssimplified
Created July 25, 2020 16:16
Show Gist options
  • Save awssimplified/a5bff8d59e66c93dd65213cfe8db8e90 to your computer and use it in GitHub Desktop.
Save awssimplified/a5bff8d59e66c93dd65213cfe8db8e90 to your computer and use it in GitHub Desktop.
//Lets Learn Redis!
// **** PreRequisites ****
// Have Reddis running on localhost, either by using docker or installing it directly on your machine
const redis = require("redis");
const client = redis.createClient();
// *** Basic Operations ***
//SET and GET
client.set("transactions", 123, redis.print)
client.get("transactions", redis.print)
client.del("transactions", redis.print)
client.get("transactions", redis.print)
// INCR and DECR
client.set("counter", 50, redis.print)
client.decr("counter", redis.print)
client.get("counter", redis.print)
// Set with TTL Expiry
client.set("test", "val", "EX", 5, redis.print)
client.get("test", redis.print)
// LISTS
client.rpush("transactions", "t1", "t2", "t3", redis.print)
client.lrange("transactions", 0, -1, redis.print)
client.rpop("transactions", redis.print)
// Queues = Push using LPUSH, extract using RPOP
// Stacks = Push using RPUSH, extract using RPOP
//** Keyspace Operations **
client.get("count", redis.print)
client.set("count", 1)
client.get("count", redis.print)
//** Hashmaps **
// {
// "transactionId": "123",
// "type": "PURCHASE",
// "amount" 50
// }
client.hset("transaction:123", "transaction", 123,
"type", "PURCHASE", "amount", 50, redis.print)
client.hgetall("transaction:123", redis.print)
client.hget("transaction:123", "amount", redis.print)
// SORTED SETS - leaderboards, ranking
client.zadd("scores", 940, "player1", redis.print)
client.zadd("scores", 950, "player2", redis.print)
client.zadd("scores", 930, "player3", redis.print)
player3, player1, player2
client.zrange("scores", 0, -1, redis.print)
client.zrevrange("scores", 0, -1, redis.print)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment