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
Key – dokan_seller_products-[hash]
Group – dokan_cache_seller_product_data_[id]
Invalidate When –
- On Add New Product –
dokan_new_product_addedaction hook - On Update Product
dokan_product_updatedaction hook - After Product Duplicate From Seller
dokan_product_duplicate_after_saveaction hook - On Delete Product
dokan_product_deletedaction hook - After Bulk Product Status Change From Seller
dokan_bulk_product_status_changeaction hook - After Bulk Product Delete From Seller
dokan_bulk_product_deleteaction hook - Duplicate Product from woocommerce –
woocommerce_product_duplicateaction hook - Update Product from woocommerce –
woocommerce_update_productaction hook - After Bulk Import from woocommerce/Dokan Import Section –
woocommerce_product_import_inserted_product_objectaction hook
# Cache: Seller Orders List Data
Key – dokan_seller_orders-[hash]
Group – dokan_seller_data_[id]
Invalidate When –
- On Update Checkout –
dokan_checkout_update_order_metaaction hook - Woocommerce Order Status Change –
woocommerce_order_status_changedaction hook - Woocommerce Order Update –
woocommerce_update_orderaction hook
# Cache: Seller Coupons List Data
Key – dokan_seller_coupons-[hash]
Group – dokan_seller_coupons_data_[id]
Invalidate When –
- After Coupon Create from Dokan –
dokan_after_coupon_createaction hook - After Coupon Delete from Dokan –
dokan_after_coupon_deleteaction hook - After Create Coupon From Woocommerce –
woocommerce_new_couponaction hook - After Update Coupon from Woocommerce –
woocommerce_update_couponaction hook - After Trash Post from Woocommerce –
wp_trash_postaction hook - After Untrash post from Woocommerce –
untrashed_postaction hook
# Cache: Store Reviews Data
Key 1 – dokan-store-reviews-[hash]
Key 2 – dokan-store-reviews-frontend-[hash]
Group – dokan_store_reviews
Invalidate When –
- When Submit Review from frontend –
DSR_View / submit_review()functions / dsr_save_store_review()
- 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()
- Update Review –
# Cache: Product Reviews Data
Key 1 – dokan-product-reviews-[seller_id]-[post_type]-[limit]-[status]
Key 2 – dokan-count-comments-product-[seller_id], Group – dokan-lite
Group – dokan_product_reviews
Invalidate When –
- When Submit Product Review from frontend –
dokan-pro / includes / Review.php > ajax_comment_status()
- Update Product Review Comment [Both from theme & Seller Panel] –
dokan-pro / includes / Review.php > ajax_update_comment()
- Bulk Process from Seller Panel –
dokan-pro / includes / Review.php > handle_status()
# Cache: Vendor List Data
Key 1 – dokan-vendors-[hash]
Group – dokan_vendors
Invalidate When –
- When do stuffs for the following action/hooks in
dokan / includes / Vendor / Cache.php–dokan_new_vendordokan_update_vendordokan_delete_vendordokan_vendor_enableddokan_vendor_disabled