top-think / top-think/think-orm

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

Open
#276 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
437
Forks
188
PR merge metrics
No merged PRs in 30d

Description

版本: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;
    }

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in vendor/topthink/think-orm/src/Model.php at Model::delete() and reproduce the reported cache() followed by delete() sequence from the issue. Trace how the delete query handles cache invalidation, then verify that the database row and the cached key are both removed without breaking related deletion behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
database
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.