Last active
April 10, 2020 06:56
-
-
Save pracps/4007e92433e32ec1cab3b71c1f558397 to your computer and use it in GitHub Desktop.
Redis DSA
This file contains hidden or 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
# Adding coordinates <longitude, latitude> | |
# Longtude Degree horizontal (east) | |
# Latitide Degree vertical (North) | |
GEOADD gpo 77.5839739 12.910647 "Home" | |
GEOADD gpo 77.6965459 12.939111 "SLV Residency" | |
GEOADD gpo 77.6102169 12.935152 "Forum Mall" | |
GEOADD gpo 77.5964085 12.91486 "Freshco Supermarket" | |
GEOADD gpo 77.64062 12.892286 "Myntra Designs Pvt Ltd" | |
# Stored as sorted set | |
# Score is computed geohash of the point | |
ZRANGE gpo 0 -1 withscores | |
# Get Geohash | |
# 11 char representation of stored hash, Confirms to GeoHash Standard | |
# Interoperability with other systems and services | |
# Truncation causes inaccuracy, but not byh much | |
geohash gpo "Home" "SLV Residency" | |
1) "tdr1mqqcvw0" | |
2) "tdr3852zu90" | |
# Getting stored coordinates | |
geopos gpo "Home" "SLV Residency" | |
1) 1) "77.58397132158279419" | |
2) "12.91064658271323395" | |
2) 1) "77.69654363393783569" | |
2) "12.93911150133261145" | |
# Finding distance between two locations in same key | |
geodist gpo "Home" "Freshco Supermarket" | |
"1427.2017" | |
geodist gpo "Home" "Freshco Supermarket" "km" | |
"1.4272" | |
geodist gpo "Home" "Myntra Designs Pvt Ltd" "km" | |
"6.4724" | |
geodist gpo "Home" "Forum Mall" "km" | |
"3.9404" | |
geodist gpo "Home" "SLV Residency" "km" | |
"12.6078" | |
# Searching for locations within given radius of Jaydeva Junction | |
# With coordinates, distance and hash in ascending order of distance | |
GEORADIUS gpo 77.599670 12.916655 2 km withcoord withdist withhash asc | |
1) 1) "Freshco Supermarket" | |
2) "0.4063" | |
3) (integer) 3574451358640845 | |
4) 1) "77.59640604257583618" | |
2) "12.91485928928005222" | |
2) 1) "Home" | |
2) "1.8284" | |
3) (integer) 3574451331983601 | |
4) 1) "77.58397132158279419" | |
2) "12.91064658271323395" | |
# Desc example | |
GEORADIUS gpo 77.599670 12.916655 4 km withcoord withdist withhash desc | |
1) 1) "Forum Mall" | |
2) "2.3539" | |
3) (integer) 3574451926733187 | |
4) 1) "77.61021941900253296" | |
2) "12.93515226688172248" | |
2) 1) "Home" | |
2) "1.8284" | |
3) (integer) 3574451331983601 | |
4) 1) "77.58397132158279419" | |
2) "12.91064658271323395" | |
3) 1) "Freshco Supermarket" | |
2) "0.4063" | |
3) (integer) 3574451358640845 | |
4) 1) "77.59640604257583618" | |
2) "12.91485928928005222" | |
# Farthest within radius | |
GEORADIUS gpo 77.599670 12.916655 4 km withcoord withdist withhash count 1 desc | |
1) 1) "Forum Mall" | |
2) "2.3539" | |
3) (integer) 3574451926733187 | |
4) 1) "77.61021941900253296" | |
2) "12.93515226688172248" | |
# GEORADIUSBYMEMBER does same thing just by named member, so the member is at center of that radius | |
# hence always included in result | |
GEORADIUSBYMEMBER gpo "Forum Mall" 3 km WITHCOORD WITHDIST WITHHASH ASC | |
1) 1) "Forum Mall" | |
2) "0.0000" | |
3) (integer) 3574451926733187 | |
4) 1) "77.61021941900253296" | |
2) "12.93515226688172248" | |
2) 1) "Freshco Supermarket" | |
2) "2.7087" | |
3) (integer) 3574451358640845 | |
4) 1) "77.59640604257583618" | |
2) "12.91485928928005222" |
This file contains hidden or 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
# Setting multiple fields | |
hset event:judo capacity 12000 location "Dumbass Arena" ticket_price:gold 100 availability:gold 100 | |
(integer) 0 | |
# Setting single field | |
HSET event:judo availability:gold 8000 | |
(integer) 0 | |
# Check if key exists | |
HEXISTS event:judo location | |
(integer) 1 | |
HEXISTS event:judo place | |
(integer) 0 | |
# Get all fields, not good for performance | |
HGETALL event:judo | |
1) "capacity" | |
2) "12000" | |
3) "location" | |
4) "Dumbass Arena" | |
5) "ticket_price:gold" | |
6) "100" | |
7) "availability:gold" | |
8) "8000" | |
9) "availability:silver" | |
10) "2000" | |
# Using scan | |
HSCAN event:judo 0 match availability:* | |
1) "0" | |
2) 1) "availability:gold" | |
2) "8000" | |
3) "availability:silver" | |
4) "2000" |
This file contains hidden or 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
# Number of keys present in cache | |
dbsize | |
# SET Command | |
# SET <key> <value> [EX seconds] [PX milliseconds] [NX] [XX] | |
# NX - set only if doesn't exist, XX set only if exist | |
# returns OK if executed correctly | |
SET customer:1000 fred | |
SET customer:1500 jane | |
# Get key values | |
GET customer:1000 | |
# KEYS and SCAN | |
# KEYS command return keys matching pattern | |
# good for debugging, not prod | |
KEYS customer:1* | |
# SCAN command (Cursor based iterator) | |
# An iteration starts when the cursor is set to 0, and terminates when the cursor returned by the server is 0 | |
SCAN 0 | |
# keep using slot index to move forward cursor for next iteration | |
# O(1) per call with linear iteration | |
# can be used in prod with downside like KEYS | |
# MATCH and COUNT are optional | |
scan 4644 MATCH customer:1* COUNT 1000 | |
# Deleting keys (and cleans up memory) | |
# can take O(M) of hash set deletion etc | |
# Blocking call, returns number of deleted keys | |
# can accept multiple keys to be deleted | |
DEL customer:1000 | |
# UNLINK | |
# Non blocking O(1) | |
# Similar to DEL, deletes key | |
# but memory reclaim happens async in another thread | |
UNLINK customer:1500 | |
# More Examples | |
127.0.0.1:6379> set inventory:100-meters-women-final 1000 NX | |
OK | |
127.0.0.1:6379> set inventory:100-meters-women-final "Sold Out" NX | |
(nil) | |
127.0.0.1:6379> set inventory:100-meters-women-final 0 XX | |
OK | |
127.0.0.1:6379> get inventory:100-meters-women-final |
This file contains hidden or 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
# List in redis is doubly linked list implementation | |
LPUSH wins:2019 ham:9 vet:1 lec:2 | |
(integer) 3 | |
LLEN wins:2019 | |
(integer) 3 | |
LRANGE wins:2019 0 -1 | |
1) "lec:2" | |
2) "vet:1" | |
3) "ham:9" | |
RPOP wins:2019 | |
"ham:9" | |
LPOP wins:2019 | |
"lec:2" | |
LRANGE wins:2019 0 -1 | |
1) "vet:1" |
This file contains hidden or 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
# Subscribing to single/multiple channels | |
SUBSCRIBE dax | |
Reading messages... (press Ctrl-C to quit) | |
1) "subscribe" | |
2) "dax" | |
3) (integer) 1 | |
1) "message" | |
2) "dax" | |
3) "hello" | |
^C | |
# Multiple channels | |
SUBSCRIBE ch1 ch2 | |
Reading messages... (press Ctrl-C to quit) | |
1) "subscribe" | |
2) "ch1" | |
3) (integer) 1 | |
1) "subscribe" | |
2) "ch2" | |
3) (integer) 2 | |
1) "message" | |
2) "ch1" | |
3) "fp1" | |
1) "message" | |
2) "ch2" | |
# Publish Messages | |
# Returns no. of clients that received the message | |
PUBLISH ch1 fp1 | |
(integer) 1 | |
PUBLISH ch2 rossi | |
(integer) 1 | |
PUBLISH ch3 mock | |
(integer) 0 | |
# --- | |
# Pattern based subscription | |
PSUBSCRIBE ch-* | |
# Admin Commands | |
# Get list of subscribed channels | |
PUBSUB channels | |
# Number of subscribed by fixed channel | |
PUBSUB numsub ch-1 ch-2 | |
# Pattern subscribers | |
PUBSUB numpat ch-* |
This file contains hidden or 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
# Creating set | |
SADD drivers "ham" "vet" "lec" "ver" "ric" | |
(integer) 5 | |
# good for test and development, scan better for prod | |
SMEMBERS drivers | |
1) "lec" | |
2) "vet" | |
3) "ver" | |
4) "ham" | |
5) "ric" | |
sscan drivers 0 match * | |
1) "0" | |
2) 1) "lec" | |
2) "vet" | |
3) "ham" | |
4) "ver" | |
5) "ric" | |
sscan drivers 0 match v* | |
1) "0" | |
2) 1) "vet" | |
2) "ver" | |
# Check Set membership | |
SISMEMBER drivers "vet" | |
(integer) 1 | |
SISMEMBER drivers "mag" | |
(integer) 0 | |
# Removing elements which exist (or not exist) | |
# Not exist | |
SREM drivers "mag" | |
(integer) 0 | |
# Exist | |
SREM drivers "ric" | |
# Remove two random elements | |
SPOP drivers 2 | |
1) "vet" | |
2) "lec" | |
(integer) 1 | |
# SET operations | |
sadd ferrari "vet" "rai" "lec" "alo" "shu" | |
(integer) 5 | |
sadd mclaren "nor" "alo" "sai" "ham" | |
(integer) 4 | |
sadd mercedes "ham" "ros" "shu" "bot" | |
(integer) 4 | |
SINTER ferrari mclaren | |
1) "alo" | |
SINTER ferrari mercedes | |
1) "shu" |
This file contains hidden or 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
zadd standings 413 ham 326 bot 278 ver 264 lec 240 vet 96 sai | |
(integer) 6 | |
zrange standings 0 -1 | |
1) "sai" | |
2) "vet" | |
3) "lec" | |
4) "ver" | |
5) "bot" | |
6) "ham" | |
# By rank, which is 0 based | |
zrange standings 0 2 | |
1) "sai" | |
2) "vet" | |
3) "lec" | |
zrange standings 0 2 WITHSCORES | |
1) "sai" | |
2) "96" | |
3) "vet" | |
4) "240" | |
5) "lec" | |
6) "264" | |
# Getting score of a specific element | |
ZSCORE standings vet | |
"240" | |
# Getting rank of a element | |
ZRANK standings vet | |
(integer) 1 | |
# Getting count if elements in given score range | |
ZCOUNT standings 250 420 | |
(integer) 4 | |
# Getting element itself | |
ZRANGEBYSCORE standings 250 420 | |
1) "lec" | |
2) "ver" | |
3) "bot" | |
4) "ham" | |
# Removing | |
zrem standings sai | |
(integer) 1 | |
zrange standings 0 -1 | |
1) "vet" | |
2) "lec" | |
3) "ver" | |
4) "bot" | |
5) "ham" |
This file contains hidden or 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
# any type of values can be stored as string data type (text, numbers, binary data) | |
# limit of 512 MB with value | |
# type is checked before any command operates on value | |
set races:pole:mercedes 12 | |
OK | |
get races:pole:mercedes | |
"12" | |
# Type is mostly string for plain values | |
TYPE races:pole:mercedes | |
string | |
# works because object encoding is integer | |
DECRBY races:pole:mercedes 1 | |
(integer) 11 | |
get races:pole:mercedes | |
"11" | |
TYPE races:pole:mercedes | |
string | |
object encoding races:pole:mercedes | |
"int" | |
# changing value to string | |
set races:pole:mercedes "More than 10" | |
OK | |
# object encoding is ember string, now INCRBY, DECRBY won't work | |
object encoding races:pole:mercedes | |
"embstr" | |
# changing value to string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment