Skip to content

Commit a370197

Browse files
authored
Merge pull request #43 from GeneaLabs/laravel-5.5
Laravel 5.5
2 parents b0dccf8 + b09e1a6 commit a370197

13 files changed

+175
-59
lines changed

.travis.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ language: php
33
php:
44
- 7.0
55
- 7.1
6+
- 7.2
67

78
before_script:
89
- travis_retry composer self-update
910
- travis_retry composer install --no-interaction --prefer-source --dev
1011

1112
script:
12-
- ./vendor/bin/phpunit --coverage-text --coverage-clover ./build/logs/clover.xml
13+
- mkdir -p ./build/logs
14+
- ./vendor/bin/phpunit --coverage-text --coverage-clover ./build/logs/clover.xml
1315

14-
after_script:
15-
- php vendor/bin/coveralls
16-
- wget https://scrutinizer-ci.com/ocular.phar
17-
- php ocular.phar code-coverage:upload --format=php-clover ./build/logs/clover.xml
16+
after_success:
17+
- travis_retry php vendor/bin/php-coveralls -v
1818

1919
notifications:
2020
webhooks:

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.13] - 28 Dec 2017
8+
### Added
9+
- ability to define custom cache store in `.env` file.
10+
711
## [0.2.12] - 14 Dec 2017
812
### Added
913
- chainable method to disable caching of queries.

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ relationships. This package is the attempt to address those requirements.
2727
- PHP >= 7.0.0
2828
- Laravel 5.5
2929

30+
## Installation
31+
```
32+
composer require genealabs/laravel-model-caching
33+
```
34+
35+
## Configuration
36+
### Optional Custom Cache Store
37+
If you would like to use a different cache store than the default one used by
38+
your Laravel application, you may do so by setting the `MODEL_CACHE_STORE`
39+
environment variable in your `.env` file to the name of a cache store configured
40+
in `config/cache.php` (you can define any custom cache store base on your
41+
specific needs there). For example:
42+
```
43+
MODEL_CACHE_STORE=redis
44+
```
45+
3046
## Usage
3147
For best performance a taggable cache provider is recommended (redis,
3248
memcached). While this is optional, using a non-taggable cache provider will
@@ -56,7 +72,7 @@ extends `Illuminate\Foundation\Auth\User`. Overriding that would break functiona
5672
Not only that, but it probably isn't a good idea to cache the user model anyway,
5773
since you always want to pull the most up-to-date info on it.
5874

59-
### Disabling Caching of Queries
75+
### Optional Disabling Caching of Queries
6076
**Recommendation: add this to all your seeder queries to avoid pulling in
6177
cached information when reseeding multiple times.**
6278
You can disable a given query by using `disableCache()` in the query chain, and

composer.json

+11-3
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
}
1010
],
1111
"require": {
12-
"php": ">=7.0.0",
1312
"illuminate/cache": "5.5.*",
14-
"illuminate/database": "5.5.*"
13+
"illuminate/database": "5.5.*",
14+
"php": ">=7.0.0"
1515
},
1616
"require-dev": {
17+
"codedungeon/phpunit-result-printer": "^0.4.4",
1718
"fzaninotto/faker": "~1.4",
1819
"laravel/laravel": "5.5.*",
1920
"mockery/mockery": "0.9.*",
2021
"phpmd/phpmd": "^2.6",
2122
"phpunit/phpunit": "5.7.*",
22-
"satooshi/php-coveralls" : "*",
23+
"php-coveralls/php-coveralls" : "*",
2324
"sebastian/phpcpd": "*"
2425
},
2526
"autoload": {
@@ -34,5 +35,12 @@
3435
"psr-4": {
3536
"GeneaLabs\\LaravelModelCaching\\Tests\\": "tests/"
3637
}
38+
},
39+
"extra": {
40+
"laravel": {
41+
"providers": [
42+
"GeneaLabs\\LaravelModelCaching\\Providers\\Service"
43+
]
44+
}
3745
}
3846
}

config/laravel-model-caching.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'store' => env('MODEL_CACHE_STORE'),
5+
];

