Skip to content

Commit 4be5937

Browse files
authored
Merge pull request #37 from renoki-co/fix/tags-on-file-driver
[fix] Non-taggable drivers support
2 parents 932b74c + ce2f279 commit 4be5937

File tree

7 files changed

+79
-39
lines changed

7 files changed

+79
-39
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ jobs:
2929
composer update --no-interaction --prefer-stable
3030
- name: Run tests
3131
run: |
32-
phpunit --coverage-text --coverage-clover=coverage.xml
32+
CACHE_DRIVER=array phpunit --coverage-text --coverage-clover=coverage_array.xml
33+
CACHE_DRIVER=file phpunit --coverage-text --coverage-clover=coverage_file.xml
3334
- uses: codecov/codecov-action@v1
3435
with:
3536
fail_ci_if_error: false
37+
file: '*.xml'

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor
22
composer.phar
33
composer.lock
4-
.DS_Store
4+
.DS_Store
5+
database.sqlite

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
<directory suffix=".php">src/</directory>
2020
</whitelist>
2121
</filter>
22+
<php>
23+
<server name="APP_ENV" value="testing" />
24+
</php>
2225
</phpunit>

src/Traits/QueryCacheModule.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Rennokki\QueryCache\Traits;
44

5+
use BadMethodCallException;
56
use DateTime;
67

78
trait QueryCacheModule
@@ -192,11 +193,11 @@ public function flushQueryCacheWithTag(string $tag): bool
192193
{
193194
$cache = $this->getCacheDriver();
194195

195-
if (! method_exists($cache, 'tags')) {
196-
return false;
196+
try {
197+
return $cache->tags($tag)->flush();
198+
} catch (BadMethodCallException $e) {
199+
return $cache->flush();
197200
}
198-
199-
return $cache->tags($tag)->flush();
200201
}
201202

202203
/**
@@ -334,7 +335,11 @@ public function getCache()
334335
$this->getCacheBaseTags() ?: []
335336
);
336337

337-
return $tags ? $cache->tags($tags) : $cache;
338+
try {
339+
return $tags ? $cache->tags($tags) : $cache;
340+
} catch (BadMethodCallException $e) {
341+
return $cache;
342+
}
338343
}
339344

340345
/**

tests/FlushCacheOnUpdateTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Rennokki\QueryCache\Test;
44

5-
use Cache;
65
use Rennokki\QueryCache\Test\Models\Page;
76

87
class FlushCacheOnUpdateTest extends TestCase
@@ -11,7 +10,7 @@ public function test_flush_cache_on_create()
1110
{
1211
$page = factory(Page::class)->create();
1312
$storedPage = Page::cacheFor(now()->addHours(1))->first();
14-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
13+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
1514

1615
$this->assertNotNull($cache);
1716

@@ -24,7 +23,7 @@ public function test_flush_cache_on_create()
2423
'name' => '9GAG',
2524
]);
2625

27-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
26+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
2827

2928
$this->assertNull($cache);
3029
}
@@ -33,7 +32,7 @@ public function test_flush_cache_on_update()
3332
{
3433
$page = factory(Page::class)->create();
3534
$storedPage = Page::cacheFor(now()->addHours(1))->first();
36-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
35+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
3736

3837
$this->assertNotNull($cache);
3938

@@ -46,7 +45,7 @@ public function test_flush_cache_on_update()
4645
'name' => '9GAG',
4746
]);
4847

49-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
48+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
5049

5150
$this->assertNull($cache);
5251
}
@@ -55,7 +54,7 @@ public function test_flush_cache_on_delete()
5554
{
5655
$page = factory(Page::class)->create();
5756
$storedPage = Page::cacheFor(now()->addHours(1))->first();
58-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
57+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
5958

6059
$this->assertNotNull($cache);
6160

@@ -66,7 +65,7 @@ public function test_flush_cache_on_delete()
6665

6766
$page->delete();
6867

69-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
68+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
7069

7170
$this->assertNull($cache);
7271
}
@@ -75,7 +74,7 @@ public function test_flush_cache_on_force_deletion()
7574
{
7675
$page = factory(Page::class)->create();
7776
$storedPage = Page::cacheFor(now()->addHours(1))->first();
78-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
77+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
7978

8079
$this->assertNotNull($cache);
8180

@@ -86,7 +85,7 @@ public function test_flush_cache_on_force_deletion()
8685

8786
$page->forceDelete();
8887

89-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
88+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
9089

9190
$this->assertNull($cache);
9291
}

tests/MethodsTest.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ public function test_cache_tags()
3636
$post = factory(Post::class)->create();
3737
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
3838

39-
$cache = Cache::get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
40-
$this->assertNull($cache);
39+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
40+
41+
// The caches that do not support tagging should
42+
// cache the query either way.
43+
$this->driverSupportsTags()
44+
? $this->assertNull($cache)
45+
: $this->assertNotNull($cache);
4146

42-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
47+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
4348
$this->assertNotNull($cache);
4449
}
4550

@@ -48,12 +53,12 @@ public function test_cache_flush_with_the_right_tag()
4853
$post = factory(Post::class)->create();
4954
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
5055

51-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
56+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
5257
$this->assertNotNull($cache);
5358

5459
Post::flushQueryCache(['test']);
5560

56-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
61+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
5762
$this->assertNull($cache);
5863
}
5964

@@ -62,22 +67,27 @@ public function test_cache_flush_without_the_right_tag()
6267
$post = factory(Post::class)->create();
6368
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
6469

65-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
70+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
6671
$this->assertNotNull($cache);
6772

6873
Post::flushQueryCache(['test2']);
6974
Post::flushQueryCacheWithTag('test2');
7075

71-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
72-
$this->assertNotNull($cache);
76+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
77+
78+
// The caches that do not support tagging should
79+
// flush the cache either way since tags are not supported.
80+
$this->driverSupportsTags()
81+
? $this->assertNotNull($cache)
82+
: $this->assertNull($cache);
7383
}
7484

7585
public function test_cache_flush_with_more_tags()
7686
{
7787
$post = factory(Post::class)->create();
7888
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
7989

80-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
90+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
8191
$this->assertNotNull($cache);
8292

8393
Post::flushQueryCache([
@@ -86,7 +96,7 @@ public function test_cache_flush_with_more_tags()
8696
'test3',
8797
]);
8898

89-
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
99+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
90100
$this->assertNull($cache);
91101
}
92102

@@ -95,12 +105,12 @@ public function test_cache_flush_with_default_tags_attached()
95105
$book = factory(Book::class)->create();
96106
$storedBook = Book::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
97107

98-
$cache = Cache::tags(['test', Book::getCacheBaseTags()[0]])->get('leqc:sqlitegetselect * from "books" limit 1a:0:{}');
108+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "books" limit 1a:0:{}', ['test', Book::getCacheBaseTags()[0]]);
99109
$this->assertNotNull($cache);
100110

101111
Book::flushQueryCache();
102112

103-
$cache = Cache::tags(['test', Book::getCacheBaseTags()[0]])->get('leqc:sqlitegetselect * from "books" limit 1a:0:{}');
113+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "books" limit 1a:0:{}', ['test', Book::getCacheBaseTags()[0]]);
104114

105115
$this->assertNull($cache);
106116
}

tests/TestCase.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
namespace Rennokki\QueryCache\Test;
44

5+
use Cache;
56
use Orchestra\Testbench\TestCase as Orchestra;
67

78
abstract class TestCase extends Orchestra
89
{
910
/**
10-
* Set up the tests.
11-
*
12-
* @return void
11+
* {@inheritdoc}
1312
*/
1413
public function setUp(): void
1514
{
@@ -27,10 +26,7 @@ public function setUp(): void
2726
}
2827

2928
/**
30-
* Get the package providers.
31-
*
32-
* @param mixed $app
33-
* @return array
29+
* {@inheritdoc}
3430
*/
3531
protected function getPackageProviders($app)
3632
{
@@ -40,10 +36,7 @@ protected function getPackageProviders($app)
4036
}
4137

4238
/**
43-
* Set up the environment.
44-
*
45-
* @param mixed $app
46-
* @return void
39+
* {@inheritdoc}
4740
*/
4841
public function getEnvironmentSetUp($app)
4942
{
@@ -53,6 +46,9 @@ public function getEnvironmentSetUp($app)
5346
'database' => __DIR__.'/database.sqlite',
5447
'prefix' => '',
5548
]);
49+
$app['config']->set(
50+
'cache.driver', getenv('CACHE_DRIVER') ?: env('CACHE_DRIVER', 'array')
51+
);
5652
$app['config']->set('auth.providers.users.model', User::class);
5753
$app['config']->set('auth.providers.posts.model', Post::class);
5854
$app['config']->set('auth.providers.kids.model', Kid::class);
@@ -80,4 +76,28 @@ protected function clearCache()
8076
{
8177
$this->artisan('cache:clear');
8278
}
79+
80+
/**
81+
* Get the cache with tags, if the driver supports it.
82+
*
83+
* @param string $key
84+
* @param array|null $tags
85+
* @return mixed
86+
*/
87+
protected function getCacheWithTags(string $key, $tags = null)
88+
{
89+
return $this->driverSupportsTags()
90+
? Cache::tags($tags)->get($key)
91+
: Cache::get($key);
92+
}
93+
94+
/**
95+
* Check if the current driver supports tags.
96+
*
97+
* @return bool
98+
*/
99+
protected function driverSupportsTags(): bool
100+
{
101+
return ! in_array(config('cache.driver'), ['file', 'database']);
102+
}
83103
}

0 commit comments

Comments
 (0)