From d4adf8fdae1db7fc86e5886afc86f81035205051 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 12 Dec 2025 13:00:48 -0400 Subject: [PATCH 1/8] Try to cut back on calls to get option --- classes/models/FrmFormApi.php | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/classes/models/FrmFormApi.php b/classes/models/FrmFormApi.php index 3775b25cf0..bbf9b00168 100644 --- a/classes/models/FrmFormApi.php +++ b/classes/models/FrmFormApi.php @@ -27,6 +27,15 @@ class FrmFormApi { */ protected $new_days = 90; + /** + * Memoized API info. + * This is indexed by cache key. + * It allows us to avoid extra calls to get_option(). + * + * @var array|null + */ + private static $memoized_api_info = array(); + /** * @since 3.06 * @@ -302,12 +311,13 @@ protected function get_cached() { } // If the api call is running, we can use the expired cache. - if ( ! $this->is_running() ) { - if ( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) { - // Cache is expired. + $is_expired = empty( $cache['timeout'] ) || time() > $cache['timeout']; + if ( $is_expired ) { + if ( ! $this->is_running() ) { return false; } + // If the api call is running, we can use the expired cache. $version = FrmAppHelper::plugin_version(); $for_current = isset( $cache['version'] ) && $cache['version'] == $version; @@ -322,6 +332,8 @@ protected function get_cached() { return $values; } + static $count = 0; + /** * Get the cache for the network if multisite. * @@ -330,15 +342,23 @@ protected function get_cached() { * @return mixed */ protected function get_cached_option() { + $key = $this->cache_key; + + if ( isset( self::$memoized_api_info[ $key ] ) ) { + return self::$memoized_api_info[ $key ]; + } + if ( is_multisite() ) { $cached = get_site_option( $this->cache_key ); if ( $cached ) { + self::$memoized_api_info[ $key ] = $cached; return $cached; } } - return get_option( $this->cache_key ); + self::$memoized_api_info[ $key ] = get_option( $this->cache_key ); + return self::$memoized_api_info[ $key ]; } /** @@ -355,6 +375,8 @@ protected function set_cached( $addons ) { 'version' => FrmAppHelper::plugin_version(), ); + self::$memoized_api_info[ $this->cache_key ] = $data; + if ( is_multisite() ) { update_site_option( $this->cache_key, $data ); } else { From 8df548685ab8edb18a1d270efa7f083190ed5724 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 12 Dec 2025 13:01:37 -0400 Subject: [PATCH 2/8] Remove var --- classes/models/FrmFormApi.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/classes/models/FrmFormApi.php b/classes/models/FrmFormApi.php index bbf9b00168..d62b4878c2 100644 --- a/classes/models/FrmFormApi.php +++ b/classes/models/FrmFormApi.php @@ -332,8 +332,6 @@ protected function get_cached() { return $values; } - static $count = 0; - /** * Get the cache for the network if multisite. * From c44be72192dc014fdb168ec4cfffb2dad6bd795d Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 12 Dec 2025 13:09:27 -0400 Subject: [PATCH 3/8] Use autoload --- classes/models/FrmFormApi.php | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/classes/models/FrmFormApi.php b/classes/models/FrmFormApi.php index d62b4878c2..d21ad1d0dc 100644 --- a/classes/models/FrmFormApi.php +++ b/classes/models/FrmFormApi.php @@ -27,15 +27,6 @@ class FrmFormApi { */ protected $new_days = 90; - /** - * Memoized API info. - * This is indexed by cache key. - * It allows us to avoid extra calls to get_option(). - * - * @var array|null - */ - private static $memoized_api_info = array(); - /** * @since 3.06 * @@ -342,21 +333,15 @@ protected function get_cached() { protected function get_cached_option() { $key = $this->cache_key; - if ( isset( self::$memoized_api_info[ $key ] ) ) { - return self::$memoized_api_info[ $key ]; - } - if ( is_multisite() ) { $cached = get_site_option( $this->cache_key ); if ( $cached ) { - self::$memoized_api_info[ $key ] = $cached; return $cached; } } - self::$memoized_api_info[ $key ] = get_option( $this->cache_key ); - return self::$memoized_api_info[ $key ]; + return get_option( $this->cache_key ); } /** @@ -373,12 +358,12 @@ protected function set_cached( $addons ) { 'version' => FrmAppHelper::plugin_version(), ); - self::$memoized_api_info[ $this->cache_key ] = $data; - if ( is_multisite() ) { update_site_option( $this->cache_key, $data ); } else { - update_option( $this->cache_key, $data, 'no' ); + // Autoload the license cache because it gets called everywhere. + $autoload = 0 === strpos( $this->cache_key, 'frm_addons_l' ); + update_option( $this->cache_key, $data, $autoload ); } } From ee786a9fb461bb050fabc0e535eee61f775f62ac Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 12 Dec 2025 13:13:47 -0400 Subject: [PATCH 4/8] Clean up --- classes/models/FrmFormApi.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/classes/models/FrmFormApi.php b/classes/models/FrmFormApi.php index d21ad1d0dc..d2f2ee99bb 100644 --- a/classes/models/FrmFormApi.php +++ b/classes/models/FrmFormApi.php @@ -301,21 +301,13 @@ protected function get_cached() { return false; } - // If the api call is running, we can use the expired cache. - $is_expired = empty( $cache['timeout'] ) || time() > $cache['timeout']; - if ( $is_expired ) { - if ( ! $this->is_running() ) { - return false; - } - - // If the api call is running, we can use the expired cache. - $version = FrmAppHelper::plugin_version(); - $for_current = isset( $cache['version'] ) && $cache['version'] == $version; + $is_expired = empty( $cache['timeout'] ) || time() > $cache['timeout']; + $version = FrmAppHelper::plugin_version(); + $for_current = isset( $cache['version'] ) && $cache['version'] == $version; - if ( ! $for_current ) { - // Force a new check. - return false; - } + // If the api call is running, we can use the expired cache. + if ( ( $is_expired || ! $for_current ) && ! $this->is_running() ) { + return false; } $values = json_decode( $cache['value'], true ); From 9cedb8acef3f8d111105ce96786c30514c5aa88a Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 12 Dec 2025 13:14:12 -0400 Subject: [PATCH 5/8] Drop unused var --- classes/models/FrmFormApi.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/classes/models/FrmFormApi.php b/classes/models/FrmFormApi.php index d2f2ee99bb..520dc8d617 100644 --- a/classes/models/FrmFormApi.php +++ b/classes/models/FrmFormApi.php @@ -323,8 +323,6 @@ protected function get_cached() { * @return mixed */ protected function get_cached_option() { - $key = $this->cache_key; - if ( is_multisite() ) { $cached = get_site_option( $this->cache_key ); From a9adfb50b4a53f7a17e7c7fcb6ee3b9a3037526e Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 12 Dec 2025 13:15:40 -0400 Subject: [PATCH 6/8] Simplify --- classes/models/FrmFormApi.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/classes/models/FrmFormApi.php b/classes/models/FrmFormApi.php index 520dc8d617..4d96615796 100644 --- a/classes/models/FrmFormApi.php +++ b/classes/models/FrmFormApi.php @@ -302,8 +302,7 @@ protected function get_cached() { } $is_expired = empty( $cache['timeout'] ) || time() > $cache['timeout']; - $version = FrmAppHelper::plugin_version(); - $for_current = isset( $cache['version'] ) && $cache['version'] == $version; + $for_current = isset( $cache['version'] ) && $cache['version'] === FrmAppHelper::plugin_version(); // If the api call is running, we can use the expired cache. if ( ( $is_expired || ! $for_current ) && ! $this->is_running() ) { From 13a0c45ce5e4bc5e00e522b4cd3561860951c812 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 12 Dec 2025 13:18:15 -0400 Subject: [PATCH 7/8] Add comments --- classes/models/FrmFormApi.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/classes/models/FrmFormApi.php b/classes/models/FrmFormApi.php index 4d96615796..0d5f44738e 100644 --- a/classes/models/FrmFormApi.php +++ b/classes/models/FrmFormApi.php @@ -304,7 +304,9 @@ protected function get_cached() { $is_expired = empty( $cache['timeout'] ) || time() > $cache['timeout']; $for_current = isset( $cache['version'] ) && $cache['version'] === FrmAppHelper::plugin_version(); - // If the api call is running, we can use the expired cache. + // Avoid old cached data, unless we're currently trying to query for new data. + // The call to $this->is_running likely triggers a database query, so only call if if we're expired. + // (Rather than the other way around, which is less efficient). if ( ( $is_expired || ! $for_current ) && ! $this->is_running() ) { return false; } From b449ec9e0f31e7271c5799c66dfa0f67fd4ba8d2 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 12 Dec 2025 13:25:41 -0400 Subject: [PATCH 8/8] Improve / optimize logic --- classes/models/FrmFormApi.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/classes/models/FrmFormApi.php b/classes/models/FrmFormApi.php index 0d5f44738e..13b030d57a 100644 --- a/classes/models/FrmFormApi.php +++ b/classes/models/FrmFormApi.php @@ -301,13 +301,16 @@ protected function get_cached() { return false; } - $is_expired = empty( $cache['timeout'] ) || time() > $cache['timeout']; - $for_current = isset( $cache['version'] ) && $cache['version'] === FrmAppHelper::plugin_version(); + $is_expired = empty( $cache['timeout'] ) || time() > $cache['timeout']; + + if ( ! $is_expired && isset( $cache['version'] ) && $cache['version'] !== FrmAppHelper::plugin_version() ) { + $is_expired = true; + } // Avoid old cached data, unless we're currently trying to query for new data. // The call to $this->is_running likely triggers a database query, so only call if if we're expired. // (Rather than the other way around, which is less efficient). - if ( ( $is_expired || ! $for_current ) && ! $this->is_running() ) { + if ( $is_expired && ! $this->is_running() ) { return false; }