mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-17 07:45:29 +08:00
58 lines
1.1 KiB
PHP
58 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Library\Cache;
|
|
|
|
use App\Models\User as UserModel;
|
|
use App\Exceptions\NotFound as ModelNotFoundException;
|
|
|
|
class User extends \Phalcon\Di\Injectable
|
|
{
|
|
|
|
private $lifetime = 86400 * 30;
|
|
|
|
public function getOrFail($id)
|
|
{
|
|
$result = $this->getById($id);
|
|
|
|
if (!$result) {
|
|
throw new ModelNotFoundException('user.not_found');
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function get($id)
|
|
{
|
|
$cacheOptions = [
|
|
'key' => $this->getKey($id),
|
|
'lifetime' => $this->getLifetime(),
|
|
];
|
|
|
|
$result = UserModel::query()
|
|
->where('id = :id:', ['id' => $id])
|
|
->cache($cacheOptions)
|
|
->execute()
|
|
->getFirst();
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
$key = $this->getKey($id);
|
|
|
|
$this->modelsCache->delete($key);
|
|
}
|
|
|
|
public function getKey($id)
|
|
{
|
|
return "user:{$id}";
|
|
}
|
|
|
|
public function getLifetime()
|
|
{
|
|
return $this->lifetime;
|
|
}
|
|
|
|
}
|