src/Cache/functions.php source

1 <?php
2
3 namespace mini;
4
5 use mini\Cache\NamespacedCache;
6 use Psr\SimpleCache\CacheInterface;
7
8 // Register SimpleCache service
9 Mini::$mini->addService(CacheInterface::class, Lifetime::Singleton, fn() => Mini::$mini->loadServiceConfig(CacheInterface::class));
10
11 /**
12 * Get cache instance
13 *
14 * Returns PSR-16 SimpleCache instance from container.
15 * With smart fallback: APCu > SQLite in /tmp > Filesystem in /tmp
16 *
17 * @param string|null $namespace Optional namespace for cache isolation
18 * @return \Psr\SimpleCache\CacheInterface Cache instance
19 */
20 function cache(?string $namespace = null): CacheInterface {
21 $cache = Mini::$mini->get(CacheInterface::class);
22
23 // Return namespaced cache if namespace provided
24 if ($namespace !== null) {
25 return new NamespacedCache($cache, $namespace);
26 }
27
28 return $cache;
29 }
30