think-orm icon indicating copy to clipboard operation
think-orm copied to clipboard

v2.0.40 使用cache()后,delete删除数据时不会删除缓存

Open ChinaMoli opened this issue 4 years ago • 9 comments

版本:v2.0.40

<?php

use app\model\Template;

$template = Template::cache(86400)->find(1);

// 更新操作会删除缓存 think_test.t_template|1
// $template->save(['name' => 'new_name']);

// 删除操作并不会删除缓存
// 数据库已经删除,但是缓存还在 think_test.t_template|1
$template->delete();

查看源码后,修改/vendor/topthink/think-orm/src/Model.php delete() 方法后可以解决 修改前:

    /**
     * 删除当前的记录
     * @access public
     * @return bool
     */
    public function delete(): bool
    {
        if (!$this->exists || $this->isEmpty() || false === $this->trigger('BeforeDelete')) {
            return false;
        }

        // 读取更新条件
        $where = $this->getWhere();

        $db = $this->db();

        $db->transaction(function () use ($where, $db) {
            // 删除当前模型数据
            $db->where($where)->delete();

            // 关联删除
            if (!empty($this->relationWrite)) {
                $this->autoRelationDelete();
            }
        });

        $this->trigger('AfterDelete');

        $this->exists   = false;
        $this->lazySave = false;

        return true;
    }

修改后:

    /**
     * 删除当前的记录
     * @access public
     * @return bool
     */
    public function delete(): bool
    {
        if (!$this->exists || $this->isEmpty() || false === $this->trigger('BeforeDelete')) {
            return false;
        }

        // 读取更新条件
        $where = $this->getWhere();

        $db = $this->db();

        $db->transaction(function () use ($where, $db) {
            // 删除当前模型数据
            $db->where($where)
                ->cache(true)
                ->setOption('key', $this->key)
                ->delete();

            // 关联删除
            if (!empty($this->relationWrite)) {
                $this->autoRelationDelete();
            }
        });

        $this->trigger('AfterDelete');

        $this->exists   = false;
        $this->lazySave = false;

        return true;
    }

不知道这样修改后会不会出现问题,麻烦官方修复一下

ChinaMoli avatar Jul 07 '21 05:07 ChinaMoli

不修改think-orm的临时解决方法

<?php

use app\model\Template;
use think\facade\Cache;

$id = 1;
$template = Template::cache(86400)->find($id);

$key = 'think_' . $template->getConfig('database') . '.' . $template->getTable() . '|' . $id;
Cache::has($key) && Cache::delete($key);
$template->delete();

ChinaMoli avatar Jul 07 '21 06:07 ChinaMoli

你查询的时候用了cache 但delete的时候并没有调用cache方法 当然不会删除

liu21st avatar Jul 14 '21 07:07 liu21st

好的,谢谢

ChinaMoli avatar Jul 15 '21 08:07 ChinaMoli

但是我前面的数据都是通过缓存获取到的,然后我调用的是模型delete() 按照您的意思是要这样写吗

<?php

use app\model\Template;

$template = Template::cache(86400)->find(1);

$template->cache(86400)->delete();

ChinaMoli avatar Jul 15 '21 08:07 ChinaMoli

但是我前面的数据都是通过缓存获取到的,然后我调用的是模型delete() 按照您的意思是要这样写吗

<?php

use app\model\Template;

$template = Template::cache(86400)->find(1);

$template->cache(86400)->delete();

你确定你看过手册了么

liu21st avatar Jul 16 '21 06:07 liu21st

是这个吧https://www.kancloud.cn/manual/thinkphp6_0/1037553 我没有明白的是

<?php

use app\model\Template;

$template = Template::cache(86400)->find(1);
$template->name = 'new_name';
$template->save();

这样是会删除缓存 但是

<?php

use app\model\Template;

$template = Template::cache(86400)->find(1);
$template->delete();

并不会删除缓存,但是数据库数据确实被删除了 也都是通过主键进行操作的

ChinaMoli avatar Jul 16 '21 06:07 ChinaMoli

我用最新版本测试似乎并没发现问题

liu21st avatar Jul 19 '21 01:07 liu21st

测试代码在:app\controller\Index.php 内 think-orm 版本是 v2.0.41 thinkphp.test.zip 有时间您可以看一下

ChinaMoli avatar Jul 19 '21 06:07 ChinaMoli

请问是有问题吗

ChinaMoli avatar Jul 30 '21 06:07 ChinaMoli