London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 2 | Implement LRU cache#143
London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 2 | Implement LRU cache#143aydaeslami wants to merge 3 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.
In fact, you can import your LinkedList class from the other exercise, and then store each key-value pairs as a tuple.
Alternatively, you can use OrderedDict directly within LruCache to maintain order.
Could you update your code using one of these approaches?
|
Thank you @cjyuan for the suggestion. I refactored the implementation to use OrderedDict , and it made the code much cleaner and easier to read. I also appreciated the reminder about the Single Responsibility Principle (SRP). Thanks for the helpful feedback! |
| value = self.cache.pop(key) | ||
| self.cache[key] = value |
There was a problem hiding this comment.
Does OrderedDict have any built-in method to move its item to the end?
There was a problem hiding this comment.
Thanks for the suggestion. I've replaced the pop-and-reinsert approach with OrderedDict.move_to_end(), which makes the code cleaner and more readable.
I've also run the full test suite again, and all tests are passing.
Please let me know if there's anything else I should change or improve.
| # test | ||
|
|
||
| cache = LruCache(3) | ||
|
|
||
| cache.set("Aida", 1) | ||
| cache.set("Bob", 2) | ||
| cache.set("Clare", 3) | ||
|
|
||
| print(cache.get("Aida")) | ||
|
|
||
| cache.set("David", 4) | ||
|
|
||
| print(cache.get("Bob")) | ||
| print(cache.get("Clare")) | ||
| print(cache.get("David")) | ||
|
|
There was a problem hiding this comment.
Why not implement this in lru_cache_test.py?
Self checklist
Changelist
Implemented LRU cache with O(1) get and set operations using a doubly linked list and dictionary.