Skip to content

Commit

Permalink
rename Cache\Hooks::normailzeRecord() to removeNullValues()
Browse files Browse the repository at this point in the history
  • Loading branch information
yeaha committed Dec 2, 2016
1 parent a5c35ec commit 786fddd
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/DataMapper/Cache/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function __afterInsert(\Owl\DataMapper\Data $data)

if ($policy['insert']) {
$record = $this->unpack($data);
$record = $this->normalizeCacheRecord($record);
$record = $this->removeNullValues($record);

$this->saveCache($id, $record);
} elseif ($policy['not_found']) {
Expand All @@ -96,7 +96,7 @@ protected function __afterUpdate(\Owl\DataMapper\Data $data)

if ($policy['update']) {
$record = $this->unpack($data);
$record = $this->normalizeCacheRecord($record);
$record = $this->removeNullValues($record);

$this->saveCache($id, $record);
} else {
Expand Down Expand Up @@ -178,7 +178,7 @@ protected function doFind(array $id, \Owl\Service $service = null, $collection =
}

if ($record = parent::doFind($id, $service, $collection)) {
$record = $this->normalizeCacheRecord($record);
$record = $this->removeNullValues($record);
$this->saveCache($id, $record);
} else {
$policy = $this->getCachePolicy();
Expand All @@ -192,25 +192,6 @@ protected function doFind(array $id, \Owl\Service $service = null, $collection =
return $record;
}

/**
* remove NULL value from record.
*
* @param array $record
*
* @return array
*/
protected function normalizeCacheRecord(array $record)
{
// 值为NULL的字段不用缓存
foreach ($record as $key => $val) {
if ($val === null) {
unset($record[$key]);
}
}

return $record;
}

/**
* @param string $key
*
Expand Down Expand Up @@ -259,4 +240,23 @@ protected function getCacheTTL()
{
return $this->hasOption('cache_ttl') ? $this->getOption('cache_ttl') : 300;
}

/**
* remove NULL value from record.
*
* @param array $record
*
* @return array
*/
private function removeNullValues(array $record)
{
// 值为NULL的字段不用缓存
foreach ($record as $key => $val) {
if ($val === null) {
unset($record[$key]);
}
}

return $record;
}
}

0 comments on commit 786fddd

Please sign in to comment.