thinkphp 做整站缓存自定义模型方法
封装一个基础模型,其他模型继承,调用模型的方法
namespace app\common\model;
use think\facade\Cache;
use think\Model;
class BaseModel extends Model
{
private $_cache_list_tag = '_xc2020_cache_list_tag_';// 列表缓存标签
private $_cache_detail_tag = '_xc2020_cache_detail_tag_';// 详细缓存标签
private $_cache_key = '_xc2020_cache_key_';// 缓存后缀
/**
* 执行添加
* @param array $data 数据
* @param bool 是否启用缓存
* @param string $tag 缓存标签
* @return bool
*/
public function execInsert($data, $cache = false, $tag = '')
{
$data['create_time'] = time();
$res = self::create($data);
if (!$res) {
return false;
}
if ($cache) {
$this->deleteListCache($tag);
}
return $res[self::getPk()];
}
/**
* @param int $pk 主键值
* @param bool 是否启用缓存
* @param string $tag 缓存标签
* @return bool
*/
public function execDelete($pk, $cache = false, $tag = '')
{
$res = self::destroy($pk);
if (!$res) {
return false;
}
if ($cache) {
$this->deleteListCache($tag);
$this->deleteDetailCache($pk);
}
return $res;
}
/**
* @param int $pk 主键值
* @param string $sql 原生sql
* @param bool 是否启用缓存
* @param string $tag 缓存标签
* @return bool
*/
public function execDelete2($pk,$query, $cache = false, $tag = '')
{
$res = self::query($query);
if ($res===false) {
return false;
}
if ($cache) {
$this->deleteLis