Skip to content

Commit

Permalink
fix: fix object cache bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Dartui committed Jul 12, 2024
1 parent 061defc commit 3c15679
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 84 deletions.
6 changes: 4 additions & 2 deletions src/Core/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -383,6 +383,8 @@ public function upgradeToV3()

// 2. Clears notifications cache.

WordPressIntegration::clearNotificationsCache();
$cache = new CacheDriver\ObjectCache('notification');
$cache->set_key('notifications');
$cache->delete();
}
}
144 changes: 92 additions & 52 deletions src/Database/NotificationDatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -373,6 +386,8 @@ public static function delete($hash)
wp_delete_post($post->ID, true);
}

static::getCache($hash)->delete();

self::$doingOperation = false;
}

Expand Down Expand Up @@ -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);
}
}
31 changes: 1 addition & 30 deletions src/Integration/WordPressIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,12 +19,6 @@
*/
class WordPressIntegration
{
/**
* Notifications cache key
*
* @var string
*/
protected static $notificationsCacheKey = 'notifications';

/**
* --------------------------
Expand All @@ -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
Expand Down

0 comments on commit 3c15679

Please sign in to comment.