Files

179 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2026-04-03 20:55:51 +03:00
<?php
namespace App\Services;
use Illuminate\Support\Facades\Cache;
class CacheService
{
// Cache durations (in seconds)
const CACHE_JOB_LISTINGS = 300; // 5 minutes
const CACHE_SEARCH_RESULTS = 600; // 10 minutes
const CACHE_CATEGORIES = 3600; // 1 hour
const CACHE_STATISTICS = 1800; // 30 minutes
const CACHE_FILTER_OPTIONS = 3600; // 1 hour
const CACHE_FEATURED_JOBS = 900; // 15 minutes
/**
* Generate cache key for job listings
*/
public function jobListingsKey(array $filters = [], int $page = 1, int $perPage = 20): string
{
ksort($filters);
return 'jobs:list:' . md5(serialize($filters) . ":{$page}:{$perPage}");
}
/**
* Generate cache key for search results
*/
public function searchResultsKey(array $filters = [], int $page = 1): string
{
ksort($filters);
return 'jobs:search:' . md5(serialize($filters) . ":{$page}");
}
/**
* Generate cache key for job details
*/
public function jobDetailsKey(int $jobId): string
{
return "job:details:{$jobId}";
}
/**
* Generate cache key for categories
*/
public function categoriesKey(): string
{
return 'categories:all';
}
/**
* Generate cache key for category jobs
*/
public function categoryJobsKey(int $categoryId, int $page = 1): string
{
return "category:{$categoryId}:jobs:{$page}";
}
/**
* Generate cache key for statistics
*/
public function statisticsKey(): string
{
return 'stats:dashboard';
}
/**
* Generate cache key for filter options
*/
public function filterOptionsKey(): string
{
return 'filters:options';
}
/**
* Generate cache key for featured jobs
*/
public function featuredJobsKey(int $limit = 10): string
{
return "jobs:featured:{$limit}";
}
/**
* Generate cache key for recent jobs
*/
public function recentJobsKey(int $days = 7, int $limit = 6): string
{
return "jobs:recent:{$days}:{$limit}";
}
/**
* Clear job-related caches
*/
public function clearJobCaches(): void
{
Cache::tags(['jobs'])->flush();
}
/**
* Clear category caches
*/
public function clearCategoryCaches(): void
{
Cache::tags(['categories'])->flush();
}
/**
* Clear statistics caches
*/
public function clearStatisticsCaches(): void
{
Cache::forget($this->statisticsKey());
}
/**
* Clear all application caches
*/
public function clearAllCaches(): void
{
Cache::flush();
}
/**
* Remember with tags (for better cache management)
*/
public function rememberWithTags(array $tags, string $key, int $ttl, callable $callback)
{
return Cache::tags($tags)->remember($key, $ttl, $callback);
}
/**
* Get cached value or store new one
*/
public function remember(string $key, int $ttl, callable $callback)
{
return Cache::remember($key, $ttl, $callback);
}
/**
* Get from cache
*/
public function get(string $key, $default = null)
{
return Cache::get($key, $default);
}
/**
* Put value in cache
*/
public function put(string $key, $value, int $ttl): bool
{
return Cache::put($key, $value, $ttl);
}
/**
* Forget cache key
*/
public function forget(string $key): bool
{
return Cache::forget($key);
}
/**
* Increment counter (useful for statistics)
*/
public function increment(string $key, int $value = 1): int
{
return Cache::increment($key, $value);
}
/**
* Decrement counter
*/
public function decrement(string $key, int $value = 1): int
{
return Cache::decrement($key, $value);
}
}