Last active
September 29, 2021 04:26
-
-
Save 5SMNOONMS5/9708e717440795e1d9d12e3f6d9c24f1 to your computer and use it in GitHub Desktop.
CacheService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Service\Cache; | |
use Illuminate\Support\Facades\Cache; | |
final class CacheService | |
{ | |
/** | |
* Is cache has value by given key and tags | |
* | |
* @param array $tags | |
* @param string $key | |
* | |
* @return mixed | |
*/ | |
public static function has(array $tags, string $key) | |
{ | |
return Cache::tags($tags)->has($key); | |
} | |
/** | |
* @param array $tags | |
* @param string $key | |
* | |
* @return mixed | |
*/ | |
public static function get(array $tags, string $key) | |
{ | |
return Cache::tags($tags)->get($key); | |
} | |
/** | |
* @param array $tags | |
* @param string $key | |
* @param $value | |
* @param int $ttl 默認 60 分鐘 | |
*/ | |
public static function put(array $tags, string $key, $value, $ttl = 3600) | |
{ | |
Cache::tags($tags)->put($key, $value, $ttl); | |
} | |
/** | |
* flush cache with tags or flush entire cache | |
* | |
* @param array $tags | |
*/ | |
public static function flush($tags = NULL) | |
{ | |
if (!$tags) { | |
Cache::flush(); | |
} else { | |
$tags = is_array($tags) ? $tags : [$tags]; | |
Cache::tags($tags)->flush(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment