Skip to content

Commit 38f9ec9

Browse files
committed
Merge branch 'master' into feature/add-config-to-disable-cache
2 parents 2085f89 + 9098a2b commit 38f9ec9

17 files changed

+237
-249
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ 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.35] - 21 Feb 2018
8+
### Fixed
9+
- cache key generation for `find()` and `findOrFail()`.
10+
11+
### Added
12+
- caching for `paginate()`;
13+
714
## [0.2.33] - 19 Feb 2018
815
### Added
916
- unit test to make sure `Model::all()` returns a collection when only only

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"phpmd/phpmd": "*",
2626
"phpunit/phpunit": "*",
2727
"sebastian/phpcpd": "*",
28-
"symfony/thanks": "^1.0"
28+
"symfony/thanks": "^1.0",
29+
"predis/predis": "^1.1"
2930
},
3031
"autoload": {
3132
"psr-4": {

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
convertWarningsToExceptions="true"
1010
printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer"
1111
processIsolation="false"
12-
stopOnFailure="true"
12+
stopOnFailure="false"
1313
syntaxCheck="false"
1414
>
1515
<testsuites>

src/CacheKey.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class CacheKey
1313
protected function getCachePrefix() : string
1414
{
1515
return "genealabs:laravel-model-caching:"
16-
. (config('genealabs:laravel-model-caching', '')
17-
? config('genealabs:laravel-model-caching', '') . ":"
16+
. (config('laravel-model-caching.cache-prefix')
17+
? config('laravel-model-caching.cache-prefix', '') . ":"
1818
: "");
1919
}
2020

src/CacheTags.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public function __construct(array $eagerLoad, Model $model)
1818
protected function getCachePrefix() : string
1919
{
2020
return "genealabs:laravel-model-caching:"
21-
. (config('genealabs:laravel-model-caching', '')
22-
? config('genealabs:laravel-model-caching', '') . ":"
21+
. (config('laravel-model-caching.cache-prefix')
22+
? config('laravel-model-caching.cache-prefix', '') . ":"
2323
: "");
2424
}
2525

src/CachedBuilder.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function find($id, $columns = ['*'])
6767
}
6868

6969
$arguments = func_get_args();
70-
$cacheKey = $this->makeCacheKey($columns);
70+
$cacheKey = $this->makeCacheKey(['*'], null, "-find_{$id}");
7171
$method = 'find';
7272

7373
return $this->cachedValue($arguments, $cacheKey, $method);
@@ -125,6 +125,24 @@ public function min($column)
125125
return $this->cachedValue($arguments, $cacheKey, $method);
126126
}
127127

128+
public function paginate(
129+
$perPage = null,
130+
$columns = ['*'],
131+
$pageName = 'page',
132+
$page = null
133+
) {
134+
if (! $this->isCachable) {
135+
return parent::paginate($perPage, $columns, $pageName, $page);
136+
}
137+
138+
$arguments = func_get_args();
139+
$page = $page ?: 1;
140+
$cacheKey = $this->makeCacheKey($columns, null, "-paginate_by_{$perPage}_{$pageName}_{$page}");
141+
$method = 'paginate';
142+
143+
return $this->cachedValue($arguments, $cacheKey, $method);
144+
}
145+
128146
public function pluck($column, $key = null)
129147
{
130148
if (! $this->isCachable) {
@@ -197,7 +215,9 @@ protected function preventHashCollision(
197215
string $method
198216
) {
199217
if ($result['key'] !== $cacheKey) {
200-
cache()->tags($cacheTags)->forget($hashedCacheKey);
218+
$this->cache()
219+
->tags($cacheTags)
220+
->forget($hashedCacheKey);
201221

202222
$result = $this->retrieveCachedValue(
203223
$arguments,

src/Console/Commands/Flush.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ public function handle()
1313

1414
if (! $option) {
1515
cache()
16-
->store(config('laravel-model-caching:store'))
16+
->store(config('laravel-model-caching.store'))
1717
->flush();
1818

19+
$this->info("✔︎ Entire model cache has been flushed.");
20+
1921
return 0;
2022
}
2123

src/Traits/Cachable.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
trait Cachable
1212
{
1313
protected $isCachable = true;
14-
protected static $isCachableKey = 'genealabs:laravel-model-caching:is-disabled';
1514

1615
protected function cache(array $tags = [])
1716
{
@@ -43,8 +42,6 @@ protected function addTagsWhenCalledFromCachedBuilder(array $tags) : array
4342

4443
public function disableCache()
4544
{
46-
cache()->forever(self::$isCachableKey, true);
47-
4845
$this->isCachable = false;
4946

5047
return $this;
@@ -89,10 +86,6 @@ public static function bootCachable()
8986

9087
public static function all($columns = ['*'])
9188
{
92-
if (! cache()->get(self::$isCachableKey)) {
93-
return parent::all($columns);
94-
}
95-
9689
$class = get_called_class();
9790
$instance = new $class;
9891
$tags = [str_slug(get_called_class())];
@@ -106,8 +99,8 @@ public static function all($columns = ['*'])
10699

107100
public function newEloquentBuilder($query)
108101
{
109-
if (cache()->get(self::$isCachableKey)) {
110-
cache()->forget(self::$isCachableKey);
102+
if (! $this->isCachable) {
103+
$this->isCachable = true;
111104

112105
return new EloquentBuilder($query);
113106
}

tests/CreatesApplication.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests;
22

33
use GeneaLabs\LaravelModelCaching\Providers\Service as LaravelModelCachingService;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
8+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
49
use Orchestra\Database\ConsoleServiceProvider;
510

611
trait CreatesApplication
712
{
13+
protected $cache;
14+
815
public function setUp()
916
{
1017
parent::setUp();
1118

1219
$this->withFactories(__DIR__ . '/database/factories');
1320
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
21+
22+
$this->cache = cache()
23+
->store(config('laravel-model-caching.store'));
24+
25+
$this->cache()->flush();
26+
$publishers = factory(Publisher::class, 10)->create();
27+
factory(Author::class, 10)->create()
28+
->each(function ($author) use ($publishers) {
29+
factory(Book::class, random_int(2, 10))->make()
30+
->each(function ($book) use ($author, $publishers) {
31+
$book->author()->associate($author);
32+
$book->publisher()->associate($publishers[rand(0, 9)]);
33+
$book->save();
34+
});
35+
factory(Profile::class)->make([
36+
'author_id' => $author->id,
37+
]);
38+
});
39+
40+
$bookIds = (new Book)->all()->pluck('id');
41+
factory(Store::class, 10)->create()
42+
->each(function ($store) use ($bookIds) {
43+
$store->books()->sync(rand($bookIds->min(), $bookIds->max()));
44+
});
45+
$this->cache()->flush();
1446
}
1547

1648
/**
@@ -23,4 +55,22 @@ protected function getPackageProviders($app)
2355
ConsoleServiceProvider::class,
2456
];
2557
}
58+
59+
protected function getEnvironmentSetUp($app)
60+
{
61+
$app['config']->set('database.redis.default', [
62+
'host' => env('REDIS_HOST', '192.168.10.10'),
63+
]);
64+
$app['config']->set('database.redis.model-cache', [
65+
'host' => env('REDIS_HOST', '192.168.10.10'),
66+
'password' => env('REDIS_PASSWORD', null),
67+
'port' => env('REDIS_PORT', 6379),
68+
'database' => 1,
69+
]);
70+
$app['config']->set('cache.stores.model', [
71+
'driver' => 'redis',
72+
'connection' => 'model-cache',
73+
]);
74+
$app['config']->set('laravel-model-caching.store', 'model');
75+
}
2676
}

tests/Fixtures/BaseModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class BaseModel extends Model
1111

1212
public function __construct($attributes = [])
1313
{
14-
config(['genealabs:laravel-model-caching' => 'test-prefix']);
14+
config(['laravel-model-caching.cache-prefix' => 'test-prefix']);
1515

1616
parent::__construct($attributes);
1717
}

0 commit comments

Comments
 (0)