Simple cache implementation using Express and MongoDB
This code was written in Node v14.17.0
Make sure you have Node and MongoDB installed on your system.
Add .env file. Add MongoDB URL to MONGODB_DATABASE_URI in the .env file
After the above steps, run:
npm install
npm start
Got MongoParseError? Make sure the MONGODB_DATABASE_URI is present in the .env file in the src folder
Name | Value | Description |
---|---|---|
PORT |
6060 | Port API runs on. |
TTL_SECONDS |
120 | Max TTL of each item in the cache. |
MAX_ENTRIES |
10 | Total number of allowed entries in the cache |
You can overwrite these values by adding them in the env file along side with MONGODB_DATABASE_URI
Below endpoints have been implemented.
POST
[http://localhost:6060/insert]
Parameters
Name | Required | Type | Description |
---|---|---|---|
key |
required | string | Unique key for value |
value |
required | string | Value you want to save |
Response
{
"error": null,
"key": "k",
"value": 1
}
GET
[http://localhost:6060/item]
Parameters
Name | Required | Type | Description |
---|---|---|---|
key |
required | string | Unique key for value |
Response
{
"error": null,
"key": "hhhhhhh",
"value": "7fcdebc5-0b0a-4f13-8a70-154f74f7c2ca"
}
GET
[http://localhost:6060/items]
Parameters
Name | Required | Type | Description |
---|
Response
{
"error": null,
"values": [
{
"key": "hhhhhhh",
"value": "e3a8a02a-f7b9-45ef-8545-8bbebea9028c",
"ttl": "2022-03-18T18:25:23.845Z"
},
{
"key": "k",
"value": 1,
"ttl": "2022-03-18T18:24:03.638Z"
}
]
}
DELETE
[http://localhost:6060/item]
Parameters
Name | Required | Type | Description |
---|---|---|---|
key |
required | string | Unique key for value |
Response
No response in case of success
DELETE
[http://localhost:6060/items]
Parameters
Name | Required | Type | Description |
---|
Response
No response in case of success