Your task is to build a REST API that exposes methods to interact with a cache that you will build. You will have to use Node.js and Express.js to build the API and MongoDB to store the cache data in. The cache does not have another data source in the background that is cached. All data returned by the cache is random dummy data. You do not need to build a frontend. The API shall be used with tools like curl or Postman.
Following features have to be implemented for the cache:
- Add an endpoint that returns the cached data for a given key. If the key is not found in the cache:
- Log an output “Cache miss”
- Create a random string
- Update the cache with this random string
- Return the random string
- If the key is found in the cache:
- Logan output “Cache hit”
- Get the data for this key
- Return the data
- Add an endpoint that returns all stored keys in the cache
- Add an endpoint that creates/updates the data for a given key
- Add an endpoint that removes a given key from the cache
- Add an endpoint that removes all keys from the cache
Following additional features have to be also included:
- The number of entries allowed in the cache is limited. If the maximum amount of cached items is reached, some old entry needs to be overwritten (Please explain the approach of what is overwritten in the comments of the source code)
- Every cached item has a Time To Live (TTL). If the TTL is exceeded, the cached data will not be used. A new random value will then be generated (just like cache miss). The TTL will be reset on every read/cache hit
Check .env.example
file, create .env
or set environments variables.
- Start MongoDB via
docker-compose up
or add URI link to the environment npm start
- standart with standard is used for checking code style and JS best practice..
- dredd is used for swagger and rest api testing.
In this part our application store any 3-rd party connection, state and etc. They have start
/stop
for bootstraping.
In this part we store our business logic.
We use dotenv-safe
package for checking environment variables. If you miss part of the settings, then application will not start.
We use bunyan
app.js
is implemented for Bootstraping and Gracefull shatdown. Can be reused for tests.
MongoDB used for this features.