phpunit.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
2+
<phpunit
3+
backupGlobals="false"
34
backupStaticAttributes="false"
5+
bootstrap="vendor/autoload.php"
46
colors="true"
57
convertErrorsToExceptions="true"
68
convertNoticesToExceptions="true"
79
convertWarningsToExceptions="true"
10+
printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer"
811
processIsolation="false"
912
stopOnFailure="false"
1013
syntaxCheck="false"
11-
bootstrap="vendor/autoload.php"
1214
>
1315
<testsuites>
1416
<testsuite name="Feature">

src/CachedBuilder.php

-21
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ class CachedBuilder extends EloquentBuilder
1010
{
1111
use Cachable;
1212

13-
protected $isCachable = true;
14-
1513
public function avg($column)
1614
{
1715
if (! $this->isCachable) {
@@ -56,13 +54,6 @@ public function delete()
5654
return parent::delete();
5755
}
5856

59-
public function disableCache()
60-
{
61-
$this->isCachable = false;
62-
63-
return $this;
64-
}
65-
6657
/**
6758
* @SuppressWarnings(PHPMD.ShortVariable)
6859
*/
@@ -155,16 +146,4 @@ public function sum($column)
155146
return parent::sum($column);
156147
});
157148
}
158-
159-
protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
160-
{
161-
return (new CacheKey($this->eagerLoad, $this->model, $this->query))
162-
->make($columns, $idColumn);
163-
}
164-
165-
protected function makeCacheTags() : array
166-
{
167-
return (new CacheTags($this->eagerLoad, $this->model))
168-
->make();
169-
}
170149
}

src/CachedModel.php

+3-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace GeneaLabs\LaravelModelCaching;
22

33
use GeneaLabs\LaravelModelCaching\CachedBuilder as Builder;
4+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
45
use Illuminate\Cache\CacheManager;
56
use Illuminate\Cache\TaggableStore;
67
use Illuminate\Cache\TaggedCache;
@@ -12,6 +13,8 @@
1213

1314
abstract class CachedModel extends Model
1415
{
16+
use Cachable;
17+
1518
public function newEloquentBuilder($query)
1619
{
1720
if (session('genealabs-laravel-model-caching-is-disabled')) {
@@ -46,30 +49,6 @@ public static function boot()
4649
});
4750
}
4851

49-
public function cache(array $tags = [])
50-
{
51-
$cache = cache();
52-
53-
if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
54-
array_push($tags, str_slug(get_called_class()));
55-
$cache = $cache->tags($tags);
56-
}
57-
58-
return $cache;
59-
}
60-
61-
public function disableCache() : self
62-
{
63-
session(['genealabs-laravel-model-caching-is-disabled' => true]);
64-
65-
return $this;
66-
}
67-
68-
public function flushCache(array $tags = [])
69-
{
70-
$this->cache($tags)->flush();
71-
}
72-
7352
public static function all($columns = ['*'])
7453
{
7554
$class = get_called_class();

src/Providers/Service.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Providers;
2+
3+
use Illuminate\Support\ServiceProvider;
4+
5+
class Service extends ServiceProvider
6+
{
7+
protected $defer = false;
8+
9+
public function boot()
10+
{
11+
$configPath = __DIR__ . '/../../config/laravel-model-caching.php';
12+
$this->mergeConfigFrom($configPath, 'laravel-model-caching');
13+
}
14+
}

src/Traits/Cachable.php

+38
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,55 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
22

3+
use GeneaLabs\LaravelModelCaching\CacheKey;
4+
use GeneaLabs\LaravelModelCaching\CacheTags;
5+
use GeneaLabs\LaravelModelCaching\CachedModel;
36
use Illuminate\Cache\TaggableStore;
47

58
trait Cachable
69
{
10+
protected $isCachable = true;
11+
712
protected function cache(array $tags = [])
813
{
914
$cache = cache();
1015

16+
if (config('laravel-model-caching.store')) {
17+
$cache = $cache->store(config('laravel-model-caching.store'));
18+
}
19+
1120
if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
21+
if (is_a($this, CachedModel::class)) {
22+
array_push($tags, str_slug(get_called_class()));
23+
}
24+
1225
$cache = $cache->tags($tags);
1326
}
1427

1528
return $cache;
1629
}
30+
31+
public function disableCache()
32+
{
33+
session(['genealabs-laravel-model-caching-is-disabled' => true]);
34+
$this->isCachable = false;
35+
36+
return $this;
37+
}
38+
39+
public function flushCache(array $tags = [])
40+
{
41+
$this->cache($tags)->flush();
42+
}
43+
44+
protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
45+
{
46+
return (new CacheKey($this->eagerLoad, $this->model, $this->query))
47+
->make($columns, $idColumn);
48+
}
49+
50+
protected function makeCacheTags() : array
51+
{
52+
return (new CacheTags($this->eagerLoad, $this->model))
53+
->make();
54+
}
1755
}

