Skip to content

Commit

Permalink
Extract common cache data preparation logic to prepareCacheData method
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 committed Nov 1, 2023
1 parent 6804fbe commit abcd82f
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 abcd82f

Please sign in to comment.