From 0655b91582f60eb77a96c17d875f962012698552 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Tue, 22 Dec 2015 18:22:34 +0900 Subject: [PATCH 1/4] file cache for ProdModule when app is unavailable --- src/Context/ProdModule.php | 28 ++++++++++++++++ .../Provider/FilesystemCacheProvider.php | 32 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/Context/Provider/FilesystemCacheProvider.php diff --git a/src/Context/ProdModule.php b/src/Context/ProdModule.php index 834eba9d..67b1e883 100644 --- a/src/Context/ProdModule.php +++ b/src/Context/ProdModule.php @@ -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; @@ -23,6 +27,17 @@ class ProdModule extends AbstractModule * {@inheritdoc} */ protected function configure() + { + if (function_exists('apc_fetch')) { + $this->installApcCache(); + + return; + } + $this->installFileCache(); + + } + + private function installApcCache() { $this->bind(Cache::class)->to(ApcCache::class)->in(Scope::SINGLETON); $this->bind(Reader::class)->toConstructor( @@ -34,4 +49,17 @@ protected function configure() $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); } + + 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)->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); + } } diff --git a/src/Context/Provider/FilesystemCacheProvider.php b/src/Context/Provider/FilesystemCacheProvider.php new file mode 100644 index 00000000..16696e53 --- /dev/null +++ b/src/Context/Provider/FilesystemCacheProvider.php @@ -0,0 +1,32 @@ +app = $app; + } + + /** + * @inheritDoc + */ + public function get() + { + return new FilesystemCache($this->app->tmpDir . '/cache'); + } +} From 2439cd3b5c91ff4e841e968007d8f93942a3b5c9 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Tue, 22 Dec 2015 18:30:32 +0900 Subject: [PATCH 2/4] apcu support --- src/Context/ProdModule.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Context/ProdModule.php b/src/Context/ProdModule.php index 67b1e883..d5048835 100644 --- a/src/Context/ProdModule.php +++ b/src/Context/ProdModule.php @@ -28,8 +28,13 @@ class ProdModule extends AbstractModule */ protected function configure() { + if (function_exists('apcu_fetch')) { + $this->installApcCache(ApcuCache::class); + + return; + } if (function_exists('apc_fetch')) { - $this->installApcCache(); + $this->installApcCache(ApcCache::class); return; } @@ -37,17 +42,17 @@ protected function configure() } - private function installApcCache() + private function installApcCache($apcClass) { - $this->bind(Cache::class)->to(ApcCache::class)->in(Scope::SINGLETON); + $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(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)->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() From 5630650c1bdc40586daa5c94147e760f4ffa8e8b Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 24 Dec 2015 10:02:31 +0900 Subject: [PATCH 3/4] check ApcuCache availability --- src/Context/ProdModule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Context/ProdModule.php b/src/Context/ProdModule.php index d5048835..9e62b6ce 100644 --- a/src/Context/ProdModule.php +++ b/src/Context/ProdModule.php @@ -28,7 +28,7 @@ class ProdModule extends AbstractModule */ protected function configure() { - if (function_exists('apcu_fetch')) { + if (function_exists('apcu_fetch') && class_exists(ApcuCache::class)) { $this->installApcCache(ApcuCache::class); return; From cae99ab01836bbdfbb997776d13a79aa46354c78 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 24 Dec 2015 10:26:11 +0900 Subject: [PATCH 4/4] fix test failure add AppMetaModule for ProdModule --- tests/Context/ProdModuleTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Context/ProdModuleTest.php b/tests/Context/ProdModuleTest.php index dbe02f9e..363a6e74 100644 --- a/tests/Context/ProdModuleTest.php +++ b/tests/Context/ProdModuleTest.php @@ -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; @@ -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); } }