• Resolved hassantafreshi

    (@hassantafreshi)


    Hi,

    As the developer of the?Easy Form Builder plugin, I’m seeking some advice about potential conflicts with WP REST Cache.

    Here is the function I am currently using for internal caching:

    public function get_efbFunction($state) {     if (isset($this->efbFunction)) return $this->efbFunction;     $efbFunctionInstance;     if (false === ($efbFunctionInstance = wp_cache_get('efbFunctionInstance', 'emsfb')))        {         if (!class_exists('Emsfb\efbFunction')) {             require_once(EMSFB_PLUGIN_DIRECTORY . 'includes/functions.php');         }         $efbFunctionInstance = new \Emsfb\efbFunction();         wp_cache_set('efbFunctionInstance', $efbFunctionInstance, 'emsfb', 3600); // 1 hour cache     }     $this->efbFunction = $efbFunctionInstance;     if ($state == 1) return $this->efbFunction;}

    My concern is whether using?wp_cache_get?and?wp_cache_set?for internal caching might cause any conflicts with WP REST Cache. Has anyone had similar experiences or know of best practices to ensure they work together smoothly?

    Any guidance or insights would be greatly appreciated.

    Thank you for your help!

    Best regards,

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @hassantafreshi

    Thank you for using our plugin!

    First of all we use the Transients API to store our caches instead of the Object Cache because we want persistent caches regardless of any persistent cache plugin being installed. However if a persistent cache plugin is installed the transients API will use the wp_cache_get and wp_cache_set functions.

    Now to your question: will there be any conflicts? I don’t think so because we use different cache keys. Only if those keys would match a conflict would occur. The odds of that happening are very limited since all our cache keys start with wp_rest_cache_ and then a hash of the requested URI. Since all your caches are prefixed with efb there will be no conflicts.

    Thread Starter hassantafreshi

    (@hassantafreshi)

    Thank you for the quick response. We use the following function to handle page caching with plugins. Do you have a structure similar to other plugins that we can add to solve the caching issue?

    public function cache_cleaner_Efb($page_id){

    if (defined('LSCWP_V') || defined('LSCWP_BASENAME' )){

    //litespeed done

    do_action( 'litespeed_purge_post', $page_id );

    }else if (function_exists('rocket_clean_post')){

    //wp-rocket done

    $r = rocket_clean_post($page_id);

    }elseif (function_exists('wp_cache_post_change')){

    //WP Super Cache done

    //Jetpack done

    $GLOBALS['super_cache_enabled']=1;

    wp_cache_post_change($page_id);

    }elseif(function_exists('autoptimize_filter_js_noptimize ')){

    //auto-Optimize done

    autoptimize_filter_js_exclude(['jquery.min-efb.js','core-efb.js']);

    autoptimize_filter_js_noptimize();

    }elseif(class_exists('WPO_Page_Cache')){

    //WP-Optimize done

    \WPO_Page_Cache::delete_single_post_cache($page_id);

    }elseif(function_exists('w3tc_flush_post')){

    //W3 Total Cache done

    w3tc_flush_post($page_id);

    }elseif(function_exists('wpfc_clear_post_cache_by_id')){

    //WP Fastest Cache done

    wpfc_clear_post_cache_by_id($page_id);

    }elseif(has_action('wphb_clear_page_cache')){

    //Hummingbird done

    do_action( 'wphb_clear_page_cache', $page_id );

    }

    }

    Click here to get the full source code on GitHub..

    Thank you.

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @hassantafreshi

    You could use this:

    \WP_REST_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_related_caches( $post_id, $object_type );

    Where $post_id in your case would be $page_id and $object_type is the post type of the item (so 'page' ?).

    Thread Starter hassantafreshi

    (@hassantafreshi)

    @rockfire
    Thank you for your response. Is there a way to clear the cache in your plugin using only the post_id? Specifically, I want to clear the cache for a specific page by referring to its post_id in the cache deletion function.

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @hassantafreshi

    At this point we do not have a function for that. The reason you need to specify it, is because the object type can be a post type or a taxonomy. So based only on the ID we can not say for sure what the object type is (a page can have the same ID as a term)

    Thread Starter hassantafreshi

    (@hassantafreshi)

    Thank you for your response. Could you please clarify the value of the $object_type variable? Given our situation, could you provide guidance on how we can assist users of both plugins effectively?

    Thank you.

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @hassantafreshi

    $object_type is the type of the object with ID $post_id (the name of that variable is maybe not very well chosen ??). Meaning: if $post_id refers to a post of a (custom) post type, $object_type is the slug of that (custom) post type. But if $post_id refers to a (custom) taxonomy term, $object_type is the slug of that (custom) taxonomy.

    Some example to hopefully make it more clear:

    Example 1:
    We have a page with ID = 42:
    $post_id = 42
    $object_type = 'page'

    Example 2:
    We have an event (= custom post type) with ID = 43:
    $post_id = 42
    $object_type = 'event'

    Example 3:
    We have a category with term ID = 44:
    $post_id = 44
    $object_type = 'category'

    Example 4:
    We have an event category (= custom taxonomy) with ID = 45:
    $post_id = 45
    $object_type = 'event_category'

    Thread Starter hassantafreshi

    (@hassantafreshi)

    @rockfire Thank you for your detailed explanation. This kind of thing always happens when naming variables during development ??

    I have added these code , what you think ?

    case 'wp-rest-cache':
    //\WP_REST_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_related_caches( $post_id, $object_type );
    $typPage = get_post_type($page_id);
    if(class_exists('\WP_REST_Cache_Plugin\Includes\Caching\Caching')) \WP_REST_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_related_caches( $page_id, $typPage );
    break;

    source

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Does This Custom Caching Code Interfere with WP REST Cache?’ is closed to new replies.