Skip to content

Commit

Permalink
增加Data::findOrCreate()方法
Browse files Browse the repository at this point in the history
重构内部$id值数据格式,统一使用数组格式
  • Loading branch information
yeaha committed Dec 2, 2016
2 parents 44bb5e9 + 786fddd commit 7690d48
Show file tree
Hide file tree
Showing 17 changed files with 448 additions and 212 deletions.
15 changes: 7 additions & 8 deletions src/DataMapper/Cache/Apc.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
<?php

namespace Owl\DataMapper\Cache;

trait Apc
{
use Hooks;

protected function getCache($id)
protected function getCache(array $id)
{
$key = $this->getCacheKey($id);
$fn = $this->getFn('fetch');
$fn = $this->getFn('fetch');

return $fn($key) ?: [];
}

protected function deleteCache($id)
protected function deleteCache(array $id)
{
$key = $this->getCacheKey($id);
$fn = $this->getFn('delete');
$fn = $this->getFn('delete');

return $fn($key);
}

protected function saveCache($id, array $record, $ttl = null)
protected function saveCache(array $id, array $record, $ttl = null)
{
$key = $this->getCacheKey($id);
$ttl = $ttl ?: $this->getCacheTTL();
$fn = $this->getFn('store');
$fn = $this->getFn('store');

return $fn($key, $record, $ttl);
}
Expand All @@ -45,6 +44,6 @@ private function getFn($method)
}
}

return $prefix.$method;
return $prefix . $method;
}
}
104 changes: 49 additions & 55 deletions src/DataMapper/Cache/Hooks.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace Owl\DataMapper\Cache;

/**
Expand Down Expand Up @@ -45,14 +44,14 @@ trait Hooks
*
* @return array|false
*/
abstract protected function getCache($id);
abstract protected function getCache(array $id);

/**
* @param mixed $id
*
* @return bool
*/
abstract protected function deleteCache($id);
abstract protected function deleteCache(array $id);

/**
* @param mixed $id
Expand All @@ -61,7 +60,7 @@ abstract protected function deleteCache($id);
*
* @return bool
*/
abstract protected function saveCache($id, array $record, $ttl = null);
abstract protected function saveCache(array $id, array $record, $ttl = null);

/**
* create cache after save new data, if cache policy set.
Expand All @@ -71,12 +70,11 @@ abstract protected function saveCache($id, array $record, $ttl = null);
protected function __afterInsert(\Owl\DataMapper\Data $data)
{
$policy = $this->getCachePolicy();

$id = $data->id();
$id = $data->id(true);

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 @@ -94,15 +92,15 @@ protected function __afterInsert(\Owl\DataMapper\Data $data)
protected function __afterUpdate(\Owl\DataMapper\Data $data)
{
$policy = $this->getCachePolicy();
$id = $data->id(true);

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

$this->saveCache($id, $record);
} else {
$this->deleteCache($data->id());
$this->deleteCache($id);
}

parent::__afterUpdate($data);
Expand All @@ -115,7 +113,7 @@ protected function __afterUpdate(\Owl\DataMapper\Data $data)
*/
protected function __afterDelete(\Owl\DataMapper\Data $data)
{
$this->deleteCache($data->id());
$this->deleteCache($data->id(true));

parent::__afterDelete($data);
}
Expand All @@ -129,7 +127,7 @@ protected function __afterDelete(\Owl\DataMapper\Data $data)
*/
public function refresh(\Owl\DataMapper\Data $data)
{
$this->deleteCache($data->id());
$this->deleteCache($data->id(true));

return parent::refresh($data);
}
Expand All @@ -142,8 +140,8 @@ public function refresh(\Owl\DataMapper\Data $data)
protected function getCachePolicy()
{
$defaults = [
'insert' => false,
'update' => false,
'insert' => false,
'update' => false,
'not_found' => false,
];

Expand All @@ -155,32 +153,32 @@ protected function getCachePolicy()

if (is_array($policy)) {
return array_merge($defaults, $policy);
} else {
if (DEBUG) {
throw new \Exception('Invalid cache policy setting');
}
}

return $defaults;
if (DEBUG) {
throw new \Exception('Invalid cache policy setting');
}

return $defaults;
}

