This post will guide you how to get your hands on Redis with String data type.
String type
Strings just store sequences of bytes, including text, serialized arrays and binary arrays.
So, how can we store them in redis database? Let’s see
Setting and getting strings
We can create a key with a string value in different ways:
Expiring string keys
What about expiring keys? We can set a time in seconds/milliseconds/unix-time-seconds/unix-time-milliseconds/keepttl to expire the key when it times out.
Time is expressed as:
EX <seconds>: EXpire secondsPX <milliseconds>: eXPire millisecondsEXAT <unix-time-seconds>: EXpire AT unix-time-secondsPXAT <unix-time-milliseconds>: eXPire AT unix-time-millisecondsKEEPTTL: When modifying value of a key, we maintain the already set expire time
Commands
Set
SET <key> <value> [NX | XX] [GET] [EX <seconds> | PX <milliseconds> | EXAT <unix-time-seconds> | PXAT <unix-time-milliseconds> | KEEPTTL]:- NX: Not eXistent.
- XX: EXXistent.
- GET: GET the old value when updating and null when not existent.
- EX: EXpire in seconds.
- PX: eXPire in milliseconds.
- EXAT: EXpire AT unix-time-seconds
date +%s - PXAT: eXPire AT unix-time-milliseconds
echo $(($(date +%s) * 1000)) - KEEPTTL: keep the expire time set when updating value of the key. TTL -> Time To Live.
SETNX <key> <value>: SET if Not eXistent. Similar toSET <key> <value> NX.SETEX <key> <expire-time-seconds> <value>: SET a key with value and EXpiration time in seconds. Similar toSET <key> <value> EX <expire-time-seconds>.PSETEX <key> <expire-time-milliseconds> <value>: SET a key with value and eXPiration time in milliseconds. Similar toSET <key> <value> PX <expire-time-milliseconds>.MSET <key> <value> [<key> <value>...]: SET Multiple keys with values in one command. This action is atomic (all keys are set at the same time).MSETNX <key> <value> [<key> <value>...]: SET Multiple keys with values in one command if they don’t exists (the ones that already exist, are skipped). This action is atomic (all keys are set at the same time).
Get
GET <key>: It returns the value of the key and if it does not exists, it returns nullGETDEL <key>: It returns the value of the key and deletes it.GETEX <key> [EX <seconds> | PX <milliseconds> | EXAT <unix-time-seconds> | PXAT <unix-time-milliseconds> | PERSIST]:- PERSIST: Remove the time to live associated with the key.
GETRANGE <key> <start> <end>: being mykey “Hello”GETRANGE mykey 0 2returns “Hel”GETRANGE mykey -2 -1returns “olGETRANGE mykey 0 10000returns “Hello”. There are not out of bounds.
GETSET <key> <value>: Set a new value to a key and retrieve the old value. Deprecated in Redis version 6.2.0 Replaced with:SET <key> <value> GET
Operations
APPEND <key> <value>:- If key exists, it appends the value at the end of the actual value.
- If key does not exists, it creates the key with the appended value.
LCS <key1> <key2> [LEN] [IDX] [MIMMATCHLEN len] [WITHMATCHLEN]:- LCS means Longest Common Subsequence. This is not the same as LCS (Longest Common String), which means that characters do not to be contiguos.
- LEN: To get the length of the string.
- IDX: get the position of each string.
- MIMMATCHLEN: Print only the ones that have the minimum “len” passed as a parameter.
- WITHMATCHLEN: Print also the length of each match.
