缓存的作用,减少数据库压力。
什么时候调用缓存?缓存 === sql最新数据时,这时需要调用sql查询吗?
缓存是什么,泛指一段时间内的数据不存在更新而被保存的信息。
<?php
namespace app\member\model;
use app\BaseModel;
use think\facade\Cache;
//*------------------------------------------------------ */
//-- 2021-04-28
//-- lianyu001
/* ------------------------------------------------------ */
class SupplierModel extends BaseModel {
protected $table = 'supplier_apply';
public $pk = 'id';
private $mkey = "supplier_apply_mkey_";
/* ------------------------------------------------------ */
//-- 清除memcache
/* ------------------------------------------------------ */
public function cleanMemcache($id = 0) {
Cache::rm($this->mkey . "row" . $id);
Cache::rm($this->mkey . "list" . $id);
Cache::rm($this->mkey . 'filed');
}
/**
* 单条数据
*/
public function getRows($user_id = 0) {
$CacheName = $this->mkey . "row" . $user_id;
$data = Cache::get($CacheName);
if (empty($data)) {
$rows = (array) $this->where('user_id = "' . $user_id . '" and is_del = 0')->find();
if ($rows) {
Cache::set($CacheName, $rows, $this->cache_time);
}
}
return $rows;
}
/**
* 数据获取
* @param type $id 数据标识
* @return type
*/
public function getlist($id = 0, $sort = '') {
$CacheName = $this->mkey . "list" . $id;
$rows = Cache::get($CacheName);
$where = $id == 0 ? 'is_del = 0' : 'id = "' . $id . '" and is_del = 0';
if (empty($rows)) {
$count = $this->where($where)->count(); //空数据处理,空数据不能 转为数组
if ($count > 0) {
if ($id == 0) {
$sort = $sort ? $sort : $this->pk . ' desc';
$rows = $this->where($where)->order($sort)->select()->toArray();
} else {
$rows = $this->where($where)->find()->toArray();
}
}
if ($rows) {
Cache::set($CacheName, $rows, $this->cache_time);
}
}
return $rows;
}
/**
* 数据更新
* 验证字段
* @param type $data
* @param type $up 1为添加,2为更新
* @return string 自增长标识 or 影响行数
*/
function up($data=[], $up = 1) {
if(empty($data)){
$data= input();
}
$up == 1 ? $data["addtime"] > 0 ? $data["addtime"] : $data["addtime"] = $this->time : $data["uptime"] > 0 ? $data["uptime"] : $data["uptime"] = $this->time;
if (empty($data["is_del"])) {
$data['is_del'] = 0;
}
$insert = $this->allowField($data);
if ($up == 1) {
return $this->insertGetId($insert);
} else if ($up == 2) {
if (empty($insert[$this->pk])) {
return "Missing field " . $this->pk;
}
$this->cleanMemcache($insert[$this->pk]);
return $this->where($this->pk, $insert[$this->pk])->update($insert);
}
}
/**
* 获取数据表字段
*/
function allField() {
$CacheName = $this->mkey . 'field';
$data = Cache::get($CacheName);
if (empty($data) == false) {
return $data;
}
//获取数据库字段
$seltable = "SELECT TABLE_NAME,COLUMN_NAME,IS_NULLABLE,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT "
. "FROM information_schema.COLUMNS WHERE table_schema = '" . config("database.database") . "' "//. "#表所在数据库 "
. "AND table_name = '" . $this->table . "';"; //#你要查的表
$res = $this->query($seltable);
if (count($res) > 0) {
Cache::set($CacheName, $res, $this->cache_time);
}
return $res;
}
/**
* 数据字段过滤
* @param type $data 验证字段数组
* @return array 提取数据表字段返回
*/
function allowField($data) {
$return = [];
$allfield = $this->allField();
foreach ($allfield as $val) {
if (isset($data[$val['COLUMN_NAME']])) {
$return[$val['COLUMN_NAME']] = $data[$val['COLUMN_NAME']];
}
}
return $return;
}
}