In-memory data storage for key-value pairs๐ฉ๐ปโ๐ป
Leverages Node.js' native net module to create TCP server. Data will also persist in data.json
file and will reload on server start.
SET key value EX?
: Stores the key-value pair in the database with option to set expiration in seconds.- returns:
'OK'
upon data entry
- returns:
GET key
: Retrieves the value associated with the key.- returns: value of the associated key or
null
if key does not exist
- returns: value of the associated key or
DEL key/s
: Deletes single or multiple key-value pair from the database.- returns:
0
if key does not exist,1
if a single key-value pair is deleted, or the number of key-value pairs that has been deleted - note: Per Redis' forum, the DEL command does not "delete" the document. This implementation is a mix of FT.DEL command that deletes the entire document and DEL command that allows multiple key deletion.
- returns:
EXPIRE key seconds
: Sets a timeout on the specified key. After the timeout has expired, the key will be deleted.- returns:
0
if key does not exist or1
if the timeout was set
- returns:
TTL key
: Returns the remaining time to live of a key that has an expiration set, in seconds.- returns:
-2
if key does not exist,-1
if the expiration is not set, or the remaining time in seconds
- returns:
Note: the return values are set to mimic Redis as closely as possible
git clone
- Run
npm install
- Write desired commands in
client.ts
- Run
npm run server
- In a separate terminal, run
npm run client
Results will logged to the console.
To check the current state of DataStorage, see data.json
file. Data will only be updated to data.json
on server close to keep state management in DatatStorage
.