Skip to content

Instantly share code, notes, and snippets.

@5SMNOONMS5
Last active September 29, 2021 04:26
Show Gist options
  • Save 5SMNOONMS5/9708e717440795e1d9d12e3f6d9c24f1 to your computer and use it in GitHub Desktop.
Save 5SMNOONMS5/9708e717440795e1d9d12e3f6d9c24f1 to your computer and use it in GitHub Desktop.
CacheService
<?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