Skip to content

Commit 25749d7

Browse files
feature: support disabling cache (#10)
Co-authored-by: ryangjchandler <ryangjchandler@users.noreply.github.com>
1 parent bb426d9 commit 25749d7

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ If you want to cache the content for a particular model, i.e. a `User` model, yo
5252

5353
When a new user is passed to this view, a separate cache entry will be created.
5454

55+
### Disabling caching
56+
57+
If you wish to disable caching when using the `@cache` directive (useful for local development and testing), you can set the `BLADE_CACHE_DIRECTIVE_ENABLED` environment variable to `false`.
58+
59+
Alternatively, publish the configuration file and modify the `enabled` entry accordingly.
60+
5561
## Testing
5662

5763
```bash

config/blade-cache-directive.php

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

33
return [
44

5+
'enabled' => env('BLADE_CACHE_DIRECTIVE_ENABLED', true),
6+
57
'ttl' => env('BLADE_CACHE_DIRECTIVE_TTL', 3600),
68

79
];

src/BladeCacheDirectiveServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function packageBooted()
2828
\$__cache_directive_ttl = config('blade-cache-directive.ttl');
2929
}
3030
31-
if (\Illuminate\Support\Facades\Cache::has(\$__cache_directive_key)) {
31+
if (config('blade-cache-directive.enabled') && \Illuminate\Support\Facades\Cache::has(\$__cache_directive_key)) {
3232
echo \Illuminate\Support\Facades\Cache::get(\$__cache_directive_key);
3333
} else {
3434
\$__cache_directive_buffering = true;

tests/CacheTest.php

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

33
namespace RyanChandler\BladeCacheDirective\Tests;
44

5-
use Artisan;
5+
use Carbon\Carbon;
6+
use Illuminate\Support\Facades\Artisan;
67

78
class CacheTest extends TestCase
89
{
@@ -18,7 +19,7 @@ public function setUp(): void
1819
$this->second_value = now()->subDays(20);
1920
$this->third_value = now()->subDays(60);
2021
}
21-
22+
2223
/** @test */
2324
public function the_cache_directive_will_render_the_same_view_before_ttl_expired()
2425
{
@@ -41,7 +42,21 @@ public function the_cache_directive_will_render_other_view_after_ttl_expired()
4142
$this->assertEquals($this->second_value->format('Y-m-d H:i:s'), $this->renderView('cache', compact('time')));
4243
}
4344

44-
protected function renderView($view, $parameters)
45+
/** @test */
46+
public function the_cache_directive_can_be_disabled()
47+
{
48+
config()->set('blade-cache-directive.enabled', false);
49+
50+
$first = $this->renderView('disabled');
51+
52+
Carbon::setTestNow(now()->addMinute());
53+
54+
$second = $this->renderView('disabled');
55+
56+
$this->assertNotEquals($first, $second);
57+
}
58+
59+
protected function renderView($view, $parameters = [])
4560
{
4661
Artisan::call('view:clear');
4762

tests/ExampleTest.php

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@cache('now', now()->addDay())
2+
{{ now()->format('H:i:s') }}
3+
@endcache

0 commit comments

Comments
 (0)