Jump to content

Does PW keep values retrieved from the database in memory?


Robin S
 Share

Recommended Posts

A general coding question I've wondered about for a while but never got around to asking until now.

Suppose I'm writing some code in a template file, and I get some value from the database...

echo $page->parent->title;

Now later on in my template I want to use this value again, so my template file is now...

echo $page->parent->title;

// More code here...

echo $page->parent->title;

Is PW going to go back to the database to get this value? Or is it automatically kept in memory so that only the first usage results in a trip to the database?

In most circumstances where I do this I would tend to put the value in a variable and then reuse the variable...

$parent_title = $page->parent->title;
echo $parent_title;

// More code here...

echo $parent_title;

...but if I didn't and instead did it like the previous example, would there be any performance penalty?

Is there any documentation or forum post that talks about how PW handles this sort of thing?

  • Like 1
Link to comment
Share on other sites

@Robin S I fas as know they are cached somehow, but I don't remember where I'he read it. Only first usage results in DB query. Actually, you can check this behavior by looking at PDO queries count in PW debug panel. Also would like to get more efficient answers. 

  • Like 1
Link to comment
Share on other sites

14 hours ago, Robin S said:

Is there any documentation or forum post that talks about how PW handles this sort of thing?

Not sure about anything beside what @kongondo linked, but PW does definitely cache things, both field values and pages. Pages are kept in memory as any calls to Pages::get and Pages::find are routed through PagesLoaderCache. Equally, field values are retrieved only once, as Page::getFieldValue (which is in turn invoked by other get... methods like getUnformatted) only calls Fieldtype::loadPageField if the raw field value isn't yet populated (and $page->fieldname and $page->get('fieldname') both delegate their work to getFieldValue).

  • Like 6
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...