1+ <?php
2+
3+ namespace App\Application\Services;
4+
5+ use Illuminate\Support\Facades\Cache;
6+ use Illuminate\Support\Facades\Log;
7+
8+ abstract class BaseService
9+ {
10+ /**
11+ * Default cache TTL in seconds (1 hour).
12+ */
13+ protected int $cacheTtl = 3600;
14+
15+ /**
16+ * Cache a result with the given key.
17+ */
18+ protected function cache(string $key, callable $callback, ?int $ttl = null)
19+ {
20+ return Cache::remember($key, $ttl ?? $this->cacheTtl, $callback);
21+ }
22+
23+ /**
24+ * Forget cached result by key.
25+ */
26+ protected function forgetCache(string $key): bool
27+ {
28+ return Cache::forget($key);
29+ }
30+
31+ /**
32+ * Log service operation.
33+ */
34+ protected function log(string $message, array $context = []): void
35+ {
36+ Log::info($message, array_merge([
37+ 'service' => static::class,
38+ ], $context));
39+ }
40+
41+ /**
42+ * Log error during service operation.
43+ */
44+ protected function logError(string $message, \Throwable $exception, array $context = []): void
45+ {
46+ Log::error($message, array_merge([
47+ 'service' => static::class,
48+ 'exception' => $exception->getMessage(),
49+ 'file' => $exception->getFile(),
50+ 'line' => $exception->getLine(),
51+ ], $context));
52+ }
53+ }
0 commit comments