删除 Laravel 中的缓存文件

Remove cache files in Laravel

我是 Laravel 的初学者。

我在我的项目中使用Laravel 7。

我的项目中有缓存系统。

我的项目中有缓存键:

  • category
  • category.id
  • category.subcategory.id
  • product.all

等等

我需要删除缓存的功能。

我这样写:

private function deleteCache(string $keyToRemove)
{
    Cache::forget($keyToRemove);
}

通用缓存删除是否可行?

我需要一个函数

  1. 删除选定的键:

    deleteCache(['category.100', 'product.all', 'category.1'])

  2. 删除所有带类别的缓存(例如:category.1、category.all、类别、category.tree、category.subcategory.1 等)。

    deleteCache(['category.*'])

我怎样才能做到?

TL:DR What you need is not available by default, you need customized, wrapper methods that requires "technical" knowledge about the cache driver(underlying technology) you choose.

Laravel缓存支持多种技术(驱动),包括redisdatabasefilememcached等。所有这些驱动都实现了相同的接口.

namespace Illuminate\Contracts\Cache;

interface Store
{
    /**
     * Retrieve an item from the cache by key.
     *
     * @param  string|array  $key
     * @return mixed
     */
    public function get($key);

    /**
     * Retrieve multiple items from the cache by key.
     *
     * Items not found in the cache will have a null value.
     *
     * @param  array  $keys
     * @return array
     */
    public function many(array $keys);

    /**
     * Store an item in the cache for a given number of minutes.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @param  float|int  $minutes
     * @return void
     */
    public function put($key, $value, $minutes);

    /**
     * Store multiple items in the cache for a given number of minutes.
     *
     * @param  array  $values
     * @param  float|int  $minutes
     * @return void
     */
    public function putMany(array $values, $minutes);

    /**
     * Increment the value of an item in the cache.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return int|bool
     */
    public function increment($key, $value = 1);

    /**
     * Decrement the value of an item in the cache.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return int|bool
     */
    public function decrement($key, $value = 1);

    /**
     * Store an item in the cache indefinitely.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return void
     */
    public function forever($key, $value);

    /**
     * Remove an item from the cache.
     *
     * @param  string  $key
     * @return bool
     */
    public function forget($key);

    /**
     * Remove all items from the cache.
     *
     * @return bool
     */
    public function flush();

    /**
     * Get the cache key prefix.
     *
     * @return string
     */
    public function getPrefix();
}

根据您选择的驱动程序 - 您需要自定义方法来实现您的需要。

对于您的第一个问题,以下方法可用于删除多个密钥。

public function deleteCache(array $keys)
{
    foreach ($keys as $key) {
        Cache::forget($key);
    }
}

我熟悉redis,所以我会举一些例子。如果您打算使用 redis 作为缓存驱动程序 - 最好像这样修改该方法;由于redis的delete命令支持一次性删除多个key。这个比上一个更有效

public function deleteCache(array $keys)
{
    Redis::del($keys);
}

一招是小心cache prefix。如果您正在使用缓存前缀(在您的缓存配置文件中定义)-那么您需要将这些前缀添加到键中。

对于你的第二个问题(删除所有类别的缓存),有几种方法可以做到这一点,但其中一些方法 performance/production 不友好。在 Redis 中,您可以执行一些命令,例如 keysscan 来遍历数据库,然后使用返回的结果调用先前定义的方法。

特别是 keys 命令只应格外小心地用于生产环境。

Redis 只是示例 - 如果您要使用 database 缓存驱动程序 - 那么您需要实施方法来满足您的情况。它将需要有关 laravel 如何通过数据库(表、查询等)实现它以及扩展方法将如何使用它(表、查询、列、索引等)的技术知识