10000 [Cache] Handle and log errors properly by nicolas-grekas · Pull Request #17530 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Handle and log errors properly #17530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 82 additions & 20 deletions src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
< 10000 td class="blob-num blob-num-deletion empty-cell">
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@

use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
abstract class AbstractAdapter implements CacheItemPoolInterface
abstract class AbstractAdapter implements CacheItemPoolInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

private $namespace;
private $deferred = array();
private $createCacheItem;
Expand Down Expand Up @@ -119,8 +123,12 @@ public function getItem($key)
$isHit = false;
$value = null;

foreach ($this->doFetch(array($id)) as $value) {
$isHit = true;
try {
foreach ($this->doFetch(array($id)) as $value) {
$isHit = true;
}
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to fetch key "{key}"', array('key' => $key, 'exception' => $e));
}

return $f($key, $value, $isHit);
Expand All @@ -139,7 +147,12 @@ public function getItems(array $keys = array())
foreach ($keys as $key) {
$ids[$key] = $this->getId($key);
}
$items = $this->doFetch($ids);
try {
$items = $this->doFetch($ids);
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to fetch requested items', array('keys' => $keys, 'exception' => $e));
$items = array();
}
$ids = array_flip($ids);

return $this->generateItems($items, $ids);
Expand All @@ -154,15 +167,28 @@ public function hasItem($key)

if (isset($this->deferred[$key])) {
$item = (array) $this->deferred[$key];
$ok = $this->doSave(array($item[CacheItem::CAST_PREFIX.'key'] => $item[CacheItem::CAST_PREFIX.'value']), $item[CacheItem::CAST_PREFIX.'lifetime']);
unset($this->deferred[$key]);

if (true === $ok || array() === $ok) {
return true;
try {
$e = null;
$value = $item[CacheItem::CAST_PREFIX.'value'];
$ok = $this->doSave(array($key => $value), $item[CacheItem::CAST_PREFIX.'lifetime']);
unset($this->deferred[$key]);

if (true === $ok || array() === $ok) {
return true;
}
} catch (\Exception $e) {
}
$type = is_object($value) ? get_class($value) : gettype($value);
CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e));
}

return $this->doHave($id);
try {
return $this->doHave($id);
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to check if key "{key}" is cached', array('key' => $key, 'exception' => $e));

return false;
}
}

/**
Expand All @@ -172,7 +198,13 @@ public function clear()
{
$this->deferred = array();

return $this->doClear($this->namespace);
try {
return $this->doClear($this->namespace);
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to clear the cache', array('exception' => $e));

return false;
}
}

/**
Expand All @@ -195,7 +227,29 @@ public function deleteItems(array $keys)
unset($this->deferred[$key]);
}

return $this->doDelete($ids);
try {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning false here wasn't psr-6 compliant

if ($this->doDelete($ids)) {
return true;
}
} catch (\Exception $e) {
}

$ok = true;

// When bulk-save failed, retry each item individually
foreach ($ids as $key => $id) {
try {
$e = null;
if ($this->doDelete(array($id))) {
continue;
}
} catch (\Exception $e) {
}
CacheItem::log($this->logger, 'Failed to delete key "{key}"', array('key' => $key, 'exception' => $e));
$ok = false;
}

return $ok;
}

/**
Expand Down Expand Up @@ -225,9 +279,9 @@ public function saveDeferred(CacheItemInterface $item)
try {
$item = clone $item;
} catch (\Exception $e) {
@trigger_error($e->__toString());

return false;
$value = $item->get();
$type = is_object($value) ? get_class($value) : gettype($value);
CacheItem::log($this->logger, 'Failed to clone key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e));
}
$this->deferred[$item->getKey()] = $item;

Expand All @@ -243,8 +297,12 @@ public function commit()
$ko = array();

foreach ($f($this->deferred, $this->namespace) as $lifetime => $values) {
if (true === $ok = $this->doSave($values, $lifetime)) {
continue;
try {
if (true === $ok = $this->doSave($values, $lifetime)) {
8000 continue;
}
} catch (\Exception $e) {
$ok = false;
}
if (false === $ok) {
$ok = array_keys($values);
Expand All @@ -260,11 +318,15 @@ public function commit()
// When bulk-save failed, retry each item individually
foreach ($ko as $lifetime => $values) {
foreach ($values as $v) {
if (!in_array($this->doSave($v, $lifetime), array(true, array()), true)) {
try {
$e = $this->doSave($v, $lifetime);
} catch (\Exception $e) {
}
if (true !== $e && array() !== $e) {
$ok = false;

foreach ($v as $key => $value) {
@trigger_error(sprintf('Failed to cache key "%s" of type "%s"', is_object($value) ? get_class($value) : gettype($value)));
$type = is_object($value) ? get_class($value) : gettype($value);
CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e instanceof \Exception ? $e : null));
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@

use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ArrayAdapter implements CacheItemPoolInterface
class ArrayAdapter implements CacheItemPoolInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

private $values = array();
private $expiries = array();
private $createCacheItem;
Expand Down Expand Up @@ -125,11 +129,9 @@ public function save(CacheItemInterface $item)
if (is_object($value)) {
try {
$value = clone $value;
} catch (\Error $e) {
} catch (\Exception $e) {
}
if (isset($e)) {
@trigger_error($e->__toString());
$type = is_object($value) ? get_class($value) : gettype($value);
CacheItem::log($this->logger, 'Failed to clone key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e));

return false;
}
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Cache/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Cache;

use Psr\Cache\CacheItemInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Cache\Exception\InvalidArgumentException;

/**
Expand Down Expand Up @@ -105,4 +106,24 @@ public function expiresAfter($time)

return $this;
}

/**
* Internal logging helper.
*
* @internal
*/
public function log(LoggerInterface $logger = null, $message, $context = array())
{
if ($logger) {
$logger->warning($message, $context);
} else {
$replace = array();
foreach ($context as $k => $v) {
if (is_scalar($v)) {
$replace['{'.$k.'}'] = $v;
}
}
@trigger_error(strtr($message, $replace), E_USER_WARNING);
}
}
}
6 changes: 5 additions & 1 deletion src/Symfony/Component/Cache/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
},
"require": {
"php": ">=5.5.9",
"psr/cache": "~1.0"
"psr/cache": "~1.0",
"psr/log": "~1.0"
},
"require-dev": {
"cache/integration-tests": "dev-master",
"doctrine/cache": "~1.6"
},
"suggest": {
"symfony/polyfill-apcu": "For using ApcuAdapter on HHVM"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Cache\\": "" },
"exclude-from-classmap": [
Expand Down
0