Skip to Content
Course content

190: Whiteboard Practice: Implementing a Simple Cache Eviction Policy

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Why can't I just let the cache grow until the script ends or clear it on a timer?

In a small script, you probably could. But imagine you're building a user profile service for a site with a million active users. If you cache every profile you fetch from the database into a static array or a Redis instance without a limit, you're eventually going to hit a memory ceiling. When that happens, your server doesn't just slow down—it crashes with an "Out of Memory" error.

A timer (TTL) helps, but it's blunt. You might have a user who is incredibly active right now, but their cache expires simply because an hour passed. An eviction policy, specifically Least Recently Used (LRU), is smarter. It says, "I only have room for 100 items; if I need to add a 101st, I'll kick out the one that hasn't been touched in the longest time." It keeps the "hot" data ready and dumps the "cold" data.

How do I actually implement LRU using PHP's arrays?

Here is the secret: PHP arrays are actually ordered maps. They remember the order in which elements were added. We can exploit this to build an LRU cache without needing a complex doubly-linked list like you would in C++ or Java.

The trick is to move an item to the end of the array every time it's accessed. That way, the item at the very front of the array is always the "least recently used."

class UserProfileCache {
    private array $cache = [];
    private int $capacity;

    public function __construct(int $capacity) {
        $this->capacity = $capacity;
    }

    public function get(string $userId): ?string {
        if (!isset($this->cache[$userId])) {
            return null;
        }

        // The "Touch" logic: 
        // Remove it and re-add it to move it to the end of the order
        $value = $this->cache[$userId];
        unset($this->cache[$userId]);
        $this->cache[$userId] = $value;

        return $value;
    }

    public function put(string $userId, string $profileData): void {
        if (isset($this->cache[$userId])) {
            unset($this->cache[$userId]);
        } elseif (count($this->cache) >= $this->capacity) {
            // Evict the oldest item (the one at the front)
            reset($this->cache); 
            $oldestKey = key($this->cache);
            unset($this->cache[$oldestKey]);
        }

        $this->cache[$userId] = $profileData;
    }
}

Does this approach actually scale, or is it slow?

If you're doing this in a whiteboard interview, the interviewer is going to ask about "Big O" notation. The beauty of this PHP implementation is that it's incredibly efficient. Both unset() and assigning a value to an array key in PHP happen in O(1) time (constant time).

Even reset() and key() are O(1) because PHP maintains an internal pointer to the current element of the array. You aren't looping through the whole list to find the oldest item; you're just grabbing the head of the map. I've used this pattern in production for simple in-memory caches, and it's rock solid as long as your cache size is reasonable for the allocated PHP memory limit.




📋 Practical Task

Exercise: Implementing a Weighted Cache Eviction Limit

The standard LRU cache treats every item as having the same "cost." However, in the real world, some cached items take up way more memory than others. Imagine you are caching API responses: some are 1KB, others are 100KB.

Modify the UserProfileCache class provided in the lesson to implement a Memory-Based Eviction Policy instead of a simple item-count limit:

  • Change $capacity from a maximum number of items to a maximum number of bytes (e.g., 1024 bytes).
  • The put()` method should calculate the size of the data being added (use strlen() for a simple approximation).
  • Instead of removing just one item when the limit is hit, the put()` method should keep evicting the oldest items (from the front of the array) until there is enough room for the new item.
  • Ensure that if a single item is larger than the total capacity, it simply isn't cached.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.