[Avg. reading time: 8 minutes]
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"
GET "character:rachel"
GETRANGE "character:rachel" 0 4
SET "character:ross" "Palentologist"
SET "character:chandler" "Accountant"
SET "character:joey" "Actor"
-- Returns all the characters
Option 1:
SCAN 0 MATCH "character:*" COUNT 10
Option 2:
KEYS character:*
Using SCAN is non blocking, allowes other commands to be processed between iterations. Iterates the key space incrementally.
Using KEYS will block the server when scanning for all the data. Returns all matching keys in a single operation. 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"
SET "character:rachel:lastname" "Green"
SET "character:rachel:gender" "Female"
SET "character:rachel:age" 30
SET "character:ross:job" "Palentologist"
SET "character:ross:lastname" "Geller"
SET "character:ross:gender" "Male"
SET "character:ross:age" 32
SET "character:chandler:job" "Accountant"
SET "character:chandler:lastname" "Bing"
SET "character:chandler:gender" "Male"
SET "character:chandler:age" 32
GET "character:rachel:job"
STRLEN "character:rachel:job"
APPEND "character:rachel:job" " Designer"
GETRANGE "character:rachel:job" 0 4
KEYS "character:*:job"
KEYS "character:ross:*"
DEL "character:chandler"
EXISTS "character:chandler"
-
SETNX: Sets the value of a key only if the key does not exist.
SETNX "character:rachel" "Fashion"
-
SETEX: Sets the value of a key with an expiration time.
SETEX "character:rachel" 60 "Fashion"
(expires in 60 seconds) -
MSET: Sets multiple keys to multiple values in a single atomic operation.
MSET "character:rachel" "Fashion" "character:ross" "Paleontologist"
-
MGET: Gets the values of all the given keys.
MGET "character:rachel" "character:ross"
-
INCR: Increments the integer value of a key by one.
INCR "pageviews:rachelProfile"
GET "pageviews:rachelProfile"
-
DECR: Decrements the integer value of a key by one.
DECR "stock:centralPerkMugs"
-
INCRBY: Increments the integer value of a key by the given amount.
INCRBY "followers:rachel" 10
-
DECRBY: Decrements the integer value of a key by the given number.
DECRBY "debt:rachel" 100
-
INCRBYFLOAT: Increments the float value of a key by the given amount.
INCRBYFLOAT "balance:rachel" 100.50
-
GETSET: Sets a new value and returns the old value.
GETSET "character:rachel" "Executive"
-
MSETNX: Sets multiple keys to multiple values only if none exist.
MSETNX "character:rachel" "Fashion" "character:monica" "Chef"
-
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)