contentful / contentful-laravel

Laravel integration for the Contentful SDK.
MIT License
44 stars 31 forks source link

Contentful cache not warming correctly #41

Open auredeco opened 3 years ago

auredeco commented 3 years ago

Hi

I'm running into some trouble when clearing/warming the contentful cache. Whenever the cache gets cleared/warmed, I receive a succesmessage but when I check my page some content has not been updated. This occurs when making a field blank. I've seen this happening two times on a Rich Text field (not sure if it has something to do with this, but it might be useful to mention it)

Steps to reproduce:

  1. Change content on contentful from text to blank
  2. Clear/Warm cache
  3. See content is not updated

Versions: laravel/framework: 8.44 contentful/laravel: 8.1 contentful/richt-text: 3.2

Sebb767 commented 3 years ago

Hi @auredeco ,

this sounds like an Issue with the core library. I'll try to reproduce it there and get back to you.

auredeco commented 3 years ago

Thanks @Sebb767! Did you find the time to do this yet? Or should I rather create an issue on the core library page?

Sebb767 commented 3 years ago

Hi @auredeco ,

so far I've failed to reproduce this locally - the cache updates fine, even when not clearing beforehand. I've tried both updating a normal text field as well as a rich text field and clearing them as well as just changing the text.

What cache implementation are you using?

Or should I rather create an issue on the core library page?

Don't worry, I'll update them as necessary :)

veewee commented 3 years ago

Hello @Sebb767,

I am in @auredeco 's team. We use redis as cache backend:

CACHE_DRIVER=redis
  /** @var ClientOptions $options */
  $options->withCache(
      (new CacheItemPoolFactory())->getCacheItemPool(
          env('CONTENTFUL_USE_PREVIEW') ? Client::API_DELIVERY : Client::API_PREVIEW,
          env('CONTENTFUL_SPACE_ID'),
          env('CONTENTFUL_ENVIRONMENT_ID')
      ),
      autoWarmup: false,
      cacheContent: true
  );

We wrapped the cache helpers from the core package into artisan commands:

use Contentful\Delivery\Cache\CacheClearer;
use Contentful\Delivery\Client\ClientInterface;
use Illuminate\Console\Command;
use RuntimeException;

class ClearCache extends Command
{
    protected $signature = 'contentful:cache:clear';
    protected $description = 'Clear the contentful cache';

    public function __invoke(ClientInterface $client, CacheClearer $cacheClearer): int
    {
        $cacheClearer->clear(
            cacheContent: true
        );

        $this->info(
            sprintf(
                'Cache cleared for space "%s" on environment "%s" using API "%s".',
                $client->getSpaceId(),
                $client->getEnvironmentId(),
                $client->getApi()
            )
        );

        return self::SUCCESS;
    }
}
class WarmCache extends Command
{
    protected $signature = 'contentful:cache:warm';
    protected $description = 'Warm the contentful cache';

    public function __invoke(ClientInterface $client, CacheWarmer $cacheWarmer): int
    {
        $cached = $cacheWarmer->warmUp(
            cacheContent: true
        );

        if (! $cached) {
            throw new RuntimeException(
                'The SDK could not warm up the cache. Errored whilst writing to the cache back-end. Is it available?'
            );
        }

        $this->info(
            sprintf(
                'Cache warmed up for space "%s" on environment "%s" using API "%s".',
                $client->getSpaceId(),
                $client->getEnvironmentId(),
                $client->getApi()
            )
        );

        return self::SUCCESS;
    }
}

The commands finish successfully, but the field that has been emptied in contentful remains filled in the laravel application.

veewee commented 3 years ago

We investigated this issue a bit further today. The problem occurs when having multiple (unused) locales. In that case, the cache key for the language is set right - but other cache keys keep old values. After removing an unused locale, the issue is resolved. It even happens if you explicitly set a default locale on the client. We were not able to detect WHY it behaves like this.

An other option is to not cache the content. Since most data in our application comes in lists, we don't benefit from content cache. It is even slower with content cache enabled than without content cache for us.

Sebb767 commented 3 years ago

Hi @veewee ,

thanks for investigating this issue further! Locales are a really good tip.

We were not able to detect WHY it behaves like this.

This sounds like an empty locale causes a bug somewhere which prevents the cache from refreshing correctly. I'll investigate this further.

An other option is to not cache the content. [...] It is even slower with content cache enabled than without content cache for us.

Would this work for you for now? I'll of course fix the bug anyway and ping you once it's resolved, in case you want to revert to using the cache later again :)

veewee commented 3 years ago

Hello @Sebb767,

Yes, the solutions provided above is workable for us at the moment.