0%
缓存调用封装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?php
namespace App\Utils;
use Closure; use Hyperf\Utils\ApplicationContext; use Psr\SimpleCache\CacheInterface;
class Cache { public static function getInstance() { return ApplicationContext::getContainer()->get(CacheInterface::class); }
public static function remember($key, $ttl, Closure $callback) { $instance = self::getInstance();
$value = $instance->get($key);
if (!is_null($value)) { return $value; }
$instance->set($key, $value = $callback(), $ttl);
return $value; }
public static function __callStatic($name, $arguments) { return self::getInstance()->$name(...$arguments); } }
|
DEMO
1 2 3
| $user_city = Cache::remember("user_city|uid:{$user['id']}", 3600, function () use ($user) { return UserCityModel::where('uid', $user['id'])->orderBy('type')->get(); });
|