-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
190: Whiteboard Practice: Implementing a Simple Cache Eviction Policy
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
$capacityfrom 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 (usestrlen()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.
There are no comments for now.