Skip to content

Commit

Permalink
Merge pull request #22 from DerManoMann/psr-logger
Browse files Browse the repository at this point in the history
Add logger support. Fixes #1
  • Loading branch information
DerManoMann committed Oct 23, 2015
2 parents 61bdcb1 + e5c0e11 commit 9ea260f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 19 deletions.
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,6 @@ All issues that break backwards compatibility are flagged [BC].
### v1.2.0
* [BC] Refactor Apc GC into separat class [#18]

### v1.2.1
* Add log support to MultiLevelCache [#1]
* Streamline multi level cache stack validation
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
"source": "https://github.com/DerManoMann/acache.git"
},
"require": {
"php": ">=5.3.0"
"php": ">=5.3.0",
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.5",
"satooshi/php-coveralls": "dev-master",
"doctrine/cache": ">=1.3,<1.4-dev"
"doctrine/cache": "^1.3"
},
"suggest": {
"doctrine/cache": ">=1.3,<1.4-dev"
"doctrine/cache": "~1.3"
},
"autoload": {
"psr-4": {
Expand Down
30 changes: 22 additions & 8 deletions src/Radebatz/ACache/MultiLevelCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Radebatz\ACache;

use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

/**
* Multi level cache.
Expand All @@ -26,26 +28,38 @@ class MultiLevelCache implements CacheInterface
{
protected $stack;
protected $bubbleOnFetch;
protected $logger;

/**
* Create new instance for the given cache instances.
*
* @param array $stack List of <code>CacheInterface</code> instances.
* @param boolean $bubbleOnFetch Optional flag to restore cache entries further up the stack if an item was only found further down.
* @param LoggerInterface $logger Optional logger.
*/
public function __construct(array $stack = array(), $bubbleOnFetch = false)
public function __construct(array $stack = array(), $bubbleOnFetch = false, LoggerInterface $logger = null)
{
if (!$stack) {
throw new InvalidArgumentException('Need at least one cache in the stack');
}
$logger = $this->logger = $logger ?: new NullLogger();

foreach ($stack as $cache) {
$this->stack = array_filter($stack, function ($cache) use ($logger) {
if (!($cache instanceof CacheInterface)) {
throw new InvalidArgumentException('All stack elements must implement the Cache interface');
$this->logger->warning(sprintf('Invalid cache - removing from stack: %s', is_object($cache) ? get_class($cache) : 'non_an_object'));

return false;
}
if (!$cache->available()) {
$logger->warning(sprintf('Cache not available - removing from stack: %s', get_class($cache)));

return false;
}

return true;
});

if (!$this->stack) {
$this->logger->warning('Empty stack');
}

$this->stack = array_filter($stack, function ($cache) { return $cache->available(); });
$this->bubbleOnFetch = $bubbleOnFetch;
}

Expand Down Expand Up @@ -96,7 +110,7 @@ public function fetch($id, $namespace = null)
}
}

return;
return null;
}

/**
Expand Down
20 changes: 12 additions & 8 deletions tests/Radebatz/ACache/Tests/MultiLevelCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ class MultiLevelCacheTest extends CacheTest
public function cacheProvider()
{
return array(
array(new MultiLevelCache(array(new ArrayCache()), false)),
array(new MultiLevelCache(array(new ArrayCache(), new ArrayCache()), false)),
array(new MultiLevelCache(array(new ArrayCache()), true)),
array(new MultiLevelCache(array(new ArrayCache(), new ArrayCache()), true)),
array(new MultiLevelCache(array(new ArrayCache()), false, new TestLogger())),
array(new MultiLevelCache(array(new ArrayCache(), new ArrayCache()), false, new TestLogger())),
array(new MultiLevelCache(array(new ArrayCache()), true, new TestLogger())),
array(new MultiLevelCache(array(new ArrayCache(), new ArrayCache()), true, new TestLogger())),
array(new MultiLevelCache(array(new ArrayCache()), true, new TestLogger())),
array(new MultiLevelCache(array(new ArrayCache(), new ArrayCache()), false, new TestLogger())),
);
}

Expand All @@ -41,7 +43,7 @@ public function cacheProvider()
*/
public function testDefaults()
{
$cache = new MultiLevelCache(array(new ArrayCache(), new ArrayCache()));
$cache = new MultiLevelCache(array(new ArrayCache(), new ArrayCache()), false, new TestLogger());
$this->assertTrue($cache->available());

$this->assertFalse($cache->contains('yin'));
Expand All @@ -65,7 +67,7 @@ public function testDefaults()
public function testNoBubbles()
{
// no bubbles :{
$cache = new MultiLevelCache(array(new ArrayCache(), new ArrayCache()), false);
$cache = new MultiLevelCache(array(new ArrayCache(), new ArrayCache()), false, new TestLogger());
$this->assertTrue($cache->available());

$this->assertFalse($cache->isBubbleOnFetch());
Expand Down Expand Up @@ -97,7 +99,7 @@ public function testNoBubbles()
public function testBubbles()
{
// bubbles :}
$cache = new MultiLevelCache(array(new ArrayCache(), new ArrayCache()), true);
$cache = new MultiLevelCache(array(new ArrayCache(), new ArrayCache()), true, new TestLogger());
$this->assertTrue($cache->available());

$this->assertTrue($cache->isBubbleOnFetch());
Expand Down Expand Up @@ -128,7 +130,9 @@ public function testBubbles()
*/
public function testUnavailable()
{
$cache = new MultiLevelCache(array(new NullCache(false)));
$cache = new MultiLevelCache(array(new NullCache(false)), false, $logger = new TestLogger());
$this->assertFalse($cache->available());
$this->assertEquals(2, count($logger->lines));
$this->assertContains('not available', $logger->lines[0]);
}
}
35 changes: 35 additions & 0 deletions tests/Radebatz/ACache/Tests/TestLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the ACache library.
*
* (c) Martin Rademacher <mano@radebatz.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Radebatz\ACache\Tests;

use Psr\Log\AbstractLogger;

/**
* Test logger.
*/
class TestLogger extends AbstractLogger
{
public $lines = array();

/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return null
*/
public function log($level, $message, array $context = array())
{
$this->lines[] = $message;
}
}

0 comments on commit 9ea260f

Please sign in to comment.