London | 26-SDC-Mar | beko | Sprint 2 | implement an LRU cache in python#194
London | 26-SDC-Mar | beko | Sprint 2 | implement an LRU cache in python#194Abubakar-Meigag wants to merge 2 commits into
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
To better adhere to the Single-Responsibility Principle (SRP) from SOLID design principles, it's preferable to implement the "doubly linked list" and the "LRU Cache" as separate classes, with the linked list used inside LruCache to manage ordering.
Alternatively, OrderedDict can be used directly within LruCache to maintain order.
|
thanks @cjyuan for the feedback i've refactored the implementation and and follow SRP by splitting the doubly linked list logic into its own DoublyLinkedList class it handles all the node ordering (remove, add_to_front, remove_last), while LruCache focuses purely on the cache logic key lookups, limit enforcement, and eviction. the two classes are now independent and each has a single clear responsibility. |
|
|
||
| class _Node: | ||
|
|
||
| def __init__(self, key=None, value=None): |
There was a problem hiding this comment.
Why not reuse your LinkedList class from the other exercise?
There was a problem hiding this comment.
you're right i can reuse linked_list from the other exercise, I just chose to keep the linked list logic self-contained within the LRU cache because the node structure here is a bit different and a good exercise to have a different approach
There was a problem hiding this comment.
You could store key-value pairs as a tuple in the node.
Self checklist
Completed implementing an LRU cache in Python