Contentful logo

Contentful Community

Laravel Framework

#1

Is there a section for questions about the PHP SDK within Laravel? If not, there should be!

0 Likes

#2

Hey Brad,

We have a PHP category for any questions you might have. :smile:

Do you have any specific questions about Laravel?

0 Likes

#3

Hi @gabriel,

Actually, yes. I’m trying to enable the cache option for the delivery client. Looks like there’s no default config to enable/disable it in the Laravel service provider.

0 Likes

#4

@davide might be able to confirm us that :slight_smile:

0 Likes

#5

Hey Brad,

You’re right, at the moment the service provider doesn’t allow setting the cache option. Must have been an oversight from the previous developer, sorry about that.

I’ll try to take a look at this soon, though the caching mechanism has been completed overhauled in version 3 of the SDK: previously it used a custom filesystem implementation, but now it’s going to accept any PSR-6 cache pool, and support automatically warming up during normal execution.

Because of this, I’d rather focus on implementing the new version of the SDK (which is currently in beta) rather than going back and work on something for the old one, which I assume is the one you’re currently running.

As a timeline, the new version of the SDK should be released around the end of April, with the updated Laravel integration coming soon after that.

Cheers,

Davide

0 Likes

#6

Makes sense, but it seems that enabling the cache is fairly straightforward if I modify the ContentfulServiceProvider.php file:

/**
 * Register the service provider.
 *
 * @return void
 */
public function register()
{
    $this->app->singleton(DeliveryClient::class, function ($app) {
        $config = $app['config']['contentful'];

        return (new DeliveryClient(
            $config['delivery.token'],
            $config['delivery.space'],
            $config['delivery.preview'],
            $config['delivery.defaultLocale'],
            $config['delivery.options']
        ))->setIntegration('contentful.laravel', self::VERSION);
    });
}

…and add the options to the contentful.php file under /config:

'delivery.options' => [
    'cacheDir' => 'storage/framework/cache',
]

I can see the cacheDir populate when I dump the $client. I’ve also warmed up the cache according to the tutorial, but it looks like it has only cached the models, not the content. Can you provide some guidance so that I can determine how to get the file system cache working?

0 Likes

#7

That’s by design: the SDK is built to cache space and content types, not entries or assets.

There are several reasons for this:

  • Whereas space and content types don’t change often, that is not usually the case for entries
  • Furthermore, content types are usually rather limited in number (a space can only have 100, IIRC), entries might be several thousands
  • It can actually be hard to query: when querying the API for entries based on certain criteria, the SDK would have to check all local entries and manually do the filtering. Querying content types collection does not happen that often, so it’s not really a problem there

If you really want to cache entries, I suggest you use something like a reverse proxy in front of your application. But our CDN should be fast enough for most use cases, so especially if you use the include operator correctly, you should be fine :slightly_smiling_face:

0 Likes

#8

Can’t you to enable disable the cache in Laravel without the SDK? To enable/disable the cache you have to edit .env file in your Laravel directory. In the file, you will find this command

Cache_Driver = file

Replace “file” with the cache driver you want to use, for example, memcached, varnish, array, etc. To configure the cache, you can do it in config/cache.php file.

Source: https://www.cloudways.com/blog/integrate-laravel-cache/

0 Likes