/**
* return record from cache if cache is created, or save data into cache.
*
* @param mixed $id
* @param array $id
* @param \Owl\Service $service
* @param string $collection
*
* @return array
*/
protected function doFind($id, \Owl\Service $service = null, $collection = null)
protected function doFind(array $id, \Owl\Service $service = null, $collection = null)
{
if ($record = $this->getCache($id)) {
return isset($record['__IS_NOT_FOUND__']) ? false : $record;
}

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 @@ -194,25 +192,6 @@ protected function doFind($id, \Owl\Service $service = null, $collection = null)
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 All @@ -226,35 +205,31 @@ protected function getCacheService($key)
}

/**
* @param mixed $id
* @param array $id
*
* @return string
*/
protected function getCacheKey($id)
protected function getCacheKey(array $id)
{
$prefix = $this->hasOption('cache_key')
? $this->getOption('cache_key')
: sprintf('entity:%s', str_replace('\\', ':', trim($this->class, '\\')));

$prefix = $prefix.'@';
$prefix = $prefix . '@';

if ($this->hasOption('cache_key_prefix')) {
$prefix = $this->getOption('cache_key_prefix').':'.$prefix;
$prefix = $this->getOption('cache_key_prefix') . ':' . $prefix;
}

if (is_array($id)) {
ksort($id);

$kv = [];
foreach ($id as $k => $v) {
$kv[] = sprintf('%s:%s', $k, $v);
}
ksort($id);

$key = $prefix.implode(':', $kv);
} else {
$key = $prefix.$id;
$kv = [];
foreach ($id as $k => $v) {
$kv[] = sprintf('%s:%s', $k, $v);
}

$key = $prefix . implode(':', $kv);

return strtolower($key);
}

Expand All @@ -265,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;
}
}
15 changes: 7 additions & 8 deletions src/DataMapper/Cache/Memcached.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php

namespace Owl\DataMapper\Cache;

trait Memcached
{
use Hooks;

protected function getCache($id)
protected function getCache(array $id)
{
$key = $this->getCacheKey($id);
$key = $this->getCacheKey($id);
$memcached = $this->getCacheService($key);

try {
Expand All @@ -26,19 +25,19 @@ protected function getCache($id)
}
}

protected function deleteCache($id)
protected function deleteCache(array $id)
{
$key = $this->getCacheKey($id);
$key = $this->getCacheKey($id);
$memcached = $this->getCacheService($key);

return $memcached->delete($key);
}

protected function saveCache($id, array $record, $ttl = null)
protected function saveCache(array $id, array $record, $ttl = null)
{
$key = $this->getCacheKey($id);
$key = $this->getCacheKey($id);
$memcached = $this->getCacheService($key);
$ttl = $ttl ?: $this->getCacheTTL();
$ttl = $ttl ?: $this->getCacheTTL();

return $memcached->set($key, \Owl\safe_json_encode($record, JSON_UNESCAPED_UNICODE), $ttl);
}
Expand Down
15 changes: 7 additions & 8 deletions src/DataMapper/Cache/Redis.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php

namespace Owl\DataMapper\Cache;

trait Redis
{
use Hooks;

protected function getCache($id)
protected function getCache(array $id)
{
$key = $this->getCacheKey($id);
$key = $this->getCacheKey($id);
$redis = $this->getCacheService($key);

try {
Expand All @@ -26,19 +25,19 @@ protected function getCache($id)
}
}

protected function deleteCache($id)
protected function deleteCache(array $id)
{
$key = $this->getCacheKey($id);
$key = $this->getCacheKey($id);
$redis = $this->getCacheService($key);

return $redis->delete($key);
}

protected function saveCache($id, array $record, $ttl = null)
protected function saveCache(array $id, array $record, $ttl = null)
{
$key = $this->getCacheKey($id);
$key = $this->getCacheKey($id);
$redis = $this->getCacheService($key);
$ttl = $ttl ?: $this->getCacheTTL();
$ttl = $ttl ?: $this->getCacheTTL();

return $redis->setex($key, $ttl, \Owl\safe_json_encode($record, JSON_UNESCAPED_UNICODE));
}
Expand Down
Loading

0 comments on commit 7690d48

Please sign in to comment.