1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-11 09:14:58 +02:00

Upgrade $pages->uncache() method so that it can also accept page IDs in addition to Page objects

This commit is contained in:
Ryan Cramer
2020-03-13 12:43:44 -04:00
parent 550fc26300
commit ffc7e6fb86
2 changed files with 15 additions and 7 deletions

View File

@@ -1004,7 +1004,7 @@ class Pages extends Wire {
*
* #pw-internal
*
* @param Page|PageArray|null $page Page to uncache, or omit to uncache all.
* @param Page|PageArray|int|null $page Page to uncache, PageArray of pages to uncache, ID of page to uncache (3.0.153+), or omit to uncache all.
* @param array $options Additional options to modify behavior:
* - `shallow` (bool): By default, this method also calls $page->uncache(). To prevent that call, set this to true.
* @return int Number of pages uncached
@@ -1014,7 +1014,7 @@ class Pages extends Wire {
$cnt = 0;
if(is_null($page)) {
$cnt = $this->cacher->uncacheAll(null, $options);
} else if($page instanceof Page) {
} else if($page instanceof Page || is_int($page)) {
if($this->cacher->uncache($page, $options)) $cnt++;
} else if($page instanceof PageArray) {
foreach($page as $p) {

View File

@@ -78,16 +78,24 @@ class PagesLoaderCache extends Wire {
*
* Note: does not remove pages from selectorCache. Call uncacheAll to do that.
*
* @param Page $page Page to uncache
* @param Page|int $page Page to uncache or ID of page (prior to 3.0.153 only Page object was accepted)
* @param array $options Additional options to modify behavior:
* - `shallow` (bool): By default, this method also calls $page->uncache(). To prevent call to $page->uncache(), set 'shallow' => true.
* @return bool True if page was uncached, false if it didn't need to be
*
*/
public function uncache(Page $page, array $options = array()) {
if(empty($options['shallow'])) $page->uncache();
if(isset($this->pageIdCache[$page->id])) {
unset($this->pageIdCache[$page->id]);
public function uncache($page, array $options = array()) {
if($page instanceof Page) {
$pageId = $page->id;
} else {
$pageId = is_int($page) ? $page : (int) "$page";
$page = isset($this->pageIdCache[$pageId]) ? $this->pageIdCache[$pageId] : null;
}
if(empty($options['shallow']) && $page) {
$page->uncache();
}
if(isset($this->pageIdCache[$pageId])) {
unset($this->pageIdCache[$pageId]);
return true;
} else {
return false;