Skip to content

Commit

Permalink
Update index on save and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Oct 2, 2019
1 parent 4b2a916 commit 8595789
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions lib/mshoplib/src/MShop/Index/Manager/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function aggregate( \Aimeos\MW\Criteria\Iface $search, $key )
public function deleteItems( array $ids )
{
$this->getManager()->deleteItems( $ids );
return $this;
return $this->clearItems( $ids )->clearCache( $ids );
}


Expand Down Expand Up @@ -362,6 +362,36 @@ public function rebuildIndex( array $items = [] )
}


/**
* Adds a new product to the storage or updates an existing one.
*
* @param \Aimeos\MShop\Product\Item\Iface $item Product item that should be saved to the storage
* @param boolean $fetch True if the new ID should be returned in the item
* @return \Aimeos\MShop\Product\Item\Iface Updated item including the generated ID
*/
public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
{
$item = parent::saveItem( $item, $fetch );
$this->rebuildIndex( [$item->getId() => $item] );
return $item;
}


/**
* Adds or updates a list of product items.
*
* @param \Aimeos\MShop\Common\Item\Iface[] $items List of item object whose data should be saved
* @param boolean $fetch True if the new ID should be returned in the item
* @return \Aimeos\MShop\Common\Item\Iface[] Saved item objects
*/
public function saveItems( array $items, $fetch = true )
{
$items = parent::saveItems( $items, $fetch );
$this->rebuildIndex( $items );
return $items;
}


/**
* Searches for items matching the given criteria.
*
Expand Down Expand Up @@ -492,8 +522,6 @@ public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [],
*/
protected function clearItems( array $ids )
{
if( empty( $ids ) ) { return; }

foreach( $this->getSubManagers() as $submanager ) {
$submanager->deleteItems( $ids );
}
Expand All @@ -505,17 +533,19 @@ protected function clearItems( array $ids )
/**
* Deletes the cache entries using the given product IDs.
*
* @param string[] $productIds List of product IDs
* @param string[] $ids List of product IDs
* @return \Aimeos\MShop\Index\Manager\Iface Manager object for chaining method calls
*/
protected function clearCache( array $productIds )
protected function clearCache( array $ids )
{
$tags = [];

foreach( $productIds as $prodId ) {
$tags[] = 'product-' . $prodId;
foreach( $ids as $id ) {
$tags[] = 'product-' . $id;
}

$this->getContext()->getCache()->deleteByTags( $tags );
return $this;
}


Expand Down

0 comments on commit 8595789

Please sign in to comment.