php 简单的缓存model

2 篇文章 0 订阅
1 篇文章 0 订阅

缓存的作用,减少数据库压力。

什么时候调用缓存?缓存 === 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;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值