-
Notifications
You must be signed in to change notification settings - Fork 3.3k
RTC: Use transients for awareness state (KISS approach). #11119
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
6ce6546
b097d00
235d84b
d904252
088548b
81bd3eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,12 +25,12 @@ class WP_Sync_Post_Meta_Storage implements WP_Sync_Storage { | |
| const POST_TYPE = 'wp_sync_storage'; | ||
|
|
||
| /** | ||
| * Meta key for awareness state. | ||
| * Transient prefix for awareness state. | ||
| * | ||
| * @since 7.0.0 | ||
| * @var string | ||
| */ | ||
| const AWARENESS_META_KEY = 'wp_sync_awareness'; | ||
| const AWARENESS_TRANSIENT_PREFIX = 'wp_sync_awareness'; | ||
|
|
||
| /** | ||
| * Meta key for sync updates. | ||
|
|
@@ -123,6 +123,24 @@ static function ( $update ): bool { | |
| return $updates; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the transient key for awareness state for a given post ID. | ||
| * | ||
| * @since 7.0.0 | ||
| * | ||
| * @param int $post_id Post ID. | ||
| * @return string Transient key for awareness state. | ||
| */ | ||
| public function get_awareness_transient_key( $post_id ) { | ||
| $cache_key = self::AWARENESS_TRANSIENT_PREFIX . '_' . $post_id; | ||
| if ( strlen( $cache_key ) <= 172 ) { | ||
| // Safe length for a transient key. | ||
| return $cache_key; | ||
| } | ||
| // If the cache key is too long, hash it to ensure it fits within limits. | ||
| return md5( $cache_key ); | ||
| } | ||
|
|
||
| /** | ||
| * Gets awareness state for a given room. | ||
| * | ||
|
|
@@ -137,7 +155,7 @@ public function get_awareness_state( string $room ): array { | |
| return array(); | ||
| } | ||
|
|
||
| $awareness = get_post_meta( $post_id, self::AWARENESS_META_KEY, true ); | ||
| $awareness = get_transient( $this->get_awareness_transient_key( $post_id ) ); | ||
|
|
||
| if ( ! is_array( $awareness ) ) { | ||
| return array(); | ||
|
|
@@ -161,8 +179,8 @@ public function set_awareness_state( string $room, array $awareness ): bool { | |
| return false; | ||
| } | ||
|
|
||
| // update_post_meta returns false if the value is the same as the existing value. | ||
| update_post_meta( $post_id, self::AWARENESS_META_KEY, $awareness ); | ||
| // set_transient() can return false if the value is the same as the existing value, which is considered a success regardless. | ||
| set_transient( $this->get_awareness_transient_key( $post_id ), $awareness, HOUR_IN_SECONDS ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What this doesn't still address is the issue of the race condition, yeah? Consider the case where two clients attempt to update the same post meta at the same time. As I understand, this is where #11068 would still be advantageous.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought I'd seen them coming from different post IDs but catching up so maybe it needs a room hash. |
||
| return true; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.