1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-08 09:49:49 +08:00
2019-12-18 16:01:17 +08:00

58 lines
1.1 KiB
PHP

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