1

My WordPress plugin depends on a large dataset (around a megabyte). The dataset is downloaded through REST API and then decoded to a PHP array. Right now the dataset is downloaded on each page load and then used for calculations etc. That gives page load times around 1 minute.

However, it is not necessary to have the data downloaded on each page load - it does not update that often. My idea is to store the data and just receive a fresh copy of the data once a day or week. I have used transients in similar cases but transients has a 172 characters limit.

It is not a problem that the first page load each day (or week) takes longer time in order to receive the fresh copy of the data. I just do not know the WordPress-way to store the large dataset when transients is not an option.

My question is: What is the WordPress-way of storing the data temporary?

1 Answer 1

2

Transient names are limited to 172 characters; the data held in a transient can be much, much larger.

set_transient() documentation

Under the hood, transients are stored in the same table as options, and some WordPress code manages their lifetimes and expiry. You should easily be able to store 1MB of data in a transient.

Autoloading

In response to a comment asking about autoloading:

NB: transients that never expire are autoloaded, whereas transients with an expiration time are not autoloaded. Consider this when adding transients that may not be needed on every page, and thus do not need to be autoloaded, impacting page performance.

So as long as you set an expiry date, your transients will not autoload.

// Sets a transient with a week-long lifetime.
set_transient( 'my_transient_name', $data, WEEK_IN_SECONDS );
2
  • Wauw.. Poor me reading way too fast. Is the transients autoloaded on each page load? I do not need the data on all pages - only when I call for them.
    – pstidsen
    Dec 9, 2022 at 15:05
  • 1
    I've updated the answer. (The answer is "not if it's got an expiry date.")
    – Pat J
    Dec 9, 2022 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.