Strings

-- Single Namespace

SET "rachel" "Fashion"
GET "rachel"
STRLEN "rachel"
APPEND "rachel" " Designer"

GET "rachel"

#get first 5 letters
GETRANGE "rachel" 0 4

#Skip last 2 (extreme right is -1 and -2 and so on)
GETRANGE "rachel" 0 -3

# Substring
GETRANGE "rachel" 5 8

# Get the last 5 letters

GETRANGE "rachel" -5 -1 
-- Two Namespaces

SET "character:rachel" "Fashion"
GET "character:rachel"
STRLEN "character:rachel"
APPEND "character:rachel" " Designer"
GETRANGE "character:rachel" 0 4

SET "character:ross" "Palentologist"
-- Get all characters

KEYS character:*

Using KEYS will block the server when scanning for all the data. This might lead to latency in large databases and can affect other users. It is not to be used in Production.

-- Three Namespaces

SET "character:rachel:job" "Fashion"
GET "character:rachel:job"
STRLEN "character:rachel:job"
APPEND "character:rachel:job" " Designer"
GETRANGE "character:rachel:job" 0 4
  1. SETNX: Sets the value of a key only if the key does not exist.

    SETNX "character:rachel" "Fashion"

  2. SETEX: Sets the value of a key with an expiration time.

    SETEX "character:rachel" 60 "Fashion" (expires in 60 seconds)

  3. MSET: Sets multiple keys to multiple values in a single atomic operation.

    MSET "character:rachel" "Fashion" "character:ross" "Paleontologist"

  4. MGET: Gets the values of all the given keys.

    MGET "character:rachel" "character:ross"

  5. INCR: Increments the integer value of a key by one.

    INCR "pageviews:rachelProfile" GET "pageviews:rachelProfile"

  6. DECR: Decrements the integer value of a key by one.

    DECR "stock:centralPerkMugs"

  7. INCRBY: Increments the integer value of a key by the given amount.

    INCRBY "followers:rachel" 10

  8. DECRBY: Decrements the integer value of a key by the given number.

    DECRBY "debt:rachel" 100

  9. INCRBYFLOAT: Increments the float value of a key by the given amount.

    INCRBYFLOAT "balance:rachel" 100.50

  10. GETSET: Sets a new value and returns the old value.

    GETSET "character:rachel" "Executive"

  11. MSETNX: Sets multiple keys to multiple values only if none exist.

    MSETNX "character:rachel" "Fashion" "character:monica" "Chef"

  12. PSETEX: Similar to SETEX PSETEX but with an expiration time in milliseconds.

    PSETEX "character:rachel" 60000 "Fashion" (expires in 60,000 milliseconds or 60 seconds)

Last updated