Jump to content

effects of uncaching a page


pogidude
 Share

Recommended Posts

In my module, I'm hooking into Pages::saveReady and have the following code:

      // get the old value of the field
      $this->pages->uncache($page);
      $page = $this->pages->get($page->id);
      $this->pageStatusBeforeSave = $page->$statusFieldName;   

basically, I'm trying to get the *database-stored* value of the field and not the *soon-to-be-saved* one which seems to be the case if I don't call uncache().

So, what I'm really asking is, will uncaching the page have other effects that I don't see?

And yes, I've already seen this http://processwire.com/talk/topic/2593-tracking-changes-on-pages-%E2%80%93-getting-original-value/ and this http://processwire.com/talk/topic/1067-accessing-previous-version-of-page-beforeafter-page-save/#entry9321 and have already studied Netcarver's field change notifier module very nice by the way :)

Link to comment
Share on other sites

One gotcha if you uncache a page as I've done it is that *some* of the fields don't save. Which is kind of weird behaviour to me because I'd at least expect either ALL or NONE of the fields to be saved if I call uncache() on the current page.

Specifically, all the text type fields get saved i.e. textarea, text (as in title, body) but the field types that don't get saved are FieldtypeTemplates and FieldtypePage fields. what gives?

anyhow, I ended up doing this to get the value stored in the database:

$clone = $page;
$clone->uncache();
$this->pageStatusBeforeSave = $clone->myfield;
Link to comment
Share on other sites

Not sure if you got it working or not, but I did a quick test and you you're right it doesn't save formated fields like page fields.

I got it working by creating a clone of the saved page and then uncache and get the page from DB to get old value. Now it also saves the page fields.

public function saveReady($event) {
    $p = $event->arguments("page");
    $p = clone($p);
    echo "new headline: " . $p->headline; // debug

    $this->pages->uncache($p);
    $old = $this->pages->get($p->id); // get page from db
    echo " | old headline: " . $old->headline; // debug
    // exit(); // debug
}
 
  • Like 1
Link to comment
Share on other sites

 
Not sure if you got it working or not, but I did a quick test and you you're right it doesn't save formated fields like page fields.

but still it would be nice to know why formatted fields aren't getting saved while other fields are saved. Not really a biggie but it sort of stands out for me in ProcessWire which has a really really clean and consistent interface (that I wish other frameworks would emulate) that stuff like this tend to stick out

Link to comment
Share on other sites

When you uncache a page, it also triggers an uncache() method on all the page's fields. Fieldtypes can decide how to handle that. If those fields are objects, then they may choose to uncache themselves as well. Depending on the fieldtype, this could mean that they essentially reload from DB the next time you access them. Text fields on the other hand are just text, not references to other object instances–so there aren't any external references to uncache.  

When you clone a page, it creates a second copy of everything (including the fields it references). By uncaching the clone instead of the original, you are effectively uncaching it from the ID cache of $pages only. A shallow uncache rather than a deep one. All the fields on your clone are still being uncached, but your original $page is still in tact and an uncache hasn't uncached your changes. Basically, I think what you are doing makes sense and is probably the right way to go here. Though it might make sense for me to add a $shalllow option to the $pages->uncache method, so that you could bypass a deep uncache if needed. 

Link to comment
Share on other sites

When you uncache a page, it also triggers an uncache() method on all the page's fields.

hmmm.. not *all* fields are uncached as the uncache() method is only triggered on fields that reference objects. and not sure but the uncaching seems to be done by the field value and not the field itself.. I dunno.. and since text fields are just text and can't call uncache... they don't get reloaded from the db? which is why after uncaching a page, changes to text fields still get saved while object fields don't?

also in Page::uncache() I'm wondering why the value is set to null:

if($value != null && is_object($value)) {
	if(method_exists($value, 'uncache')) $value->uncache();
	parent::set($field->name, null); 
}

SIDENOTE:

I'm very interested in the inner workings of PW as I'm trying to create a small version of it as a framework for wordpress plugins.

Link to comment
Share on other sites

  • 1 year later...

Though it might make sense for me to add a $shalllow option to the $pages->uncache method, so that you could bypass a deep uncache if needed. 

Hi Ryan, is there any update on that? Is it going to be implemented?

Link to comment
Share on other sites

  • 6 years later...

I'm curious what the rationale is for the default behavior of a page save uncaching all pages in the system, as this sometimes causes problems when editing and saving more than one page during the same request. I'm finding myself having to add ['uncacheAll' => false] to most of my page saves. But if there is some important reason for clearing the cache, I want to be aware of it!

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...