Created on 26 July 2023, over 1 year ago
Updated 25 September 2023, about 1 year ago

Problem/Motivation

Provide the option for a cache.
Using a cache, data should not need to be fetched more than once from Drupal. Data should be stored in a local cache (or store). Data fetched from Drupal should be cached and subsequent calls for that data should serve from the cache.

Steps to reproduce

Proposed resolution

This client won't cache things by default, but it should accept a caching mechanism as an option that will be used to cache requests.

Consider locale - might need one cache per locale

I (Brian) really like the Next for Drupal client's API for this, so what I'm proposing here would be very similar to that as a starting point.

The options object will accept a cache, which is an object with:
* a get method that takes a key as a parameter.
* a set method that takes a key and a value as a parameter.

getCollection will also accept an optional cache key, which will be used when getting a collection with cache enabled. With a cache provided, getCollection will first check the cache (based on the provided cache key) and return data from the cache if it exists. If it does have to fetch from the API, it will store the result in the cache for future re-use.

> Consider locale - might need one cache per locale

This doesn't specifically address locale, but it is also an API that is flexible enough to support multiple locales.

To implement this for the POC, we should also choose a cache library to use as an example (an example, not a direct dependency of the client). I'm familiar with Zustand due to using it with Drupal State and think it would be compatible with the proposed API. But open to other viable alternatives - we're trying to prove that this could work generically.

Remaining tasks

* Implement the necessary changes
** Add cache option to apiClient class
** Add cacheKey option to getCollection on the jsonApiClient.
** If cache is provided, check cache before making a fetch request.
** If cache is provided, store any fetched data in cache.
* Define necessary types
* Document the current ApiClient class inline.
* Add test coverage.

User interface changes

API changes

* The options object will now include a cache property.
* getCollection will now accept a cacheKey property.

Data model changes

Feature request
Status

Fixed

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024