Skip to content

Commit

Permalink
Merge pull request #20015 from salehhashemi1992/feature/extract-prepa…
Browse files Browse the repository at this point in the history
…reCacheData-method-

Extract common cache data preparation logic to new method
  • Loading branch information
bizley authored Nov 12, 2023
2 parents aa9adc8 + abcd82f commit a8aa2cf
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions framework/caching/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,21 +294,7 @@ public function multiSet($items, $duration = null, $dependency = null)
$duration = $this->defaultDuration;
}

if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency($this);
}

$data = [];
foreach ($items as $key => $value) {
if ($this->serializer === null) {
$value = serialize([$value, $dependency]);
} elseif ($this->serializer !== false) {
$value = call_user_func($this->serializer[0], [$value, $dependency]);
}

$key = $this->buildKey($key);
$data[$key] = $value;
}
$data = $this->prepareCacheData($items, $dependency);

return $this->setValues($data, $duration);
}
Expand Down Expand Up @@ -343,6 +329,21 @@ public function madd($items, $duration = 0, $dependency = null)
* @since 2.0.7
*/
public function multiAdd($items, $duration = 0, $dependency = null)
{
$data = $this->prepareCacheData($items, $dependency);

return $this->addValues($data, $duration);
}

/**
* Prepares data for caching by serializing values and evaluating dependencies.
*
* @param array $items The items to be cached.
* @param mixed $dependency The dependency to be evaluated.
*
* @return array The prepared data for caching.
*/
private function prepareCacheData($items, $dependency)
{
if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency($this);
Expand All @@ -360,7 +361,7 @@ public function multiAdd($items, $duration = 0, $dependency = null)
$data[$key] = $value;
}

return $this->addValues($data, $duration);
return $data;
}

/**
Expand Down

0 comments on commit a8aa2cf

Please sign in to comment.