Plugin Directory

Changeset 2787887


Ignore:
Timestamp:
09/20/2022 11:20:21 PM (4 years ago)
Author:
nfusionsolutions
Message:

cache performance enhancement

Location:
precious-metals-automated-product-pricing-pro/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • precious-metals-automated-product-pricing-pro/trunk/WooCommerce_Plugin_Nfusion.php

    r2759355 r2787887  
    77 * Author URI: https://nfusionsolutions.com
    88 * WC requires at least: 3.0.0
    9  * WC tested up to: 6.1.1
     9 * WC tested up to: 6.9.2
    1010 * License: GPLv2 or later
    1111 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
    1212 
    13  * Version: 2.9.13
     13 * Version: 2.9.14
    1414*/
    15 define("NFS_CATALOG_PLUGIN_VERSION",   "2.9.13");
     15define("NFS_CATALOG_PLUGIN_VERSION",   "2.9.14");
     16define("NFS_CATALOG_REMOTE_TIMEOUT_SECONDS", 2);
    1617
    1718function nfs_catalog_plugin_tryGetProduct($skus){
     
    3233    $currency = get_woocommerce_currency();
    3334    $ttlSeconds = 60;//cache timeout in seconds
     35    $ttlSecondarySeconds = 3600;//very long timeout for secondary cache
    3436    $productsMapKey = 'nfs_catalog_products_all_' . $currency;
     37    $productsMapSecondaryKey = 'nfs_catalog_products_all_secondary_' . $currency;
    3538    //first check cache
    3639    $productsMap = get_transient($productsMapKey);
     
    4952   
    5053    if ($fetchRemote === true) {
    51         $productsMap = nfs_catalog_plugin_fetch_products_from_remote($currency);
    52         if($productsMap !== false) {
    53             //only cache if we got a valid response
    54             set_transient($productsMapKey, $productsMap, $ttlSeconds);
    55         }
    56         else {
    57             return false;//failed to find in cache and failed remote fetch
     54        //we want too prevent many sessions from trying to make the remote call at the same time
     55        //this can happen around cache expiry boundaries. The problem can become more pronounced if the remote
     56        //server is slow to reponse, since the remote calls are blocking. An asynchronous approach with a true semaphore
     57        //might be preferred here, but options are limited in WordPress/PHP stack without plugins (whose existence we cannot guarantee)
     58       
     59        //here we will use a second transient as a sort of pseudo-semaphore. It will not truly prevent duplicate requests, but it may reduce them preventing stampede conditions.
     60        $semaphoreKey = 'nfs_catalog_request_semaphore_' . $currency;
     61        $semaphoreInUse = get_transient($semaphoreKey);
     62        if ($semaphoreInUse === false || !isset($semaphoreInUse)) {
     63            set_transient($semaphoreKey, true, NFS_CATALOG_REMOTE_TIMEOUT_SECONDS);//set transient ttl same as remote request timeout
     64            $remoteResult = nfs_catalog_plugin_fetch_products_from_remote($currency);
     65            if($remoteResult !== false) {//only cache if we got a valid response
     66                //store the previous data in the secondary cache
     67                set_transient($productsMapSecondaryKey, $productsMap, $ttlSecondarySeconds);
     68               
     69                //store new data in first level cache
     70                $productsMap = $remoteResult;
     71                set_transient($productsMapKey, $productsMap, $ttlSeconds);
     72            }
     73        }
     74       
     75        if ($productsMap === false || !isset($productsMap)) {
     76            //if we don't have product data at this point, grab from secondary
     77            $productsMap = get_transient($productsMapSecondaryKey);
    5878        }
    5979    }
     
    7292   
    7393    $args = array(
     94        'timeout' => NFS_CATALOG_REMOTE_TIMEOUT_SECONDS,//timeout in seconds
    7495        'headers' => array(
    7596            'User-Agent' => 'wpwc-'.NFS_CATALOG_PLUGIN_VERSION,
  • precious-metals-automated-product-pricing-pro/trunk/readme.txt

    r2759798 r2787887  
    55License: GPLv2 or later
    66License URI: https://www.gnu.org/licenses/gpl-2.0.html
    7 Tested up to: 5.9
     7Tested up to: 6.0.2
    88Requires at least: 3.5.0
    9 Stable tag: 2.9.13
     9Stable tag: 2.9.14
    1010
    1111Automated realtime metals spot and futures data dynamically updates product prices in your store for Gold, Silver, Platinum, and Palladium
     
    145145* add compatibility for variable products
    146146
     147= 2.9.14 =
     148* cache performance enhancement
     149
    147150== Upgrade Notice ==
    148151Latest Stable Version
Note: See TracChangeset for help on using the changeset viewer.