清除 Laravel 中的缓存文件

4

我是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、category.tree、category.subcategory.1等)。

    deleteCache(['category.*'])

如何实现呢?

1个回答

1
TL:DR 您需要的功能默认不可用,您需要定制包装器方法,并需要了解所选缓存驱动程序(基础技术)的"技术"知识。
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命令支持一次删除多个键。这比以前的方法更有效。
public function deleteCache(array $keys)
{
    Redis::del($keys);
}

有一个技巧是要注意缓存前缀。如果您正在使用缓存前缀(在缓存配置文件中定义),那么您需要将这些前缀添加到键中。

对于您的第二个问题(删除所有类别的缓存),有几种方法可以实现,但其中一些方法可能不太适合生产环境/性能。在redis中,您可以执行一些命令,如keysscan,以遍历数据库,然后使用返回的结果调用先前定义的方法。

特别是keys命令应该小心谨慎地在生产环境中使用。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接