Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions config/static_caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@

'ignore_query_strings' => false,

/*
|--------------------------------------------------------------------------
| Nocache
|--------------------------------------------------------------------------
|
| Here you may define where the nocache data is stored.
|
| Supported drivers: "cache", "database"
|
*/

'nocache' => 'cache',

/*
|--------------------------------------------------------------------------
| Replacers
Expand Down
42 changes: 42 additions & 0 deletions src/Console/Commands/NocacheMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Statamic\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Composer;
use Statamic\Console\RunsInPlease;
use Statamic\Facades\File;

class NocacheMigration extends Command
{
use RunsInPlease;

protected $composer;
protected $signature = 'statamic:nocache:migration {--path=}';
protected $description = 'Generate Nocache Migrations';

public function __construct(Composer $composer)
{
parent::__construct();

$this->composer = $composer;
}

public function handle()
{
$from = __DIR__.'/stubs/statamic_nocache_tables.php.stub';
$file = Carbon::now()->format('Y_m_d_His').'_statamic_nocache_tables';
$to = ($path = $this->option('path')) ? $path."/{$file}.php" : database_path("migrations/{$file}.php");

$contents = File::get($from);

$contents = str_replace('NOCACHE_TABLE', 'nocache_regions', $contents);

File::put($to, $contents);

$this->components->info("Migration [$file] created successfully.");

$this->composer->dumpAutoloads();
}
}
22 changes: 22 additions & 0 deletions src/Console/Commands/stubs/statamic_nocache_tables.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class StatamicNocacheTables extends Migration
{
public function up()
{
Schema::create('NOCACHE_TABLE', function (Blueprint $table) {
$table->string('key')->index()->primary();
$table->string('url')->index();
$table->longText('region');
});
}

public function down()
{
Schema::dropIfExists('nocache_regions');
}
}
1 change: 1 addition & 0 deletions src/Providers/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ConsoleServiceProvider extends ServiceProvider
Commands\SupportZipBlueprint::class,
Commands\AuthMigration::class,
Commands\Multisite::class,
Commands\NocacheMigration::class,
Commands\SiteClear::class,
Commands\UpdatesRun::class,
Commands\ImportGroups::class,
Expand Down
20 changes: 20 additions & 0 deletions src/StaticCaching/NoCache/DatabaseRegion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Statamic\StaticCaching\NoCache;

use Illuminate\Database\Eloquent\Model;

class DatabaseRegion extends Model
{
protected $table = 'nocache_regions';

protected $guarded = [];

protected $primaryKey = 'key';

public $timestamps = false;

protected $casts = [
'key' => 'string',
];
}
44 changes: 44 additions & 0 deletions src/StaticCaching/NoCache/DatabaseSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Statamic\StaticCaching\NoCache;

class DatabaseSession extends Session
{
public function write()
{
// Nothing to write. Session gets compiled by querying regions.
}

public function restore()
{
$regions = DatabaseRegion::where('url', $this->url)->get(['key']);

$this->regions = $regions->map->key;

$this->cascade = $this->restoreCascade();

$this->resolvePageAndPathForPagination();

return $this;
}

public function region(string $key): Region
{
$region = DatabaseRegion::where('key', $key)->first();

if (! $region) {
throw new RegionNotFound($key);
}

return unserialize($region->region);
}

protected function cacheRegion(Region $region)
{
DatabaseRegion::create([
'key' => $region->key(),
'url' => $this->url,
'region' => serialize($region),
]);
}
}
6 changes: 3 additions & 3 deletions src/StaticCaching/NoCache/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ public function restore()
return $this;
}

private function restoreCascade()
protected function restoreCascade()
{
return Cascade::instance()
->withContent(Data::findByRequestUrl($this->url))
->hydrate()
->toArray();
}

private function resolvePageAndPathForPagination(): void
protected function resolvePageAndPathForPagination(): void
{
AbstractPaginator::currentPathResolver(fn () => Str::before($this->url, '?'));

Expand All @@ -148,7 +148,7 @@ private function resolvePageAndPathForPagination(): void
});
}

private function cacheRegion(Region $region)
protected function cacheRegion(Region $region)
{
StaticCache::cacheStore()->forever('nocache::region.'.$region->key(), $region);
}
Expand Down
7 changes: 6 additions & 1 deletion src/StaticCaching/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
use Statamic\Facades\Cascade;
use Statamic\StaticCaching\NoCache\DatabaseSession;
use Statamic\StaticCaching\NoCache\Session;

class ServiceProvider extends LaravelServiceProvider
Expand Down Expand Up @@ -41,7 +42,11 @@ public function register()
$uri = explode('?', $uri)[0];
}

return new Session($uri);
return match ($driver = config('statamic.static_caching.nocache', 'cache')) {
'cache' => new Session($uri),
'database' => new DatabaseSession($uri),
default => throw new \Exception('Nocache driver ['.$driver.'] is not supported.'),
};
});

$this->app->bind(UrlExcluder::class, function ($app) {
Expand Down
20 changes: 17 additions & 3 deletions src/StaticCaching/StaticCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Statamic\StaticCaching\Cachers\FileCacher;
use Statamic\StaticCaching\Cachers\NullCacher;
use Statamic\StaticCaching\Cachers\Writer;
use Statamic\StaticCaching\NoCache\DatabaseRegion;
use Statamic\Support\Manager;

class StaticCacheManager extends Manager
Expand Down Expand Up @@ -66,11 +67,26 @@ public function flush()
{
$this->driver()->flush();

$this->flushNocache();

if ($this->hasCustomStore()) {
$this->cacheStore()->flush();
}

StaticCacheCleared::dispatch();
}

private function flushNocache()
{
if (config('statamic.static_caching.nocache', 'cache') === 'database') {
DatabaseRegion::truncate();

StaticCacheCleared::dispatch();
return;
}

// No need to do any looping if there's a custom
// store because the entire store will be flushed.
if ($this->hasCustomStore()) {
return;
}

Expand All @@ -81,8 +97,6 @@ public function flush()
});

$this->cacheStore()->forget('nocache::urls');

StaticCacheCleared::dispatch();
}

public function nocacheJs(string $js)
Expand Down