Skip to content

Commit 2023bfd

Browse files
committed
Simplify use of in memory cache.
1 parent ec1fa68 commit 2023bfd

1 file changed

Lines changed: 8 additions & 17 deletions

File tree

src/wp-includes/collaboration/class-wp-collaboration-table-storage.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,10 @@ public function get_awareness_state( string $room, int $timeout = 30 ): array {
104104
$cutoff_timestamp = time() - $timeout;
105105
$cutoff_mysql = gmdate( 'Y-m-d H:i:s', $cutoff_timestamp );
106106
$cache_key = 'awareness::' . str_replace( '/', ':', $room );
107+
$cached = wp_cache_get( $cache_key, 'collaboration' );
107108

108-
if ( wp_using_ext_object_cache() ) {
109-
$cached = wp_cache_get( $cache_key, 'collaboration' );
110-
111-
if ( false === $cached || ! is_string( $cached ) ) {
112-
// Room not set/corrupted.
113-
return array();
114-
}
115-
109+
if ( false !== $cached && is_string( $cached ) ) {
110+
// Room is cached.
116111
$cached = json_decode( $cached, true );
117112

118113
// Deterministic ordering.
@@ -128,19 +123,14 @@ public function get_awareness_state( string $room, int $timeout = 30 ): array {
128123
return array_values( $cached_awareness );
129124
}
130125

131-
// Check in-memory cache for sites without a persistent object cache.
132-
$cached = wp_cache_get( $cache_key, 'collaboration' );
133-
if ( false !== $cached && is_string( $cached ) ) {
134-
$cached = json_decode( $cached, true );
135-
136-
if ( is_array( $cached ) ) {
137-
return $cached;
138-
}
126+
if ( wp_using_ext_object_cache() ) {
127+
// Sites with a persistent cache do not use the database.
128+
return array();
139129
}
140130

141131
$rows = $wpdb->get_results(
142132
$wpdb->prepare(
143-
"SELECT client_id, user_id, data FROM {$wpdb->collaboration} WHERE room = %s AND type = 'awareness' AND date_gmt >= %s",
133+
"SELECT client_id, user_id, date_gmt, data FROM {$wpdb->collaboration} WHERE room = %s AND type = 'awareness' AND date_gmt >= %s",
144134
$room,
145135
$cutoff_mysql
146136
)
@@ -160,6 +150,7 @@ public function get_awareness_state( string $room, int $timeout = 30 ): array {
160150
'client_id' => $row->client_id,
161151
'state' => $decoded,
162152
'user_id' => (int) $row->user_id,
153+
'timestamp' => date_create_from_format( 'Y-m-d H:i:s', $row->date_gmt, new DateTimeZone( 'UTC' ) )->getTimestamp(),
163154
);
164155
}
165156
}

0 commit comments

Comments
 (0)