From 1efa632c658a1e20293492baa5bb02a0b7d174c0 Mon Sep 17 00:00:00 2001 From: Mike Auteri Date: Sun, 28 Jan 2024 09:10:30 -0500 Subject: [PATCH] Switched over to transients and managing transient on update to prevent extra DB calls. Removed caching as transients will take over. --- includes/core/classes/class-event.php | 21 +++++-------------- .../core/classes/class-test-event.php | 5 ----- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/includes/core/classes/class-event.php b/includes/core/classes/class-event.php index d006dfa9d..7639a88be 100644 --- a/includes/core/classes/class-event.php +++ b/includes/core/classes/class-event.php @@ -63,7 +63,6 @@ class Event { */ const TAXONOMY = 'gp_topic'; - /** * Event post object. * @@ -72,14 +71,6 @@ class Event { */ protected $event = null; - /** - * Storing and retrieving event datetimes. - * - * @since 1.0.0 - * @var array - */ - protected array $datetimes = array(); - /** * RSVP instance. * @@ -575,16 +566,14 @@ public function get_datetime(): array { } $cache_key = sprintf( self::DATETIME_CACHE_KEY, $this->event->ID ); - $cache = wp_cache_get( $cache_key ); - $data = ! empty( $cache ) ? $cache : $this->datetimes; + $data = get_transient( $cache_key ); if ( empty( $data ) || ! is_array( $data ) ) { $table = sprintf( static::TABLE_FORMAT, $wpdb->prefix ); - $data = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT datetime_start, datetime_start_gmt, datetime_end, datetime_end_gmt, timezone FROM ' . esc_sql( $table ) . ' WHERE post_id = %d LIMIT 1', $this->event->ID ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery + $data = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT datetime_start, datetime_start_gmt, datetime_end, datetime_end_gmt, timezone FROM ' . esc_sql( $table ) . ' WHERE post_id = %d LIMIT 1', $this->event->ID ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $data = ( ! empty( $data ) ) ? (array) current( $data ) : array(); - wp_cache_set( $cache_key, $data, 15 * MINUTE_IN_SECONDS ); - $this->datetimes = $data; + set_transient( $cache_key, $data, 15 * MINUTE_IN_SECONDS ); } return array_merge( @@ -923,12 +912,12 @@ function ( $key ) { ); if ( ! empty( $exists ) ) { - $retval = $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery + $retval = $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $table, $fields, array( 'post_id' => $fields['post_id'] ) ); - wp_cache_delete( sprintf( self::DATETIME_CACHE_KEY, $fields['post_id'] ) ); + delete_transient( sprintf( self::DATETIME_CACHE_KEY, $fields['post_id'] ) ); } else { $retval = $wpdb->insert( $table, $fields ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery } diff --git a/test/unit/php/includes/core/classes/class-test-event.php b/test/unit/php/includes/core/classes/class-test-event.php index 98478dbf0..6497d04c0 100644 --- a/test/unit/php/includes/core/classes/class-test-event.php +++ b/test/unit/php/includes/core/classes/class-test-event.php @@ -604,7 +604,6 @@ public function test_has_event_started(): void { 'Failed to assert that event has started.' ); - $event = new Event( $post->ID ); $start = new DateTime( 'now' ); $end = new DateTime( 'now' ); @@ -632,7 +631,6 @@ public function test_has_event_started(): void { 'Failed to assert that event has started with offset.' ); - $event = new Event( $post->ID ); $start = new DateTime( 'now' ); $end = new DateTime( 'now' ); @@ -688,7 +686,6 @@ public function test_has_event_past(): void { 'Failed to assert that event has past.' ); - $event = new Event( $post->ID ); $start = new DateTime( 'now' ); $end = new DateTime( 'now' ); @@ -706,7 +703,6 @@ public function test_has_event_past(): void { $this->assertFalse( $output ); - $event = new Event( $post->ID ); $start = new DateTime( 'now' ); $end = new DateTime( 'now' ); @@ -768,7 +764,6 @@ public function test_is_event_happening(): void { 'Failed to assert that event is happening' ); - $event = new Event( $post->ID ); $start = new DateTime( 'now' ); $end = new DateTime( 'now' );