1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-24 04:01:31 +08:00
xiaochong0302 0342331d89 1.重命名kg_img_url
2.调整错误页
3.模拟topic数据
2020-04-12 17:27:33 +08:00

102 lines
1.8 KiB
PHP

<?php
namespace App\Caches;
use Phalcon\Cache\Backend\Redis as RedisCache;
use Phalcon\Mvc\User\Component;
use Yansongda\Supports\Collection;
abstract class Cache extends Component
{
/**
* @var RedisCache
*/
protected $cache;
public function __construct()
{
$this->cache = $this->getDI()->get('cache');
}
/**
* 获取缓存内容
*
* @param mixed $id
* @return mixed
*/
public function get($id = null)
{
$key = $this->getKey($id);
if (!$this->cache->exists($key)) {
$content = $this->getContent($id);
/**
* 原始内容为空,设置较短的生存时间,简单防止穿透
*/
$lifetime = $content ? $this->getLifetime() : 5 * 60;
$this->cache->save($key, $content, $lifetime);
} else {
$content = $this->cache->get($key);
}
if (is_array($content)) {
$content = new Collection($content);
}
return $content;
}
/**
* 删除缓存内容
*
* @param mixed $id
*/
public function delete($id = null)
{
$key = $this->getKey($id);
$this->cache->delete($key);
}
/**
* 重建缓存内容
*
* @param mixed $id
*/
public function rebuild($id = null)
{
$this->delete($id);
$this->get($id);
}
/**
* 获取缓存有效期
*
* @return int
*/
abstract public function getLifetime();
/**
* 获取键值
*
* @param mixed $id
* @return string
*/
abstract public function getKey($id = null);
/**
* 获取原始内容
*
* @param mixed $id
* @return mixed
*/
abstract public function getContent($id = null);
}