LRU cache is a data structure that removed the least recently used (key, value)
pair from the cache & puts the most recent pair data at the top/beginning of it. Thus it allows to access recently used data faster & easier. The least recently used pair always stays at the bottom/ end and the most recently used pair always stays at the top/beginning of this LRUCache.
-
LRUCache
- a class named with it is implemented which provides properties & methods to implement this data structure
- the size of it must be initialized while declaring
-
get(key)
- returns the
value
of this key if this key exists in the LRUcache - returns
-1
of this key if this key doesn't exist in the LRUcache - puts this
(key, value)
pair on top/ beginning of the LRUCache
- returns the
-
put(key, value)
- if this
key
exits, then it changes itsvalue
with the given new one & puts this(key, value)
pair on top/ beginning of the LRUCache - if this
key
doesn't exist, but the cache is full; then it deletes the least used(key, value)
pair from the bottom/ end & adds this new(key, value)
pair on top/beginning of the LRUCache - if this
key
doesn't exist and the cache is not full; then it just adds this new(key, value)
pair on top/beginning of the LRUCache
- if this
Runtime environment
- Node.js
Testing framework
- Jest
-
Navigate & open CLI into the directory where you want to put this project & Clone this project (will be cloned inside LRU-cache folder) using this command.
git clone https://github.com/tazbin/LRU-caching-class.git ./LRU-cache
- Download the repository
- Extract the zip file, navigate into it & copy the folder to your desired directory
-
To build docker image
docker compose build --no-cache
-
To run the containers in detached mode (wait for a while for database connection)
docker compose up -d
-
To view running containers
docker container ps
-
To view API logs
docker logs lru-cache-c
-
To run tests, first enter within the API container
- on windows CMD (not switching to bash)
docker exec -it lru-cache-c /bin/sh
- on windows CMD (after switching to bash)
or
docker exec -it lru-cache-c //bin//sh
winpty docker exec -it lru-cache-c //bin//sh
now run test command
npm test
- on windows CMD (not switching to bash)
-
To exit from API container, press Ctrl+D on terminal
-
To stop the containers
docker compose down