Contentful logo

Contentful Community

Caching implemented but no difference

#1

I added the cacheItemPool to my contentful client, but nothing is cached…

I even get more api calls when i activate the cache…

#2

Hey Olivier,

can you post a snippet of how you implemented the cache?

Thank you

Davide

#3

I dump a message into the set function of InstanceRepository and items are never being saved…

     $this->app->getContainer()['cms.cache'] = function ($container) {
        $config = $container->get('settings')['redis'];   
        if (Helper::isEqual($config['enable'], false)) {
            return null;
        }

        try {
            $redis = new \Redis();
            $redis->connect($config['host'], $config['port'], 1800);
            $redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);

            return new RedisAdapter($redis, 'cms', 1800);
        } catch (\Exception $e) {
            $container['monolog']->addError($e);

            return null;
        }
    };

getCacheClient() calls the above code…

    $cacheItemPool = $this->getCacheClient();
    $config = $this->app->getContainer()->get('settings')['contentful'];
    $keys = $this->app->getContainer()['cms_key']->getKey();

    $client = new Client(
        Helper::isEqual($SLIM_MODE, BL_MODE_PROD) ? $keys['accessToken'] : $keys['previewToken'],
        $keys['spaceID'], $config['main-env'],
        !Helper::isEqual($SLIM_MODE, BL_MODE_PROD),
        null,
        $options = [
            'cache' => $cacheItemPool,
        ]
    );
#4

Hey Olivier,

as it’s explained in this tutorial, you need to warm up the cache, otherwise it’s useless.

There are 3 ways of doing this:

  • Using the CLI utility
  • Manually calling the cache warmer in your app’s workflow
  • Setting the client to automatically cache content through regular use

I suggest you use option 3 during development, as it’s the easiest and quickest to set up (just pass 'autoWarmup' => true as an extra parameter in the $options array), and then move to either 1 or 2 when the moment comes.

Davide

#5

I see!

i will try that thanks

#6

Okay seems to be working.

But there is one thing i dont understand. When i display all the keys in Redis, i only see keys related to Environment and Content-type…

The SDK is not supposed to cache Entry and Asset also?

For example, if i query all entries of content-type Location that has a linked Entry custom_image, when i get this linked entry (location->get(‘custom_image’)), this linked entry is not supposed to be cached with a key similar to : contentful-PREVIEW-naxc0d1dcrd4-master-ContentType-custom_image-ID_OF_THE_ENTRY??

Thanks

#7

Hey Olivier,

no, the SDK by design does not cache entries or assets. This might change in the future, but right now only content types and environment (locales) info are cached.

Davide

Entry and asset not cached?
#8

I have a doubt regarding this topic, I’m using this on a php file

$clientOptions= array('autoWarmup' => true,'cacheContent' => true );

$client = new Client($accessToken, $spaceID,'master',false,$contenfulLocale,$clientOptions);   

How can I refresh or delete the cache? I read the tutorial but there is not clear info about that its only says:
" you should always try to do a cache purge on content type changes, either manually or using webhooks."…

The question is how?

Thanks in advance