tests/CreatesApplication.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests;
22

3+
use GeneaLabs\LaravelModelCaching\Providers\Service as LaravelModelCachingService;
34
use Illuminate\Contracts\Console\Kernel;
45
use Illuminate\Database\Eloquent\Factory;
56

@@ -13,6 +14,7 @@ public function createApplication()
1314
$app->afterResolving('migrator', function ($migrator) {
1415
$migrator->path(__DIR__ . '/database/migrations');
1516
});
17+
$app->register(LaravelModelCachingService::class);
1618

1719
return $app;
1820
}

tests/Unit/CachedModelTest.php

-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ public function testAllModelResultsCreatesCache()
6262
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
6363
}
6464

65-
/**
66-
* @group test
67-
**/
6865
public function testScopeDisablesCaching()
6966
{
7067
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';

tests/Unit/Traits/CachableTest.php

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Unit;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
8+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
9+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
10+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile;
11+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher;
12+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore;
13+
use GeneaLabs\LaravelModelCaching\Tests\TestCase;
14+
use Illuminate\Foundation\Testing\RefreshDatabase;
15+
16+
class CachableTest extends TestCase
17+
{
18+
use RefreshDatabase;
19+
20+
public function setUp()
21+
{
22+
parent::setUp();
23+
24+
cache()->flush();
25+
$publishers = factory(Publisher::class, 10)->create();
26+
factory(Author::class, 10)->create()
27+
->each(function ($author) use ($publishers) {
28+
factory(Book::class, random_int(2, 10))->make()
29+
->each(function ($book) use ($author, $publishers) {
30+
$book->author()->associate($author);
31+
$book->publisher()->associate($publishers[rand(0, 9)]);
32+
$book->save();
33+
});
34+
factory(Profile::class)->make([
35+
'author_id' => $author->id,
36+
]);
37+
});
38+
39+
$bookIds = (new Book)->all()->pluck('id');
40+
factory(Store::class, 10)->create()
41+
->each(function ($store) use ($bookIds) {
42+
$store->books()->sync(rand($bookIds->min(), $bookIds->max()));
43+
});
44+
cache()->flush();
45+
}
46+
47+
public function testSpecifyingAlternateCacheDriver()
48+
{
49+
$configCacheStores = config('cache.stores');
50+
$configCacheStores['customCache'] = ['driver' => 'array'];
51+
config(['cache.stores' => $configCacheStores]);
52+
config(['laravel-model-caching.store' => 'customCache']);
53+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
54+
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
55+
56+
$authors = (new Author)
57+
->all();
58+
$defaultcacheResults = cache()
59+
->tags($tags)
60+
->get($key);
61+
$customCacheResults = cache()
62+
->store('customCache')
63+
->tags($tags)
64+
->get($key);
65+
$liveResults = (new UncachedAuthor)
66+
->all();
67+
68+
$this->assertEquals($customCacheResults, $authors);
69+
$this->assertNull($defaultcacheResults);
70+
$this->assertEmpty($liveResults->diffAssoc($customCacheResults));
71+
}
72+
}

0 commit comments

Comments
 (0)