Skip to content

London | 26-SDC-Mar | beko | Sprint 2 | implement an LRU cache in python#194

Open
Abubakar-Meigag wants to merge 2 commits into
CodeYourFuture:mainfrom
Abubakar-Meigag:beko-implement-LRU-cache-in-python
Open

London | 26-SDC-Mar | beko | Sprint 2 | implement an LRU cache in python#194
Abubakar-Meigag wants to merge 2 commits into
CodeYourFuture:mainfrom
Abubakar-Meigag:beko-implement-LRU-cache-in-python

Conversation

@Abubakar-Meigag

@Abubakar-Meigag Abubakar-Meigag commented Jun 30, 2026

Copy link
Copy Markdown

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Completed implementing an LRU cache in Python

@Abubakar-Meigag Abubakar-Meigag added 📅 Sprint 2 Assigned during Sprint 2 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Complexity The name of the module. labels Jun 30, 2026

@cjyuan cjyuan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jun 30, 2026
@Abubakar-Meigag

Copy link
Copy Markdown
Author

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.

@Abubakar-Meigag Abubakar-Meigag added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Jul 1, 2026

@cjyuan cjyuan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good.


class _Node:

def __init__(self, key=None, value=None):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not reuse your LinkedList class from the other exercise?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could store key-value pairs as a tuple in the node.

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. Module-Complexity The name of the module. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants