Skip to content

Commit

Permalink
Add a cache to mitigate the problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
gogdzl committed Dec 20, 2024
1 parent 80a5b84 commit 2207106
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions projects/plugins/crm/includes/ZeroBSCRM.DAL3.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ class zbsDAL {
*/
private $cache = array();

/**
* This is a temporary fix for issue #3504, until we don't change how we load settings this should help to avoid slowing down websites.
*
* @var array
*/
private static $mitigation_cache_for_issue_3504 = array();

// ===============================================================================
// =========== SUB DAL LAYERS ==================================================
// These hold sub-objects, e.g. contact
Expand Down Expand Up @@ -1633,6 +1640,9 @@ public function setting( $key = '', $default = false, $accept_cached = false){

if ( !empty( $key ) ){

if ( isset( self::$mitigation_cache_for_issue_3504[ $key ] ) ) {
return self::$mitigation_cache_for_issue_3504[ $key ];
}
return $this->getSetting(array(

'key' => $key,
Expand Down Expand Up @@ -1708,6 +1718,10 @@ public function getSetting($args=array()){
); foreach ($defaultArgs as $argK => $argV){ $$argK = $argV; if (is_array($args) && isset($args[$argK])) { if (is_array($args[$argK])){ $newData = $$argK; if (!is_array($newData)) $newData = array(); foreach ($args[$argK] as $subK => $subV){ $newData[$subK] = $subV; }$$argK = $newData;} else { $$argK = $args[$argK]; } } }
#} =========== / LOAD ARGS =============

if ( ! $fullDetails && isset( self::$mitigation_cache_for_issue_3504[ $key ] ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase, VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
return self::$mitigation_cache_for_issue_3504[ $key ]; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
}

#} Check key
if (!empty($key)){

Expand Down Expand Up @@ -1818,6 +1832,11 @@ public function getSetting($args=array()){
* @return array of settings lines
*/
public function getSettings($args=array()){
// If we have already loaded something in our cache, return it.
// In this function we won't care if the values have changed or not because when they become invalid due to other functions, these functions should clear the cache.
if ( ! empty( self::$mitigation_cache_for_issue_3504 ) ) {
return self::$mitigation_cache_for_issue_3504;
}

#} ============ LOAD ARGS =============
$defaultArgs = array(
Expand Down Expand Up @@ -1889,20 +1908,19 @@ public function getSettings($args=array()){

#} Has results, tidy + return
foreach ($potentialRes as $resDataLine) {
// We are only caching the singular setting (i.e. the value), because it is good enough.
self::$mitigation_cache_for_issue_3504[ $resDataLine->zbsset_key ] = $this->tidy_settingSingular( $resDataLine ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase

// DEBUG echo $resDataLine->zbsset_key.' = ';

if ($fullDetails){
// tidy
$resArr = $this->tidy_setting($resDataLine);
$res[$resArr['key']] = $resArr;
} else
$res[$resDataLine->zbsset_key] = $this->tidy_settingSingular($resDataLine);

if ( $fullDetails ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase, VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
$resArr = $this->tidy_setting( $resDataLine ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
$res[ $resArr['key'] ] = $resArr; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
} else {
$res[ $resDataLine->zbsset_key ] = $this->tidy_settingSingular( $resDataLine ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
}
}
}

return $res;
return $res;
}

/**
Expand Down Expand Up @@ -2005,6 +2023,9 @@ public function addUpdateSetting($args=array()){
); foreach ($defaultArgs as $argK => $argV){ $$argK = $argV; if (is_array($args) && isset($args[$argK])) { if (is_array($args[$argK])){ $newData = $$argK; if (!is_array($newData)) $newData = array(); foreach ($args[$argK] as $subK => $subV){ $newData[$subK] = $subV; }$$argK = $newData;} else { $$argK = $args[$argK]; } } }
#} =========== / LOAD ARGS ============

// Invalidates our whole cache.
self::$mitigation_cache_for_issue_3504 = array();

#} ========== CHECK FIELDS ============

$id = (int)$id;
Expand Down

0 comments on commit 2207106

Please sign in to comment.