Skip to content

Commit

Permalink
feature: support disabling cache (#10)
Browse files Browse the repository at this point in the history
Co-authored-by: ryangjchandler <[email protected]>
  • Loading branch information
ryangjchandler and ryangjchandler authored May 1, 2023
1 parent bb426d9 commit 25749d7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ If you want to cache the content for a particular model, i.e. a `User` model, yo

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

### Disabling caching

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`.

Alternatively, publish the configuration file and modify the `enabled` entry accordingly.

## Testing

```bash
Expand Down
2 changes: 2 additions & 0 deletions config/blade-cache-directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

return [

'enabled' => env('BLADE_CACHE_DIRECTIVE_ENABLED', true),

'ttl' => env('BLADE_CACHE_DIRECTIVE_TTL', 3600),

];
2 changes: 1 addition & 1 deletion src/BladeCacheDirectiveServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function packageBooted()
\$__cache_directive_ttl = config('blade-cache-directive.ttl');
}
if (\Illuminate\Support\Facades\Cache::has(\$__cache_directive_key)) {
if (config('blade-cache-directive.enabled') && \Illuminate\Support\Facades\Cache::has(\$__cache_directive_key)) {
echo \Illuminate\Support\Facades\Cache::get(\$__cache_directive_key);
} else {
\$__cache_directive_buffering = true;
Expand Down
21 changes: 18 additions & 3 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace RyanChandler\BladeCacheDirective\Tests;

use Artisan;
use Carbon\Carbon;
use Illuminate\Support\Facades\Artisan;

class CacheTest extends TestCase
{
Expand All @@ -18,7 +19,7 @@ public function setUp(): void
$this->second_value = now()->subDays(20);
$this->third_value = now()->subDays(60);
}

/** @test */
public function the_cache_directive_will_render_the_same_view_before_ttl_expired()
{
Expand All @@ -41,7 +42,21 @@ public function the_cache_directive_will_render_other_view_after_ttl_expired()
$this->assertEquals($this->second_value->format('Y-m-d H:i:s'), $this->renderView('cache', compact('time')));
}

protected function renderView($view, $parameters)
/** @test */
public function the_cache_directive_can_be_disabled()
{
config()->set('blade-cache-directive.enabled', false);

$first = $this->renderView('disabled');

Carbon::setTestNow(now()->addMinute());

$second = $this->renderView('disabled');

$this->assertNotEquals($first, $second);
}

protected function renderView($view, $parameters = [])
{
Artisan::call('view:clear');

Expand Down
12 changes: 0 additions & 12 deletions tests/ExampleTest.php

This file was deleted.

3 changes: 3 additions & 0 deletions tests/resources/views/disabled.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@cache('now', now()->addDay())
{{ now()->format('H:i:s') }}
@endcache

0 comments on commit 25749d7

Please sign in to comment.