1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-17 07:45:29 +08:00
2019-12-13 00:10:10 +08:00

58 lines
1.1 KiB
PHP

<?php
namespace App\Library\Cache;
use App\Models\Category as CategoryModel;
use App\Exceptions\NotFound as ModelNotFoundException;
class Category extends \Phalcon\Di\Injectable
{
private $lifetime = 86400 * 30;
public function getOrFail($id)
{
$result = $this->getById($id);
if (!$result) {
throw new ModelNotFoundException('category.not_found');
}
return $result;
}
public function get($id)
{
$cacheOptions = [
'key' => $this->getKey($id),
'lifetime' => $this->getLifetime(),
];
$result = CategoryModel::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 "category:{$id}";
}
public function getLifetime()
{
return $this->lifetime;
}
}