1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| <?php interface CacheReader { public function get(string $key); public function has(string $key): bool; }
interface CacheWriter { public function set(string $key, $value, int $ttl = 3600): void; public function delete(string $key): void; }
interface CacheStats { public function getHitRate(): float; public function getStats(): array; }
class CacheManager { public function __construct( private CacheReader&CacheWriter $cache ) {} public function remember(string $key, callable $callback, int $ttl = 3600) { if ($this->cache->has($key)) { return $this->cache->get($key); } $value = $callback(); $this->cache->set($key, $value, $ttl); return $value; } public function getWithStats(string $key, CacheReader&CacheWriter&CacheStats $statsCache) { $value = $statsCache->get($key); echo "缓存命中率: " . $statsCache->getHitRate() . "%\n"; echo "缓存统计: " . json_encode($statsCache->getStats()) . "\n"; return $value; } }
class MemoryCache implements CacheReader, CacheWriter { private array $data = []; private array $expiry = []; public function get(string $key) { if (!$this->has($key)) { return null; } return $this->data[$key]; } public function has(string $key): bool { if (!isset($this->data[$key])) { return false; } if (isset($this->expiry[$key]) && $this->expiry[$key] < time()) { $this->delete($key); return false; } return true; } public function set(string $key, $value, int $ttl = 3600): void { $this->data[$key] = $value; $this->expiry[$key] = time() + $ttl; } public function delete(string $key): void { unset($this->data[$key], $this->expiry[$key]); } }
class StatisticsCache extends MemoryCache implements CacheStats { private int $hits = 0; private int $misses = 0; private int $writes = 0; public function get(string $key) { $value = parent::get($key); if ($value !== null) { $this->hits++; } else { $this->misses++; } return $value; } public function set(string $key, $value, int $ttl = 3600): void { parent::set($key, $value, $ttl); $this->writes++; } public function getHitRate(): float { $total = $this->hits + $this->misses; return $total > 0 ? ($this->hits / $total) * 100 : 0; } public function getStats(): array { return [ 'hits' => $this->hits, 'misses' => $this->misses, 'writes' => $this->writes, 'hit_rate' => $this->getHitRate() ]; } }
echo "=== 缓存系统示例 ===\n";
$basicCache = new MemoryCache(); $statsCache = new StatisticsCache();
$cacheManager = new CacheManager($basicCache);
$expensiveData = $cacheManager->remember('user:123', function() { echo "执行昂贵的数据库查询...\n"; return ['id' => 123, 'name' => '张三', 'email' => 'zhangsan@example.com']; });
echo "用户数据: " . json_encode($expensiveData) . "\n";
$cachedData = $cacheManager->remember('user:123', function() { echo "这不应该被执行\n"; return null; });
echo "缓存数据: " . json_encode($cachedData) . "\n";
$statsCache->set('test:key', 'test value'); $statsCache->get('test:key'); $statsCache->get('nonexistent');
$cacheManager->getWithStats('test:key', $statsCache); ?>
|