traffske
traffske

Reputation: 111

Remove cache files in Laravel

I am beginner in Laravel.

I use in my project Laravel 7.

I have cache system in my project.

I have cache in my project with keys:

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

etc.

I need function to remove cache.

I write this:

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

Is universal cache removal possible?

I need a function that will be

  1. Remove selected keys:

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

  2. Remove all cache with category (for example: category.1, category.all, category, category.tree, category.subcategory.1 etc).

    deleteCache(['category.*'])

How can I make it?

Upvotes: 4

Views: 2270

Answers (1)

Ersoy
Ersoy

Reputation: 9586

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 cache supports multiple technologies(drivers) including redis, database, file, memcached etc. All these drivers implement the same interface.

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();
}

Depending on the driver you choose - you need customized methods to achieve what you need.

For your first question, the following method would be useful to remove multiple keys.

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

I am familiar with redis so i will give examples around it. If you are going to use redis as cache driver - it is better to modify that method like this; Since redis's delete command supports deleting multiple keys at once. This one is more effective than the previous one.

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

One trick is to be careful about cache prefix. If you are using cache prefix(defined in your cache config file) - then you need to prepend those prefix to keys.

For your second question(Remove all cache with category) there are several ways to do it but some of them wouldn't be performance/production friendly. In redis you may execute some command such as keys or scan to iterate through database and then invoke the previously defined method with the returned results.

Especially keys command should only be used in production environments with extreme care.

Redis is only example - if you are going to use database cache driver - then you need to implement methods to satisfy your case. It will require technical knowledge about the both how laravel implements it via database(tables, queries etc) and how your extended methods will use it(tables, queries, columns, indexes etc)

Upvotes: 1

Related Questions