Skip to content

Commit

Permalink
Merge pull request #211 from koriym/cache
Browse files Browse the repository at this point in the history
file cache and apcu cache support for ProdModule
  • Loading branch information
koriym committed Dec 24, 2015
2 parents 94ddedb + cae99ab commit 65123ca
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
41 changes: 37 additions & 4 deletions src/Context/ProdModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
*/
namespace BEAR\Package\Context;

use BEAR\Package\Context\Provider\FilesystemCacheProvider;
use BEAR\RepositoryModule\Annotation\Storage;
use BEAR\Resource\Annotation\LogicCache;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\ApcuCache;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\Cache\FileCache;
use Doctrine\Common\Cache\FilesystemCache;
use Ray\Di\AbstractModule;
use Ray\Di\Scope;

Expand All @@ -24,14 +28,43 @@ class ProdModule extends AbstractModule
*/
protected function configure()
{
$this->bind(Cache::class)->to(ApcCache::class)->in(Scope::SINGLETON);
if (function_exists('apcu_fetch') && class_exists(ApcuCache::class)) {
$this->installApcCache(ApcuCache::class);

return;
}
if (function_exists('apc_fetch')) {
$this->installApcCache(ApcCache::class);

return;
}
$this->installFileCache();

}

private function installApcCache($apcClass)
{
$this->bind(Cache::class)->to($apcClass)->in(Scope::SINGLETON);
$this->bind(Reader::class)->toConstructor(
CachedReader::class,
'reader=annotation_reader'
);
$this->bind(Reader::class)->annotatedWith('annotation_reader')->to(AnnotationReader::class);
$this->bind(CacheProvider::class)->annotatedWith(Storage::class)->to($apcClass)->in(Scope::SINGLETON);
$this->bind(Cache::class)->annotatedWith(LogicCache::class)->to($apcClass)->in(Scope::SINGLETON);
$this->bind(Cache::class)->annotatedWith(Storage::class)->to($apcClass)->in(Scope::SINGLETON);
}

private function installFileCache()
{
$this->bind(Cache::class)->toProvider(FilesystemCacheProvider::class)->in(Scope::SINGLETON);
$this->bind(Reader::class)->toConstructor(
CachedReader::class,
'reader=annotation_reader'
);
$this->bind(Reader::class)->annotatedWith('annotation_reader')->to(AnnotationReader::class);
$this->bind(CacheProvider::class)->annotatedWith(Storage::class)->to(ApcCache::class)->in(Scope::SINGLETON);
$this->bind(Cache::class)->annotatedWith(LogicCache::class)->to(ApcCache::class)->in(Scope::SINGLETON);
$this->bind(Cache::class)->annotatedWith(Storage::class)->to(ApcCache::class)->in(Scope::SINGLETON);
$this->bind(CacheProvider::class)->annotatedWith(Storage::class)->toProvider(FilesystemCacheProvider::class)->in(Scope::SINGLETON);
$this->bind(Cache::class)->annotatedWith(LogicCache::class)->toProvider(FilesystemCacheProvider::class)->in(Scope::SINGLETON);
$this->bind(Cache::class)->annotatedWith(Storage::class)->toProvider(FilesystemCacheProvider::class)->in(Scope::SINGLETON);
}
}
32 changes: 32 additions & 0 deletions src/Context/Provider/FilesystemCacheProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* This file is part of the BEAR.Package package.
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace BEAR\Package\Context\Provider;

use BEAR\AppMeta\AbstractAppMeta;
use Doctrine\Common\Cache\FilesystemCache;
use Ray\Di\ProviderInterface;

class FilesystemCacheProvider implements ProviderInterface
{
/**
* @var AbstractAppMeta
*/
private $app;

public function __construct(AbstractAppMeta $app)
{
$this->app = $app;
}

/**
* @inheritDoc
*/
public function get()
{
return new FilesystemCache($this->app->tmpDir . '/cache');
}
}
4 changes: 3 additions & 1 deletion tests/Context/ProdModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace BEAR\Package\Context;

use BEAR\AppMeta\AppMeta;
use BEAR\Package\AppMetaModule;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\Reader;
use Ray\Di\Injector;
Expand All @@ -10,7 +12,7 @@ class ProdModuleTest extends \PHPUnit_Framework_TestCase
{
public function testModule()
{
$reader = (new Injector(new ProdModule))->getInstance(Reader::class);
$reader = (new Injector(new ProdModule(new AppMetaModule(new AppMeta('FakeVendor\HelloWorld')))))->getInstance(Reader::class);
$this->assertInstanceOf(CachedReader::class, $reader);
}
}

0 comments on commit 65123ca

Please sign in to comment.