Problem/Motivation

When rendering or loading Cloudinary assets that use the media source the Cloudinary media API is hit multiple times for each individual asset. When loading multiple assets simultaneously this makes a page request very slow and risks hitting the Cloudinary API limits.

Steps to reproduce

  1. Create a cloudinary media type
  2. Use the media type in a content type
  3. Add content with multiple cloudinary media entities

Proposed resolution

There are two main ways the API is hit:

  • Via the CloudinaryAssetHelper::loadAssetByPublicId() method. This is called for nearly every media source attribute, so multiple times for each entity
  • Via Cloudinary::getMetadataAttributes(), which is called once for every media entity that is loaded

To avoid calling the API a caching layer can be added to store assets loaded by public ID. The Cloudinary::getMetadataAttributes() method can use static caching so the meta data attributes are only fetched once each request.

Issue fork cloudinary-3479652

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

klaasvw created an issue. See original summary.

klaasvw’s picture

Status: Active » Needs review
StatusFileSize
new4.97 KB

The attached patch is a start for fixing the above issues. Feedback and improvements are definitely welcome!

klaasvw’s picture

StatusFileSize
new17.54 KB

The following patch applies the asset caching like the previous patch but also caches Admin API calls in the cache bin. Becasue Cloudinary has a 500 to 2000 requests / hour limit on the admin API and this module calls the API multiple times for every asset it loads the calls add up quickly.

klaasvw changed the visibility of the branch 3.0.x to hidden.

klaasvw changed the visibility of the branch 3479652-avoid-overloading-admin to hidden.

klaasvw changed the visibility of the branch 3479652-api-cache to hidden.

klaasvw changed the visibility of the branch 3479652-api-cache to active.

klaasvw’s picture

StatusFileSize
new21.82 KB

Updated the MR and added a patch that also caches admin API calls from the cloudinary_stream_wrapper module. Also implements a fix from #3354894: Cloudinary Stream Wrapper caches items in the database (Database Storage) but never actually checks if an item is inside the storage so the db storage is not bypassed.

klaasvw’s picture