Skip to content

Commit

Permalink
Merge pull request #518 from GatherPress/GP-507
Browse files Browse the repository at this point in the history
Add caching of datetimes to improve performance.
  • Loading branch information
mauteri authored Jan 28, 2024
2 parents 178a907 + 1efa632 commit 74d989f
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 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 Down Expand Up @@ -567,14 +566,14 @@ public function get_datetime(): array {
}

$cache_key = sprintf( self::DATETIME_CACHE_KEY, $this->event->ID );
$data = wp_cache_get( $cache_key );
$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 );
set_transient( $cache_key, $data, 15 * MINUTE_IN_SECONDS );
}

return array_merge(
Expand Down Expand Up @@ -913,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

0 comments on commit 74d989f

Please sign in to comment.