phpcachingmemcachedphpfastcache

Adding tags to cache entry takes a long time using PhpFastCache with Memcached driver


Here is the code:

    public static function applyInvalidationTags(ExtendedCacheItemInterface $cacheItem, $values): void
    {
        $isSomething = false;
        $tags = [];
        foreach ($values[0] as $value) {
            $tags[] = 'product_id_' . $value->id;

            if (!$isSomething && $value->isSomething) {
                $tags[] = 'isSomething';
                $isSomething = true;
            }
        }

        $cacheItem->addTags($tags);
    }

When I remove this line : '$cacheItem->addTags($tags);' my request takes about 1.6s instead of 2.6s. There is always less than 19 objects in $values[0]. Meaning there can be 20 tags maximum on a cache entry.

I need those tags to be able to invalidate the cache when needed. Without that I would have to reduce the caching time drastically, from 2h to minutes.

Any idea why putting tags takes so much time and how I would be able to improve this?

For information, I have good performance when the cache is hit. I'm using Memcached driver, but I tried using Redis instead and I do not have this issue when using it. Sadly my lead does not want to use Redis and insists on using Memcached.


Solution

  • After having investigated, the issue was that I had too many cache entries with the same 'search' tag (like ten of thousands entries).

    PhpfastCache does tagging by creating a cache entry for each tags. Then when adding a tag on a cache entry, it first retrieves it, then modify it and resend it to Memcached. So there was also a memory issue.

    Anyway I do recommend using Redis instead of Memcached as I had less performance issues during testing. Besides Redis is a much more complete caching solution.