From 5590d6bc4e328b21efe51551bd4ed8a1c8d58ca7 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Thu, 28 Dec 2017 10:00:18 -0800 Subject: [PATCH 1/5] Install nicer phpunit output --- composer.json | 1 + phpunit.xml | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index a11f451..d1e77e5 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "illuminate/database": "5.5.*" }, "require-dev": { + "codedungeon/phpunit-result-printer": "^0.4.4", "fzaninotto/faker": "~1.4", "laravel/laravel": "5.5.*", "mockery/mockery": "0.9.*", diff --git a/phpunit.xml b/phpunit.xml index c3ae6e6..2be4735 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,14 +1,16 @@ - From c5b32405bf4bf63e370bcdea69da22adb76e2474 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Thu, 28 Dec 2017 10:01:03 -0800 Subject: [PATCH 2/5] Refactor out all caching functionality to trait --- src/CachedBuilder.php | 21 --------------------- src/CachedModel.php | 27 +++------------------------ src/Traits/Cachable.php | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 45 deletions(-) diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index a2fbeaa..a541895 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -10,8 +10,6 @@ class CachedBuilder extends EloquentBuilder { use Cachable; - protected $isCachable = true; - public function avg($column) { if (! $this->isCachable) { @@ -56,13 +54,6 @@ public function delete() return parent::delete(); } - public function disableCache() - { - $this->isCachable = false; - - return $this; - } - /** * @SuppressWarnings(PHPMD.ShortVariable) */ @@ -155,16 +146,4 @@ public function sum($column) return parent::sum($column); }); } - - protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string - { - return (new CacheKey($this->eagerLoad, $this->model, $this->query)) - ->make($columns, $idColumn); - } - - protected function makeCacheTags() : array - { - return (new CacheTags($this->eagerLoad, $this->model)) - ->make(); - } } diff --git a/src/CachedModel.php b/src/CachedModel.php index c911d7f..790c45b 100644 --- a/src/CachedModel.php +++ b/src/CachedModel.php @@ -1,6 +1,7 @@ getStore(), TaggableStore::class)) { - array_push($tags, str_slug(get_called_class())); - $cache = $cache->tags($tags); - } - - return $cache; - } - - public function disableCache() : self - { - session(['genealabs-laravel-model-caching-is-disabled' => true]); - - return $this; - } - - public function flushCache(array $tags = []) - { - $this->cache($tags)->flush(); - } - public static function all($columns = ['*']) { $class = get_called_class(); diff --git a/src/Traits/Cachable.php b/src/Traits/Cachable.php index 6f34d30..e69bfca 100644 --- a/src/Traits/Cachable.php +++ b/src/Traits/Cachable.php @@ -1,17 +1,51 @@ getStore(), TaggableStore::class)) { + if (is_a($this, CachedModel::class)) { + array_push($tags, str_slug(get_called_class())); + } + $cache = $cache->tags($tags); } return $cache; } + + public function disableCache() + { + session(['genealabs-laravel-model-caching-is-disabled' => true]); + $this->isCachable = false; + + return $this; + } + + public function flushCache(array $tags = []) + { + $this->cache($tags)->flush(); + } + + protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string + { + return (new CacheKey($this->eagerLoad, $this->model, $this->query)) + ->make($columns, $idColumn); + } + + protected function makeCacheTags() : array + { + return (new CacheTags($this->eagerLoad, $this->model)) + ->make(); + } } From f9ce04ebb7eb3e0b53e380f2fe627c8b605981fe Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Thu, 28 Dec 2017 13:05:20 -0800 Subject: [PATCH 3/5] Update travis config and code coverage scripts --- .travis.yml | 10 +++++----- composer.json | 13 ++++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0654f0d..90c60f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,15 +3,15 @@ language: php php: - 7.0 - 7.1 + - 7.2 before_script: - travis_retry composer self-update - travis_retry composer install --no-interaction --prefer-source --dev script: - - ./vendor/bin/phpunit --coverage-text --coverage-clover ./build/logs/clover.xml + - mkdir -p ./build/logs + - ./vendor/bin/phpunit --coverage-text --coverage-clover ./build/logs/clover.xml -after_script: - - php vendor/bin/coveralls - - wget https://scrutinizer-ci.com/ocular.phar - - php ocular.phar code-coverage:upload --format=php-clover ./build/logs/clover.xml +after_success: + - travis_retry php vendor/bin/php-coveralls -v diff --git a/composer.json b/composer.json index d1e77e5..7a49316 100644 --- a/composer.json +++ b/composer.json @@ -9,9 +9,9 @@ } ], "require": { - "php": ">=7.0.0", "illuminate/cache": "5.5.*", - "illuminate/database": "5.5.*" + "illuminate/database": "5.5.*", + "php": ">=7.0.0" }, "require-dev": { "codedungeon/phpunit-result-printer": "^0.4.4", @@ -20,7 +20,7 @@ "mockery/mockery": "0.9.*", "phpmd/phpmd": "^2.6", "phpunit/phpunit": "5.7.*", - "satooshi/php-coveralls" : "*", + "php-coveralls/php-coveralls" : "*", "sebastian/phpcpd": "*" }, "autoload": { @@ -35,5 +35,12 @@ "psr-4": { "GeneaLabs\\LaravelModelCaching\\Tests\\": "tests/" } + }, + "extra": { + "laravel": { + "providers": [ + "GeneaLabs\\LaravelModelCaching\\Providers\\Service" + ] + } } } From e3d1fcbe94264757c504f02a163163ad21eea118 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Thu, 28 Dec 2017 13:06:35 -0800 Subject: [PATCH 4/5] Add custom cache store feature --- config/laravel-model-caching.php | 5 +++ src/Providers/Service.php | 14 ++++++ src/Traits/Cachable.php | 4 ++ tests/CreatesApplication.php | 2 + tests/Unit/CachedModelTest.php | 3 -- tests/Unit/Traits/CachableTest.php | 72 ++++++++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 config/laravel-model-caching.php create mode 100644 src/Providers/Service.php create mode 100644 tests/Unit/Traits/CachableTest.php diff --git a/config/laravel-model-caching.php b/config/laravel-model-caching.php new file mode 100644 index 0000000..cc75105 --- /dev/null +++ b/config/laravel-model-caching.php @@ -0,0 +1,5 @@ + env('MODEL_CACHE_STORE'), +]; diff --git a/src/Providers/Service.php b/src/Providers/Service.php new file mode 100644 index 0000000..64afdbb --- /dev/null +++ b/src/Providers/Service.php @@ -0,0 +1,14 @@ +mergeConfigFrom($configPath, 'laravel-model-caching'); + } +} diff --git a/src/Traits/Cachable.php b/src/Traits/Cachable.php index e69bfca..08d4119 100644 --- a/src/Traits/Cachable.php +++ b/src/Traits/Cachable.php @@ -13,6 +13,10 @@ protected function cache(array $tags = []) { $cache = cache(); + if (config('laravel-model-caching.store')) { + $cache = $cache->store(config('laravel-model-caching.store')); + } + if (is_subclass_of($cache->getStore(), TaggableStore::class)) { if (is_a($this, CachedModel::class)) { array_push($tags, str_slug(get_called_class())); diff --git a/tests/CreatesApplication.php b/tests/CreatesApplication.php index 20300b6..9c275d3 100644 --- a/tests/CreatesApplication.php +++ b/tests/CreatesApplication.php @@ -1,5 +1,6 @@ afterResolving('migrator', function ($migrator) { $migrator->path(__DIR__ . '/database/migrations'); }); + $app->register(LaravelModelCachingService::class); return $app; } diff --git a/tests/Unit/CachedModelTest.php b/tests/Unit/CachedModelTest.php index db34ec4..eb48379 100644 --- a/tests/Unit/CachedModelTest.php +++ b/tests/Unit/CachedModelTest.php @@ -62,9 +62,6 @@ public function testAllModelResultsCreatesCache() $this->assertEmpty($liveResults->diffAssoc($cachedResults)); } - /** - * @group test - **/ public function testScopeDisablesCaching() { $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; diff --git a/tests/Unit/Traits/CachableTest.php b/tests/Unit/Traits/CachableTest.php new file mode 100644 index 0000000..f1218b7 --- /dev/null +++ b/tests/Unit/Traits/CachableTest.php @@ -0,0 +1,72 @@ +flush(); + $publishers = factory(Publisher::class, 10)->create(); + factory(Author::class, 10)->create() + ->each(function ($author) use ($publishers) { + factory(Book::class, random_int(2, 10))->make() + ->each(function ($book) use ($author, $publishers) { + $book->author()->associate($author); + $book->publisher()->associate($publishers[rand(0, 9)]); + $book->save(); + }); + factory(Profile::class)->make([ + 'author_id' => $author->id, + ]); + }); + + $bookIds = (new Book)->all()->pluck('id'); + factory(Store::class, 10)->create() + ->each(function ($store) use ($bookIds) { + $store->books()->sync(rand($bookIds->min(), $bookIds->max())); + }); + cache()->flush(); + } + + public function testSpecifyingAlternateCacheDriver() + { + $configCacheStores = config('cache.stores'); + $configCacheStores['customCache'] = ['driver' => 'array']; + config(['cache.stores' => $configCacheStores]); + config(['laravel-model-caching.store' => 'customCache']); + $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; + $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; + + $authors = (new Author) + ->all(); + $defaultcacheResults = cache() + ->tags($tags) + ->get($key); + $customCacheResults = cache() + ->store('customCache') + ->tags($tags) + ->get($key); + $liveResults = (new UncachedAuthor) + ->all(); + + $this->assertEquals($customCacheResults, $authors); + $this->assertNull($defaultcacheResults); + $this->assertEmpty($liveResults->diffAssoc($customCacheResults)); + } +} From cb55d37299b0a46028af85da9272e2fb943c91eb Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Thu, 28 Dec 2017 13:07:59 -0800 Subject: [PATCH 5/5] Update documentation --- CHANGELOG.md | 4 ++++ README.md | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b157c2..4867625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [0.2.13] - 28 Dec 2017 +### Added +- ability to define custom cache store in `.env` file. + ## [0.2.12] - 14 Dec 2017 ### Added - chainable method to disable caching of queries. diff --git a/README.md b/README.md index 60b0755..698235c 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,22 @@ relationships. This package is the attempt to address those requirements. - PHP >= 7.0.0 - Laravel 5.5 +## Installation +``` +composer require genealabs/laravel-model-caching +``` + +## Configuration +### Optional Custom Cache Store +If you would like to use a different cache store than the default one used by +your Laravel application, you may do so by setting the `MODEL_CACHE_STORE` +environment variable in your `.env` file to the name of a cache store configured +in `config/cache.php` (you can define any custom cache store base on your +specific needs there). For example: +``` +MODEL_CACHE_STORE=redis +``` + ## Usage For best performance a taggable cache provider is recommended (redis, memcached). While this is optional, using a non-taggable cache provider will @@ -49,7 +65,7 @@ abstract class BaseModel extends CachedModel } ``` -### Disabling Caching of Queries +### Optional Disabling Caching of Queries **Recommendation: add this to all your seeder queries to avoid pulling in cacched information when reseeding multiple times.** You can disable a given query by using `disableCache()` in the query chain, and