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

fix: fix object cache bug #476

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
140 changes: 89 additions & 51 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,64 +264,72 @@ 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);

$notificationData = $cache->get();

if (!is_array($notificationData)) {
$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;
},
[]
);

$cache->set($notificationData);
}

return Notification::from('array', $notificationData);
}
Expand Down Expand Up @@ -373,6 +384,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 +420,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);
}
}
32 changes: 1 addition & 31 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,13 +19,6 @@
*/
class WordPressIntegration
{
/**
* Notifications cache key
*
* @var string
*/
protected static $notificationsCacheKey = 'notifications';

/**
* --------------------------
* Loaders & Cache
Expand All @@ -44,32 +35,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
Loading