From 3c15679089212070a4beccbb5a5705809b470fd1 Mon Sep 17 00:00:00 2001 From: Krzysztof Grabania Date: Fri, 12 Jul 2024 08:54:45 +0200 Subject: [PATCH 1/3] fix: fix object cache bug --- src/Core/Upgrade.php | 6 +- src/Database/NotificationDatabaseService.php | 144 ++++++++++++------- src/Integration/WordPressIntegration.php | 31 +--- 3 files changed, 97 insertions(+), 84 deletions(-) diff --git a/src/Core/Upgrade.php b/src/Core/Upgrade.php index 01108ad6..77a03aa8 100644 --- a/src/Core/Upgrade.php +++ b/src/Core/Upgrade.php @@ -12,7 +12,7 @@ use BracketSpace\Notification\Database\DatabaseService; use BracketSpace\Notification\Database\NotificationDatabaseService; -use BracketSpace\Notification\Integration\WordPressIntegration; +use BracketSpace\Notification\Dependencies\Micropackage\Cache\Driver as CacheDriver; use BracketSpace\Notification\Interfaces; use BracketSpace\Notification\Store; use BracketSpace\Notification\Utils\WpObjectHelper; @@ -383,6 +383,8 @@ public function upgradeToV3() // 2. Clears notifications cache. - WordPressIntegration::clearNotificationsCache(); + $cache = new CacheDriver\ObjectCache('notification'); + $cache->set_key('notifications'); + $cache->delete(); } } diff --git a/src/Database/NotificationDatabaseService.php b/src/Database/NotificationDatabaseService.php index e1a22731..d164a55d 100644 --- a/src/Database/NotificationDatabaseService.php +++ b/src/Database/NotificationDatabaseService.php @@ -13,6 +13,7 @@ namespace BracketSpace\Notification\Database; use BracketSpace\Notification\Core\Notification; +use BracketSpace\Notification\Dependencies\Micropackage\Cache\Driver as CacheDriver; use BracketSpace\Notification\Store\Notification as NotificationStore; /** @@ -233,6 +234,8 @@ public static function upsert(Notification $notification) do_action_deprecated('notification/data/save/after', [$notification], '[Next]', 'notification/data/saved'); do_action('notification/data/saved', $notification); + static::getCache($notification->getHash())->delete(); + self::$doingOperation = false; } @@ -261,66 +264,76 @@ public static function has($hash) */ public static function get($hash) { - $notificationData = DatabaseService::db()->get_row( - DatabaseService::db()->prepare( - 'SELECT * FROM %i WHERE hash = %s', - self::getNotificationsTableName(), - $hash - ), - 'ARRAY_A' - ); + $cache = static::getCache($hash); + + $notification = $cache->get(); + + if (!$notification instanceof Notification) { + $notificationData = DatabaseService::db()->get_row( + DatabaseService::db()->prepare( + 'SELECT * FROM %i WHERE hash = %s', + self::getNotificationsTableName(), + $hash + ), + 'ARRAY_A' + ); - if ($notificationData === null) { - return null; - } + if ($notificationData === null) { + return null; + } - $notificationData['trigger'] = $notificationData['trigger_slug']; + $notificationData['trigger'] = $notificationData['trigger_slug']; - // Set version based on creation or last update date. - $versionDate = $notificationData['updated_at'] ?? $notificationData['created_at'] ?? 'now'; - $notificationData['version'] = strtotime($versionDate); + // Set version based on creation or last update date. + $versionDate = $notificationData['updated_at'] ?? $notificationData['created_at'] ?? 'now'; + $notificationData['version'] = strtotime($versionDate); - $carriersDataRaw = DatabaseService::db()->get_results( - DatabaseService::db()->prepare( - 'SELECT * FROM %i WHERE notification_hash = %s', - self::getNotificationCarriersTableName(), - $hash - ), - 'ARRAY_A' - ); + $carriersDataRaw = DatabaseService::db()->get_results( + DatabaseService::db()->prepare( + 'SELECT * FROM %i WHERE notification_hash = %s', + self::getNotificationCarriersTableName(), + $hash + ), + 'ARRAY_A' + ); - $notificationData['carriers'] = array_reduce( - (array)$carriersDataRaw, - static function ($carriers, $data) { - if (is_string($data['data'])) { - $carriers[$data['slug']] = json_decode($data['data'], true); - } - return $carriers; - }, - [] - ); + $notificationData['carriers'] = array_reduce( + (array)$carriersDataRaw, + static function ($carriers, $data) { + if (is_string($data['data'])) { + $carriers[$data['slug']] = json_decode($data['data'], true); + } + return $carriers; + }, + [] + ); - $extrasDataRaw = DatabaseService::db()->get_results( - DatabaseService::db()->prepare( - 'SELECT * FROM %i WHERE notification_hash = %s', - self::getNotificationExtrasTableName(), - $hash - ), - 'ARRAY_A' - ); + $extrasDataRaw = DatabaseService::db()->get_results( + DatabaseService::db()->prepare( + 'SELECT * FROM %i WHERE notification_hash = %s', + self::getNotificationExtrasTableName(), + $hash + ), + 'ARRAY_A' + ); - $notificationData['extras'] = array_reduce( - (array)$extrasDataRaw, - static function ($extras, $data) { - if (is_string($data['data'])) { - $extras[$data['slug']] = json_decode($data['data'], true); - } - return $extras; - }, - [] - ); + $notificationData['extras'] = array_reduce( + (array)$extrasDataRaw, + static function ($extras, $data) { + if (is_string($data['data'])) { + $extras[$data['slug']] = json_decode($data['data'], true); + } + return $extras; + }, + [] + ); + + $notification = Notification::from('array', $notificationData); + + $cache->set($notification); + } - return Notification::from('array', $notificationData); + return $notification; } /** @@ -373,6 +386,8 @@ public static function delete($hash) wp_delete_post($post->ID, true); } + static::getCache($hash)->delete(); + self::$doingOperation = false; } @@ -407,4 +422,29 @@ public static function deleteExtras($hash) ] ); } + + /** + * Gets the cache instance for single notification. + * + * @param string $hash Notification hash. + * @return CacheDriver\ObjectCache + */ + protected static function getCache($hash) + { + $cache = new CacheDriver\ObjectCache('notification'); + $cache->set_key(static::getCacheKey($hash)); + + return $cache; + } + + /** + * Gets the cache key for single notification. + * + * @param string $hash Notification hash. + * @return string + */ + protected static function getCacheKey($hash) + { + return sprintf('notification-%s', $hash); + } } diff --git a/src/Integration/WordPressIntegration.php b/src/Integration/WordPressIntegration.php index 068a1d34..3e2f23ba 100644 --- a/src/Integration/WordPressIntegration.php +++ b/src/Integration/WordPressIntegration.php @@ -11,8 +11,6 @@ namespace BracketSpace\Notification\Integration; use BracketSpace\Notification\Database\NotificationDatabaseService; -use BracketSpace\Notification\Dependencies\Micropackage\Cache\Cache; -use BracketSpace\Notification\Dependencies\Micropackage\Cache\Driver as CacheDriver; use BracketSpace\Notification\Interfaces\Triggerable; use BracketSpace\Notification\Register; @@ -21,12 +19,6 @@ */ class WordPressIntegration { - /** - * Notifications cache key - * - * @var string - */ - protected static $notificationsCacheKey = 'notifications'; /** * -------------------------- @@ -44,32 +36,11 @@ class WordPressIntegration */ public function loadDatabaseNotifications() { - $driver = new CacheDriver\ObjectCache('notification'); - $cache = new Cache($driver, static::$notificationsCacheKey); - - /** - * @var array<\BracketSpace\Notification\Core\Notification> - */ - $notifications = $cache->collect(static fn() => NotificationDatabaseService::getAll()); + $notifications = NotificationDatabaseService::getAll(); array_map([Register::class, 'notificationIfNewer'], $notifications); } - /** - * Clears the Notifications cache - * - * @action notification/data/saved - * - * @since [Next] - * @return void - */ - public static function clearNotificationsCache() - { - $cache = new CacheDriver\ObjectCache('notification'); - $cache->set_key(static::$notificationsCacheKey); - $cache->delete(); - } - /** * -------------------------- * Duplicate prevention From ba5ea411a7a62ecb54d775675a2861b060b3dd7a Mon Sep 17 00:00:00 2001 From: Krzysztof Grabania Date: Fri, 12 Jul 2024 08:57:31 +0200 Subject: [PATCH 2/3] chore: fix phpcs error --- src/Integration/WordPressIntegration.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Integration/WordPressIntegration.php b/src/Integration/WordPressIntegration.php index 3e2f23ba..e1de86af 100644 --- a/src/Integration/WordPressIntegration.php +++ b/src/Integration/WordPressIntegration.php @@ -19,7 +19,6 @@ */ class WordPressIntegration { - /** * -------------------------- * Loaders & Cache From 7ba8a717df12698e55c7a0297e866711059347c7 Mon Sep 17 00:00:00 2001 From: Krzysztof Grabania Date: Fri, 12 Jul 2024 11:12:47 +0200 Subject: [PATCH 3/3] fix: cache notification data rather than notification object --- src/Database/NotificationDatabaseService.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Database/NotificationDatabaseService.php b/src/Database/NotificationDatabaseService.php index d164a55d..aa95e6b0 100644 --- a/src/Database/NotificationDatabaseService.php +++ b/src/Database/NotificationDatabaseService.php @@ -266,9 +266,9 @@ public static function get($hash) { $cache = static::getCache($hash); - $notification = $cache->get(); + $notificationData = $cache->get(); - if (!$notification instanceof Notification) { + if (!is_array($notificationData)) { $notificationData = DatabaseService::db()->get_row( DatabaseService::db()->prepare( 'SELECT * FROM %i WHERE hash = %s', @@ -328,12 +328,10 @@ static function ($extras, $data) { [] ); - $notification = Notification::from('array', $notificationData); - - $cache->set($notification); + $cache->set($notificationData); } - return $notification; + return Notification::from('array', $notificationData); } /**