lru_create(int $capacity): LruCache Creates and returns a new LruCache instance with the given fixed capacity.
LruCache Class
Constructor
new LruCache(int $capacity) Creates a cache with maximum capacity $capacity. Throws an Exception if $capacity <= 0.
Core Operations
set(string $key, $value, $ttlSeconds = 0): void Inserts or updates $key with $value and marks it as most recently used. If $ttlSeconds > 0, the entry expires after $ttlSeconds seconds (supports int or float). If $ttlSeconds == 0, the entry does not expire. When the cache is full, expired entries are purged first; if still full, the least recently used key is evicted. Throws an Exception if $ttlSeconds is negative or not a number.
get(string $key, $default = null) Returns the cached value for $key and marks it as most recently used. Increments hits on a cache hit. If the key does not exist or has expired, increments misses and returns $default.
has(string $key): bool Returns true if $key exists and has not expired, false otherwise. Does not update recency order or hit/miss counters.
delete(string $key): bool Removes $key from the cache. Returns true if the key existed, false otherwise.
clear(): void Removes all entries from the cache.
purgeExpired(): int Removes all expired entries and returns the number of entries removed.
Inspection & Statistics
size(): int Returns the current number of active (non-expired) entries in the cache.
capacity(): int Returns the maximum capacity of the cache.
keys(): array Returns an array of active keys ordered from most recently used to least recently used.
hits(): int Returns the total number of cache hits.
misses(): int Returns the total number of cache misses.
hitRate(): float Returns hits / (hits + misses), or 0.0 if no get() calls have been made.
resetStats(): void Resets hits and misses counters to 0.
stats(): array Returns an associative array with keys: "hits", "misses", "hit_rate", "size", and "capacity".
Error Handling
All invalid inputs throw Exception with clear, prefixed messages:
Invalid capacity (<= 0): lru: capacity must be a positive integer (> 0), got 0
Negative TTL: lru: TTL must be >= 0 seconds, got -5
Non-numeric TTL: lru: TTL must be a non-negative number (int or float)
Examples
01Quick start
Bundled with the compiler — import "lru"; works offline in any file, or add it to a project with hphp install lru.
import "lru";
$cache = lru_create(2);
$cache->set("a", "alpha");
$cache->set("b", "beta");
// Access "a" so it becomes the most recently used key
println($cache->get("a")); // "alpha"
// Adding "c" evicts the least recently used key ("b")
$cache->set("c", "gamma");
println($cache->has("b")); // false
println($cache->keys()); // ["c", "a"]
// Set a key with a 60-second TTL
$cache->set("session", ["user" => "alice"], 60);
// Inspect cache statistics
$stats = $cache->stats();
println($stats["hits"]); // 1
println($stats["misses"]); // 0
println($stats["hit_rate"]); // 1.0
Try it
hphp install lru
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_lru.hphp.