UsmanMERN / REDIS

Learning REDIS TO IMPLEMENT THIS WITH MONGODB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Redis Basic Commands

Strings

Basic Operations

  • SET key value: Sets the value of a key.
    SET mykey "Hello"
  • GET key: Gets the value of a key.
    GET mykey
  • SETNX key value: Sets the value of a key if it does not exist.
    SETNX mykey "World"
  • MGET key1 key2 ... keyN: Gets the values of multiple keys.
    MGET key1 key2 key3
  • MSET key1 value1 key2 value2 ... keyN valueN: Sets the values of multiple keys.
    MSET key1 "value1" key2 "value2" key3 "value3"

Number Operations

  • INCR key: Increments the value of a key by 1.
    INCR mycounter
  • INCRBY key increment: Increments the value of a key by a specified increment.
    INCRBY mycounter 5

Bitwise Operations

  • GETBIT key offset: Returns the bit value at the specified offset.
    GETBIT mykey 7
  • SETBIT key offset value: Sets or clears the bit at the specified offset.
    SETBIT mykey 7 1
  • BITCOUNT key [start end]: Counts the number of set bits (population counting) in a string.
    BITCOUNT mykey 0 7

Performance

  • SUBSTR key start end: Returns the substring of the string stored at a key.
    SUBSTR mykey 0 4
  • GETRANGE key start end: Returns the substring of the string stored at a key (alias of SUBSTR).
    GETRANGE mykey 0 4
  • SETRANGE key offset value: Overwrites part of the string stored at a key starting at the specified offset.
    SETRANGE mykey 6 "Redis"

Lists

Stack Operations

  • LPUSH key value1 [value2]: Inserts values at the head of the list.
    LPUSH mylist "world"
    LPUSH mylist "hello"
  • RPUSH key value1 [value2]: Inserts values at the tail of the list.
    RPUSH mylist "hello"
    RPUSH mylist "world"

Queue Operations

  • LPOP key: Removes and returns the first element of the list.
    LPOP mylist
  • RPOP key: Removes and returns the last element of the list.
    RPOP mylist

Blocking Commands

  • BLPOP key [key2 ...] timeout: Removes and returns the first element of the list, or blocks until one is available.
    BLPOP mylist 0
  • BRPOP key [key2 ...] timeout: Removes and returns the last element of the list, or blocks until one is available.
    BRPOP mylist 0
  • LRANGE key start stop: Returns a range of elements from the list.
    LRANGE mylist 0 -1

Miscellaneous

  • DEL key: Deletes a key.
    DEL mykey
KEYS user:* ```sh KEYS user:* ```

Redis Sets

  • SADD key member1 [member2]: Adds members to a set.
    SADD myset "Hello"
  • SREM key member1 [member2]: Removes members from a set.
    SREM myset "Hello"
  • SISMEMBER key member: Checks if a member exists in a set.
    SISMEMBER myset "Hello"
  • SMEMBERS key: Returns all members of a set.
    SMEMBERS myset
  • SCARD key: Gets the number of members in a set.
    SCARD myset

Redis Hashes

  • HSET key field value: Sets the value of a field in a hash.
    HSET myhash field1 "Hello"
  • HGET key field: Gets the value of a field in a hash.
    HGET myhash field1
  • HMGET key field1 [field2]: Gets the values of multiple fields in a hash.
    HMGET myhash field1 field2
  • HINCRBY key field increment: Increments the value of a field in a hash by a specified increment.
    HINCRBY myhash field1 5

Redis Sorted Sets

  • ZADD key score member [score member ...]: Adds members to a sorted set, or updates the score if it already exists.
    ZADD myzset 1 "one"
  • ZRANGE key start stop [WITHSCORES]: Returns a range of members in a sorted set, by index.
    ZRANGE myzset 0 -1
  • ZREVRANGE key start stop [WITHSCORES]: Returns a range of members in a sorted set, by index, with scores ordered from high to low.
    ZREVRANGE myzset 0 -1

Redis Streams

  • XADD key * field value [field value ...]: Appends a new entry to a stream.
    XADD mystream * field1 "value1"
  • XREAD COUNT count STREAMS key [key ...] ID [ID ...]: Reads data from one or multiple streams.
    XREAD COUNT 2 STREAMS mystream 0
  • XRANGE key start end [COUNT count]: Returns a range of entries from a stream.
    XRANGE mystream - +
  • XLEN key: Gets the length of a stream.
    XLEN mystream

Redis Geospatial

  • GEOADD key longitude latitude member [longitude latitude member ...]: Adds geospatial items (latitude, longitude, name) to a key.
    GEOADD mygeoset 13.361389 38.115556 "Palermo"
  • GEORADIUS key longitude latitude radius unit: Queries a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point.
    GEORADIUS mygeoset 15 37 200 km

Additional Commonly Used Commands

Keys

  • EXISTS key: Checks if a key exists.
    EXISTS mykey
  • EXPIRE key seconds: Sets a timeout on a key.
    EXPIRE mykey 10
  • TTL key: Gets the time to live for a key.
    TTL mykey

Transactions

  • MULTI: Marks the start of a transaction block.
    MULTI
  • EXEC: Executes all commands issued after MULTI.
    EXEC
  • DISCARD: Discards all commands issued after MULTI.
    DISCARD

Pub/Sub

  • PUBLISH channel message: Posts a message to a channel.
    PUBLISH mychannel "Hello, Redis!"
  • SUBSCRIBE channel [channel ...]: Subscribes to a channel.
    SUBSCRIBE mychannel
  • UNSUBSCRIBE [channel ...]: Unsubscribes from a channel.
    UNSUBSCRIBE mychannel

About

Learning REDIS TO IMPLEMENT THIS WITH MONGODB


Languages

Language:JavaScript 100.0%