Skip to content

Commit

Permalink
Fix memory usage on t-shirt cache prime (#1085)
Browse files Browse the repository at this point in the history
* Fix memory usage on t-shirt cache prime

Warning are being thrown during the current execution of the t-shirt cron:
```
E_USER_WARNING
Peak memory usage at 92.2%. Current action/filter: switch_blog.
Stack Trace
do_action_ref_array('camptix_prime_tshirt_report')
```

In testing this reduced the memory issues.

* Update field-tshirt.php

Fix linting.
  • Loading branch information
pkevan authored Oct 11, 2023
1 parent f131eac commit 888ecae
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions public_html/wp-content/plugins/camptix/addons/field-tshirt.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ public function prime_report_cache() {
'order' => 'DESC',
) );

$i = 0;
foreach ( $sites as $site_id ) {
switch_to_blog( $site_id );

$i++;
$sizes = $this->get_aggregated_sizes();

if ( ! empty( $sizes ) ) {
Expand All @@ -86,8 +87,11 @@ public function prime_report_cache() {
}

restore_current_blog();
// Reset DB query log each time, to reduce memory usage.
self::reset_db_query_log();
// Reset DB query log & object cache every 10 loops, to reduce memory usage.
if ( 0 == $i % 10 ) {
self::reset_db_query_log();
self::reset_local_object_cache();
}
}

update_site_option( 'tix_aggregated_tshirt_sizes', $sizes_by_site );
Expand All @@ -103,6 +107,26 @@ public function reset_db_query_log() {
$wpdb->queries = array();
}

/**
* Reset local WordPress object cache.
*/
public function reset_local_object_cache() {
global $wp_object_cache;

if ( ! is_object( $wp_object_cache ) ) {
return;
}

$wp_object_cache->group_ops = array();
$wp_object_cache->memcache_debug = array();
$wp_object_cache->cache = array();

if ( method_exists( $wp_object_cache, '__remoteset' ) ) {
$wp_object_cache->__remoteset();
}
}


/**
* Get the counts for each shirt size that attendees selected
*
Expand Down

0 comments on commit 888ecae

Please sign in to comment.