Dokan Caching Documentation

  • by

How to Implement Caching in Dokan:

There are three important methods to implement caching in Dokan.

  • CacheHelper::get_cache( $cache_key, $cache_group = null, $forced = false )
  • CacheHelper::set_cache( $cache_key, $cache_value, $cache_group = null )
  • CacheHelper::invalidate_cache_group( $cache_group )

More methods, could be checked from includes/Cache/CacheHelper.php

Basic Implementation snippet:

// Get Cache
$data = CacheHelper::get_cache( $cache_key, $cache_group );

// Set Cache
if ( false === $data ) {
   $data = []; // find data and set in $data
   CacheHelper::set_cache( $cache_key, $data, $cache_group );
}

// Invalidate Cache
CacheHelper::invalidate_cache_group( $cache_group );

Example of caching in Sellers Orders

$status      = "pending";
$seller_id   = dokan_get_current_user_id();

$cache_group = "dokan_seller_data_{$seller_id}";
$cache_key   = "dokan-seller-orders-{$status}";

$orders      = CacheHelper::get_cache( $cache_key, $cache_group );

if ( false === $orders ) {
   // Process to get orders
   $orders = [];

   CacheHelper::set_cache( $cache_key, $orders, $cache_group );
}

return $orders;

After set, when needs to invalidate, we should make that on specific class of that. Like, Order’s caching would be invalidated in includes/Order/Cache.php

CacheHelper::invalidate_cache_group( "dokan_seller_data_$seller_id" );

# Cache: Seller Products List Data

Keydokan_seller_products-[hash]

Groupdokan_cache_seller_product_data_[id]

Invalidate When

  1. On Add New Product – dokan_new_product_added action hook
  2. On Update Product dokan_product_updated action hook
  3. After Product Duplicate From Seller dokan_product_duplicate_after_save action hook
  4. On Delete Product dokan_product_deleted action hook
  5. After Bulk Product Status Change From Seller dokan_bulk_product_status_change action hook
  6. After Bulk Product Delete From Seller dokan_bulk_product_delete action hook
  7. Duplicate Product from woocommerce – woocommerce_product_duplicate action hook
  8. Update Product from woocommerce – woocommerce_update_product action hook
  9. After Bulk Import from woocommerce/Dokan Import Section – woocommerce_product_import_inserted_product_object action hook

# Cache: Seller Orders List Data

Keydokan_seller_orders-[hash]

Groupdokan_seller_data_[id]

Invalidate When

  1. On Update Checkout – dokan_checkout_update_order_meta action hook
  2. Woocommerce Order Status Change – woocommerce_order_status_changed action hook
  3. Woocommerce Order Update – woocommerce_update_order action hook

# Cache: Seller Coupons List Data

Keydokan_seller_coupons-[hash]

Groupdokan_seller_coupons_data_[id]

Invalidate When

  1. After Coupon Create from Dokan – dokan_after_coupon_create action hook
  2. After Coupon Delete from Dokan – dokan_after_coupon_delete action hook
  3. After Create Coupon From Woocommerce – woocommerce_new_coupon action hook
  4. After Update Coupon from Woocommerce – woocommerce_update_coupon action hook
  5. After Trash Post from Woocommerce – wp_trash_post action hook
  6. After Untrash post from Woocommerce – untrashed_post action hook

# Cache: Store Reviews Data

Key 1dokan-store-reviews-[hash]

Key 2dokan-store-reviews-frontend-[hash]

Groupdokan_store_reviews

Invalidate When

  1. When Submit Review from frontend –
    • DSR_View / submit_review()
    • functions / dsr_save_store_review()
  2. CRUD Review Operations From Backend –
    • Update Review – class-store-reviews-controller.php / update_review()
    • Delete Review – class-store-reviews-controller.php / delete_review()
    • Restore Review – class-store-reviews-controller.php / restore_reviews()
    • Batch Delete/Restore Review – class-store-reviews-controller.php / batch_items()

# Cache: Product Reviews Data

Key 1dokan-product-reviews-[seller_id]-[post_type]-[limit]-[status]

Key 2 – dokan-count-comments-product-[seller_id], Groupdokan-lite

Groupdokan_product_reviews

Invalidate When

  1. When Submit Product Review from frontend –
    • dokan-pro / includes / Review.php > ajax_comment_status()
  2. Update Product Review Comment [Both from theme & Seller Panel] –
    • dokan-pro / includes / Review.php > ajax_update_comment()
  3. Bulk Process from Seller Panel –
    • dokan-pro / includes / Review.php > handle_status()

# Cache: Vendor List Data

Key 1dokan-vendors-[hash]

Groupdokan_vendors

Invalidate When

  1. When do stuffs for the following action/hooks in dokan / includes / Vendor / Cache.php
    • dokan_new_vendor
    • dokan_update_vendor
    • dokan_delete_vendor
    • dokan_vendor_enabled
    • dokan_vendor_disabled

Leave a Reply

Your email address will not be published. Required fields are marked *