From 3cfbf2bce5bfb076e7f77dde9b98c9c1e7d47ee3 Mon Sep 17 00:00:00 2001 From: Marek Isalski Date: Sun, 20 May 2018 16:17:40 +0100 Subject: [PATCH] cache invalidation based on hooks --- fulcrm_shoppingcart.module | 56 +++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/fulcrm_shoppingcart.module b/fulcrm_shoppingcart.module index 2ea0ec2..fa6474c 100644 --- a/fulcrm_shoppingcart.module +++ b/fulcrm_shoppingcart.module @@ -147,10 +147,16 @@ function fulcrm_shoppingcart_theme( $existing, $type, $theme, $path ) { ); } -function fulcrm_shoppingcart_invalidate_cache() { - if ( array_key_exists( 'fulcrm_shoppingcart_id', $_SESSION ) ) { - $cid = 'fulcrm_shoppingcart:shoppingcart:' . $_SESSION[ 'fulcrm_shoppingcart_id' ]; - cache_clear_all( $cid, 'cache' ); +function fulcrm_shoppingcart_invalidate_cache( $cart_id = NULL ) { + if ( is_null( $cart_id ) ) { + if ( array_key_exists( 'fulcrm_shoppingcart_id', $_SESSION ) ) + $cart_id = $_SESSION[ 'fulcrm_shoppingcart_id' ]; + } + if ( $cart_id === '*' ) { + cache_clear_all( $cid = 'fulcrm_shoppingcart:shoppingcart:', $bin = 'cache', $wildcard = TRUE ); + } else if ( !is_null( $cart_id ) ) { + $cid = 'fulcrm_shoppingcart:shoppingcart:' . $cart_id; + cache_clear_all( $cid = $cid, $bin = 'cache' ); } } @@ -894,3 +900,45 @@ function fulcrm_shoppingcart_user_logout( $account ) { unset( $_SESSION[ 'fulcrm_shoppingcart_id' ] ); } } + +function fulcrm_shoppingcart_create_for_fulcrm_shoppingcart( &$vars ) { +} + +function fulcrm_shoppingcart_update_for_fulcrm_shoppingcart( &$vars ) { + if ( fulcrm_apiv2_url_to_type( $data[ 'url' ] ) === 'shoppingcart' ) { + $cart_id = fulcrm_apiv2_url_to_pk( $data[ 'url' ] ); + if ( $cart_id ) + fulcrm_shoppingcart_invalidate_cache( $cart_id ); + } +} + +function fulcrm_shoppingcart_delete_for_fulcrm_shoppingcart( &$vars ) { + if ( fulcrm_apiv2_url_to_type( $data[ 'url' ] ) === 'shoppingcart' ) { + $cart_id = fulcrm_apiv2_url_to_pk( $data[ 'url' ] ); + if ( $cart_id ) + fulcrm_shoppingcart_invalidate_cache( $cart_id ); + } +} + +function fulcrm_shoppingcart_create_for_fulcrm_shoppingitem( &$vars ) { +} + +function fulcrm_shoppingcart_update_for_fulcrm_shoppingitem( &$vars ) { + if ( fulcrm_apiv2_url_to_type( $data[ 'shoppingcart' ] ) === 'shoppingcart' ) { + $cart_id = fulcrm_apiv2_url_to_pk( $data[ 'shoppingcart' ] ); + if ( $cart_id ) + fulcrm_shoppingcart_invalidate_cache( $cart_id ); + } +} + +function fulcrm_shoppingcart_delete_for_fulcrm_shoppingitem( &$vars ) { + fulcrm_shoppingcart_invalidate_cache( '*' ); +} + +function fulcrm_shoppingcart_create_for_fulcrm_transaction( &$vars ) { + if ( fulcrm_apiv2_url_to_type( $data[ 'content_object' ] ) === 'shoppingcart' ) { + $cart_id = fulcrm_apiv2_url_to_pk( $data[ 'content_object' ] ); + if ( $cart_id ) + fulcrm_shoppingcart_invalidate_cache( $cart_id ); + } +}