Skip to content

Commit

Permalink
Switched over to transients and managing transient on update to preve…
Browse files Browse the repository at this point in the history
…nt extra DB calls. Removed caching as transients will take over.
  • Loading branch information
mauteri committed Jan 28, 2024
1 parent 21183a3 commit 1efa632
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
21 changes: 5 additions & 16 deletions includes/core/classes/class-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class Event {
*/
const TAXONOMY = 'gp_topic';


/**
* Event post object.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 0 additions & 5 deletions test/unit/php/includes/core/classes/class-test-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

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

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

Expand All @@ -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' );

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

Expand Down

0 comments on commit 1efa632

Please sign in to comment.