Skip to content

Commit

Permalink
changed: invalidating widget cache using entity invalidateCache function
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalsem committed Oct 2, 2024
1 parent e0eea24 commit 4f0d847
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
27 changes: 2 additions & 25 deletions classes/ColdTrick/WidgetManager/Widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static function getContentFromCache(\Elgg\Event $event): ?array {
}

$result = $event->getValue();
$result[\Elgg\ViewsService::OUTPUT_KEY] = $cached_data;
$result[\Elgg\ViewsService::OUTPUT_KEY] = $cached_data . 'from_cache';

return $result;
}
Expand Down Expand Up @@ -192,37 +192,14 @@ public static function saveContentInCache(\Elgg\Event $event): void {
elgg_save_system_cache("widget_cache_{$widget->guid}_{$current_language}", $event->getValue());
}

/**
* Unsets the cached data for cacheable widgets
*
* @param \Elgg\Event $event 'update:after', 'object'
*
* @return void
*/
public static function clearWidgetCacheOnUpdate(\Elgg\Event $event): void {
$widget = $event->getObject();
if (!$widget instanceof \ElggWidget) {
return;
}

if (!self::isCacheableWidget($widget)) {
return;
}

$languages = elgg()->translator->getAllowedLanguages();
foreach ($languages as $language) {
elgg_delete_system_cache("widget_cache_{$widget->guid}_{$language}");
}
}

/**
* Checks if the provide widget is registered as a cacheable widget
*
* @param \ElggWidget $widget widget to check
*
* @return bool
*/
protected static function isCacheableWidget(\ElggWidget $widget): bool {
public static function isCacheableWidget(\ElggWidget $widget): bool {
static $cacheable_handlers;
if (!isset($cacheable_handlers)) {
$cacheable_handlers = elgg_trigger_event_results('cacheable_handlers', 'widget_manager', [], []);
Expand Down
20 changes: 20 additions & 0 deletions classes/WidgetManagerWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,24 @@ class WidgetManagerWidget extends \ElggWidget {
public function getDisplayName(): string {
return $this->widget_manager_custom_title ?: parent::getDisplayName();
}

/**
* {@inheritdoc}
*/
public function invalidateCache(): void {
if (!$this->guid) {
return;
}

parent::invalidateCache();

if (!\ColdTrick\WidgetManager\Widgets::isCacheableWidget($this)) {
return;
}

$languages = elgg()->translator->getAllowedLanguages();
foreach ($languages as $language) {
elgg_delete_system_cache("widget_cache_{$this->guid}_{$language}");
}
}
}
5 changes: 0 additions & 5 deletions elgg-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@
'\ColdTrick\WidgetManager\Groups::updateGroupWidgets' => [],
],
],
'update:after' => [
'object' => [
'\ColdTrick\WidgetManager\Widgets::clearWidgetCacheOnUpdate' => [],
],
],
'view' => [
'object/widget/body' => [
'\ColdTrick\WidgetManager\Widgets::saveContentInCache' => ['priority' => 9999],
Expand Down

0 comments on commit 4f0d847

Please sign in to comment.