I have a wind captor connected to a dedicated database.

Datas from the captor are pushed to the db every minutes.

I have no problem to connect to the second db and print the data to a wp page using

    $meteodb = new wpdb('xx','xx','xx','xx');
$meteoValues = $meteodb->get_results( 
    SELECT average, min, max, direction, timestamp 
    FROM wind
    ORDER BY timestamp 

I am totaly new to transient concept. I try to be efficient wise using db requests to the minimum

Instead of connecting directly to the second database everytime a user view the page, I thought it might be a good idea to create a transient says every 5 min pulling new set of datas from second database directly to wp database using a cron. I would keep only the datas from the current day (from 6am to 8pm) and have them auto wiped after 24 hrs

Do you think it is a wise idea?

would it be smarter to have 1° a new transient recorded every 5 min with only the new data and then call all the transient that are available (less than 24 hrs)

2° a single transient refreshed every 5 min with an array containing all the datas from the last 24 hrs?

with a validity of 5 min only (or even 2 minutes), does that sounds smart to use transient?

Thank you in advance


1 Answer 1


Transients are a simple way of storing cached data in the database temporarily by giving it a custom name and a timeframe after which it will expire and be deleted. So yes, it will be a good idea to get data from second DB and store it as a transient.

First thing you have to remember is that:

Everyone seems to misunderstand how transient expiration works, so the long and short of it is: transient expiration times are a maximum time. There is no minimum age. Transients might disappear one second after you set them, or 24 hours, but they will never be around after the expiration time.

And of course it will be pretty hard to select all transients from last 24 hours by your name - names of transients are suffixed with random values (or maybe hashes - it’s not important in this case).

Given these two facts, I’m afraid both your methods won’t work.

There is no point in setting transients with cron - they may disappear right after that. And you can’t store values from last 24 hours and select all of them from DB.

So how would I use transients in this case?

Let’s say there is a function that grabs all data from second DB as an array. Let’s call this function get_remote_db_data(). My code would look something like this:

$data = get_transient('my_transient_name');
if ( ! $data ) {
    $data = get_remote_db_data();
    set_transient('my_transient_name', $data, 5*60);
// ... do your stuff with $data

And how long the expiration time should be? It depends on how frequently should the data be refreshed.

  • thank you very much Krzysiek Dróżdż for your fast answer.. pretty straight forward to your question 'how long should be the expiration date' it is 5 minutes but I guess you solved it with the code you wrote so my understanding from you code is that when the page is called it look if the transient exist.. if not it create one.. since every 5 min the transient will be expired and then wiped.. if the call is made between the 5 minutes then the current transient is used, if the call is after the 5 min, then the transient would have disapeared so a new one is created.. do am I right? thank you Apr 23, 2018 at 5:59
  • @SteffyKristiensen yes, you're right. Although there is no guarantee that it will be exactly 5 minutes. It can disappear earlier, but still... it will lower the number of queries to remote DB significantly, I guess. Apr 23, 2018 at 6:01
  • thank you for you answer.. how comes the expiration time is not guarantee to 5 minutes if I set 5*60 ? thanx Apr 23, 2018 at 6:44
  • It’s not meant to be guaranteed. Take a look at quote in my answer. The expiration of transient is its max life duration. It’s guaranteed that transient won’t exist after this time. Apr 23, 2018 at 6:46
  • it works like a charm, you are a king !! I have learnt a new thing thank to you buddy. I have hard time to understand why the life duration can't be guaranteed only that it is a max time life guarantee.. I am not denying the fact, I just would like to understand why it is such a mecanism? Apr 24, 2018 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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