From 591fee6111525471af0fa685afab4279a34e91d5 Mon Sep 17 00:00:00 2001 From: John Saunders Date: Tue, 8 Mar 2022 13:31:40 +0000 Subject: [PATCH] fix cache speed issue --- src/DB/ShardCache.php | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/DB/ShardCache.php b/src/DB/ShardCache.php index 17af126..aed84bb 100644 --- a/src/DB/ShardCache.php +++ b/src/DB/ShardCache.php @@ -35,17 +35,33 @@ public static function execute( return $shardDb->__execute( $preStatement, $useNewConnection, $rollbacks ); } $key = $preStatement->getHashKey(); - if ( $remove && $preStatement->getUuid() ) { - $shardDb->getPdoCache()->clean( $preStatement->getUuid() ); + if ( $remove ) { + if ( $preStatement->getUuid() ) { + $shardDb->getPdoCache()->clean( $preStatement->getUuid() ); + if ( $preStatement->isFreshDataOnly() ) { + $shardDb->getPdoCache()->cleanAllMatching( $preStatement->getUuid() ); + } + } else { + $shardDb->getPdoCache()->clean( $key ); + } } elseif ( ! $preStatement->isFreshDataOnly() && ! $useNewConnection ) { - $cacheRead = $shardDb->getPdoCache()->read( $key ); - if ( $cacheRead instanceof ResultsInterface && $cacheRead->isSuccessful() ) { - return $cacheRead; + if ( $preStatement->getUuid() ) { + $cacheRead = $shardDb->getPdoCache()->read( $preStatement->getUuid() ); + if ( $cacheRead instanceof ResultsInterface && $cacheRead->isSuccessful() ) { + return $cacheRead; + } + } else { + $cacheRead = $shardDb->getPdoCache()->read( $key ); + if ( $cacheRead instanceof ResultsInterface && $cacheRead->isSuccessful() ) { + return $cacheRead; + } } } $returnValue = $shardDb->__execute( $preStatement, $useNewConnection, $rollbacks ); - if ( $returnValue instanceof ResultsInterface && $preStatement->isSelectQuery() ) { + if ( $returnValue instanceof ResultsInterface && $preStatement->isSelectQuery() && ! $preStatement->getUuid() ) { $shardDb->getPdoCache()->write( $key, $returnValue ); + } else if ( $returnValue instanceof ResultsInterface && $preStatement->isSelectQuery() && $preStatement->getUuid() ) { + $shardDb->getPdoCache()->write( $preStatement->getUuid(), $returnValue ); } return $returnValue;