You've already forked Atomcms-edit
163 lines
4.6 KiB
PHP
Executable File
163 lines
4.6 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
readonly class CacheService
|
|
{
|
|
private const array CACHE_KEYS = [
|
|
'website_settings' => 'website_settings',
|
|
'website_permissions' => 'website_permissions',
|
|
'points_service_settings' => 'points_service_settings',
|
|
'radio_leaderboard' => 'radio_leaderboard*',
|
|
'radio_leaderboard_weekly' => 'radio_leaderboard_weekly',
|
|
'radio_leaderboard_monthly' => 'radio_leaderboard_monthly',
|
|
'blocked_words' => 'blocked_words',
|
|
'content_moderation_settings' => 'content_moderation_settings',
|
|
'radio_settings_general' => 'radio_settings_general',
|
|
'radio_settings_dj' => 'radio_settings_dj',
|
|
'radio_ranks_active' => 'radio_ranks_active',
|
|
'radio_banners_active' => 'radio_banners_active',
|
|
'radio_shouts_recent' => 'radio_shouts_recent',
|
|
'rare_value_categories' => 'rare_value_categories',
|
|
'rare_value_categories_active' => 'rare_value_categories_active',
|
|
'staff_positions' => 'staff_positions',
|
|
'staff_ids' => 'staff_ids',
|
|
'hotel_teams' => 'hotel_teams',
|
|
];
|
|
|
|
private static function useRedis(): bool
|
|
{
|
|
return config('cache.default') === 'redis';
|
|
}
|
|
|
|
public static function clearWebsiteSettings(): void
|
|
{
|
|
self::forget('website_settings');
|
|
}
|
|
|
|
public static function clearWebsitePermissions(): void
|
|
{
|
|
self::forget('website_permissions');
|
|
}
|
|
|
|
public static function clearPointsSettings(): void
|
|
{
|
|
self::forget('points_service_settings');
|
|
}
|
|
|
|
public static function clearPointsUser(int $userId): void
|
|
{
|
|
self::forget('user_points_' . $userId);
|
|
}
|
|
|
|
public static function clearLeaderboards(): void
|
|
{
|
|
self::forgetPattern('radio_leaderboard*');
|
|
}
|
|
|
|
public static function clearContentModeration(): void
|
|
{
|
|
self::forget('blocked_words');
|
|
self::forget('content_moderation_settings');
|
|
}
|
|
|
|
public static function clearRadioSettings(): void
|
|
{
|
|
self::forget('radio_settings_general');
|
|
self::forget('radio_settings_dj');
|
|
self::forget('radio_ranks_active');
|
|
self::forget('radio_banners_active');
|
|
}
|
|
|
|
public static function clearRadioShouts(): void
|
|
{
|
|
self::forget('radio_shouts_recent');
|
|
}
|
|
|
|
public static function clearRareValueCategories(): void
|
|
{
|
|
self::forget('rare_value_categories');
|
|
self::forget('rare_value_categories_active');
|
|
}
|
|
|
|
public static function clearStaff(): void
|
|
{
|
|
self::forget('staff_positions');
|
|
self::forget('staff_ids');
|
|
self::forget('hotel_teams');
|
|
}
|
|
|
|
public static function clearApiCache(): void
|
|
{
|
|
self::forgetPattern('api_cache_*');
|
|
}
|
|
|
|
public static function clearLeaderboard(): void
|
|
{
|
|
self::forget('leaderboard_credits');
|
|
self::forget('leaderboard_duckets');
|
|
self::forget('leaderboard_diamonds');
|
|
self::forget('leaderboard_online');
|
|
self::forget('leaderboard_respects');
|
|
self::forget('leaderboard_achievements');
|
|
}
|
|
|
|
public static function clearHelpCenter(): void
|
|
{
|
|
self::forget('help_center_categories');
|
|
self::forget('website_rules_categories');
|
|
}
|
|
|
|
public static function clearAllAppCache(): void
|
|
{
|
|
foreach (self::CACHE_KEYS as $key) {
|
|
if (str_contains($key, '*')) {
|
|
self::forgetPattern($key);
|
|
} else {
|
|
self::forget($key);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static function forget(string $key): void
|
|
{
|
|
try {
|
|
$prefix = config('cache.prefix', 'atomcms_cache_');
|
|
Cache::forget($prefix . $key);
|
|
} catch (\Exception) {
|
|
Cache::forget($key);
|
|
}
|
|
}
|
|
|
|
private static function forgetPattern(string $pattern): void
|
|
{
|
|
if (self::useRedis()) {
|
|
try {
|
|
$connection = config('cache.stores.redis.connection', 'cache');
|
|
$prefix = config('cache.prefix', 'atomcms_cache_');
|
|
$fullPattern = $prefix . $pattern;
|
|
$redis = Redis::connection($connection)->client();
|
|
$keys = $redis->keys($fullPattern);
|
|
if (! empty($keys)) {
|
|
$redis->del($keys);
|
|
}
|
|
|
|
return;
|
|
} catch (\Exception) {
|
|
}
|
|
}
|
|
|
|
$keys = self::CACHE_KEYS;
|
|
foreach ($keys as $key) {
|
|
if (fnmatch($pattern, $key, FNM_NOESCAPE)) {
|
|
self::forget($key);
|
|
}
|
|
}
|
|
}
|
|
}
|