Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directory cache is not invalidated after saving page #3152

Closed
Flamenco opened this issue Dec 3, 2020 · 6 comments
Closed

Directory cache is not invalidated after saving page #3152

Flamenco opened this issue Dec 3, 2020 · 6 comments
Assignees

Comments

@Flamenco
Copy link

Flamenco commented Dec 3, 2020

This code does not work.

$page->save();
Cache::clearCache('invalidate');
$grav->redirect('/admin/pages' . $newPage->rawRoute());

The redirect page shows the following message, although the page is saved and served:

"This page will not exist until it is saved."

After several seconds and a page refresh, the error goes away. It also goes away by clearing the cache in the admin.

This code works after calling $directory->clearCache();

$page->save();
Cache::clearCache('invalidate');
$flex = $grav['flex'];
$directory = $flex->getDirectory('pages');
$directory->clearCache();
$grav->redirect('/admin/pages' . $newPage->rawRoute());

I would like to simply call the following and be isolated from the implementation.

$page->save([invalidate=>true]);
@Flamenco
Copy link
Author

Flamenco commented Dec 3, 2020

After downloading Grav v1.7.0-rc.18 - Admin v1.10.0-rc.18, I tried the following code:

$page->save();
$grav->redirect('/admin/pages' . $newPage->rawRoute());

Same error.

Screen Shot 2020-12-03 at 5 44 08 PM

After waiting 10 seconds, and refreshing, the error goes away and the edit page shows up.

@mahagr mahagr self-assigned this Dec 4, 2020
@Flamenco
Copy link
Author

Flamenco commented Dec 5, 2020

Here are a couple of utility functions I added to core-service-manager as our workaround.

	/**
	 * Call this function, after saving a page, if needing to redirect back to the page's admin.
	 */
	public function updateAdminCache(): void
	{
		if ($this->gravVersionMatches('1.7.0-rc.0', '>=')) {
			Cache::clearCache('invalidate');
			$flex = $this->grav['flex'];
			$directory = $flex->getDirectory('pages');
			$directory->clearCache();
		}
	}

	/**
	 * A workaround that saves a page and then updates relevant Admin plugin caches.
	 * @param Page $page
	 */
	public function save(Page $page)
	{
		$page->save();
		$this->updateAdminCache();
	}

Invoke it like this:

$this->grav['core-service-util']->save($myPage);

I am currently adding methods to CSM to help write code blocks to run under different versions of Grav, GravAdmin, and PHP, so this will be updated to call the proper code depending on the installation.

@mahagr
Copy link
Member

mahagr commented Dec 14, 2020

BTW, you cannot have Page as the type hint (in save), it will not work with flex pages, just with the old class.

You should be using PageInterface instead. Is your class using the old Page class?

@Flamenco
Copy link
Author

Yes this is legacy code and I am not currently using PageInterface.

@mahagr
Copy link
Member

mahagr commented Jan 15, 2021

I took a quick look; saving the old Page object doesn't affect admin because of it's using the new Flex Page.

I think we need to add some code to signal the another party that there has been a change.

@mahagr mahagr transferred this issue from getgrav/grav-plugin-admin Jan 15, 2021
@mahagr
Copy link
Member

mahagr commented Jan 15, 2021

@Flamenco Should be fixed now, the old code should work.

@mahagr mahagr closed this as completed Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants