Write the code to implement a Least-Recently Used (LRU) Cache. A correctly constructed LRU Cache will have its maximum capacity set at the time of the construction, and when adding new keys that cause the capacity to be exceeded, the least recently used item needs to be identified and discarded. You can use any language to implement this.
- Any operation (get/put/del/reset) must be done on a valid cache. If cache is not defined and operation is requested, it will raise an exception
- Calling put for an existing key will update the value and mark it as most recently used
- Calling del for an existing key will mark it as accessed and then delete it
- Calling del for a non existant key will do nothing
- This code uses Python 3.7.6
- The cache is implemented using OrderedDict as the base structure
- The individual methods are implemted in the encapsulating class
- The cache supports both numeric and strings keys and values
- The unit tests are implemeted as python unittest class
- To get detailed print messages, set the cache with verbose as True
There are two files in the submission
- cacheLRU.py - This is the file with the cache implemented as an OrderedDict
- unit_tests.py - Unit tests implemented as unittest which cover edge cases as well as the functionality
- main.py - Driver script for using the cache and its functions
To run the project
- Clone the repository to your local machine
- run python unit_tests.py to run all the testcases
- run python main.py to run custom driver script