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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
| <?php namespace app\service;
use think\facade\Cache; use think\facade\Log; use think\exception\InvalidArgumentException;
class CacheService { const CACHE_FILE = 'file'; const CACHE_REDIS = 'redis'; const CACHE_CLUSTER = 'redis_cluster'; const TTL_MINUTE = 60; const TTL_HOUR = 3600; const TTL_DAY = 86400; const TTL_WEEK = 604800; const TTL_MONTH = 2592000;
public function set($key, $value, $ttl = self::TTL_HOUR, $store = self::CACHE_REDIS) { try { $result = Cache::store($store)->set($key, $value, $ttl); Log::info('缓存设置成功', [ 'key' => $key, 'ttl' => $ttl, 'store' => $store, 'size' => strlen(serialize($value)) ]); return $result; } catch (\Exception $e) { Log::error('缓存设置失败', [ 'key' => $key, 'error' => $e->getMessage(), 'store' => $store ]); return false; } }
public function get($key, $default = null, $store = self::CACHE_REDIS) { try { $value = Cache::store($store)->get($key, $default); if ($value !== $default) { Log::debug('缓存命中', [ 'key' => $key, 'store' => $store ]); } else { Log::debug('缓存未命中', [ 'key' => $key, 'store' => $store ]); } return $value; } catch (\Exception $e) { Log::error('缓存获取失败', [ 'key' => $key, 'error' => $e->getMessage(), 'store' => $store ]); return $default; } }
public function remember($key, callable $callback, $ttl = self::TTL_HOUR, $store = self::CACHE_REDIS) { $value = $this->get($key, null, $store); if ($value === null) { $startTime = microtime(true); $value = $callback(); $executeTime = microtime(true) - $startTime; if ($value !== null) { $this->set($key, $value, $ttl, $store); Log::info('缓存回调执行', [ 'key' => $key, 'execute_time' => round($executeTime * 1000, 2) . 'ms', 'store' => $store ]); } } return $value; }
public function delete($key, $store = self::CACHE_REDIS) { try { $result = Cache::store($store)->delete($key); Log::info('缓存删除', [ 'key' => $key, 'store' => $store, 'result' => $result ]); return $result; } catch (\Exception $e) { Log::error('缓存删除失败', [ 'key' => $key, 'error' => $e->getMessage(), 'store' => $store ]); return false; } }
public function setMultiple(array $data, $ttl = self::TTL_HOUR, $store = self::CACHE_REDIS) { try { $success = 0; foreach ($data as $key => $value) { if ($this->set($key, $value, $ttl, $store)) { $success++; } } Log::info('批量缓存设置', [ 'total' => count($data), 'success' => $success, 'store' => $store ]); return $success === count($data); } catch (\Exception $e) { Log::error('批量缓存设置失败', [ 'error' => $e->getMessage(), 'store' => $store ]); return false; } }
public function tag($tags, $store = self::CACHE_REDIS) { return Cache::store($store)->tag($tags); }
public function clearTag($tags, $store = self::CACHE_REDIS) { try { $result = Cache::store($store)->tag($tags)->clear(); Log::info('标签缓存清除', [ 'tags' => $tags, 'store' => $store ]); return $result; } catch (\Exception $e) { Log::error('标签缓存清除失败', [ 'tags' => $tags, 'error' => $e->getMessage(), 'store' => $store ]); return false; } }
public function getStats($store = self::CACHE_REDIS) { try { if ($store === self::CACHE_REDIS) { $redis = Cache::store($store)->handler(); $info = $redis->info(); return [ 'used_memory' => $info['used_memory_human'] ?? 'N/A', 'connected_clients' => $info['connected_clients'] ?? 0, 'total_commands_processed' => $info['total_commands_processed'] ?? 0, 'keyspace_hits' => $info['keyspace_hits'] ?? 0, 'keyspace_misses' => $info['keyspace_misses'] ?? 0, 'hit_rate' => $this->calculateHitRate($info), ]; } return ['message' => '该缓存驱动不支持统计信息']; } catch (\Exception $e) { Log::error('获取缓存统计失败', [ 'store' => $store, 'error' => $e->getMessage() ]); return ['error' => $e->getMessage()]; } }
private function calculateHitRate($info) { $hits = $info['keyspace_hits'] ?? 0; $misses = $info['keyspace_misses'] ?? 0; $total = $hits + $misses; if ($total === 0) { return '0%'; } return round(($hits / $total) * 100, 2) . '%'; } }
|