Skip to content

Commit

Permalink
✨ enforced but lazier boost index
Browse files Browse the repository at this point in the history
💚 more unittests
  • Loading branch information
bnomei committed Sep 30, 2021
1 parent 6a6c06b commit f3040ce
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 22 deletions.
5 changes: 5 additions & 0 deletions classes/BoostIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public function toArray(): array
return $this->index;
}

public function count(): int
{
return count($this->index);
}

private static $singleton;
public static function singleton(): self
{
Expand Down
16 changes: 14 additions & 2 deletions classes/PageHasBoost.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ public function writeContent(array $data, string $languageCode = null): bool
$this->writeContentCache($data, $languageCode);
}

public function delete(bool $force = false): bool
public function deleteContentCache(): bool
{
$cache = BoostCache::singleton();
if (! $cache) {
return parent::delete($force);
return true;
}

foreach (kirby()->languages() as $language) {
Expand All @@ -168,6 +168,18 @@ public function delete(bool $force = false): bool
$this->contentBoostedKey().'-modified'
);

return true;
}

public function delete(bool $force = false): bool
{
$cache = BoostCache::singleton();
if (! $cache) {
return parent::delete($force);
}

$this->deleteContentCache();

return parent::delete($force);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bnomei/kirby3-boost",
"type": "kirby-plugin",
"version": "1.4.1",
"version": "1.5.0",
"description": "Boost the speed of Kirby by having content files of pages cached, with automatic unique ID, fast lookup and Tiny-URL.",
"license": "MIT",
"authors": [
Expand Down
28 changes: 14 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,20 @@ function siteIndexFilterByBoostID(string $id): ?\Kirby\Cms\Page
}
return true;
},
'unboost' => function () {
// has boost?
if ($this->hasBoost() === false) {
return false;
}
$this->boostIndexRemove();
return $this->deleteContentCache();
},
'isBoosted' => function () {
// has boost?
if ($this->hasBoost() === false) {
return false;
}
$this->boostIndexAdd();
// $this->boostIndexAdd(); // this would trigger content add
return $this->isContentBoosted(kirby()->languageCode());
},
'boostIDField' => function () {
Expand Down Expand Up @@ -135,6 +143,16 @@ function siteIndexFilterByBoostID(string $id): ?\Kirby\Cms\Page
\Bnomei\BoostCache::endTransaction();
return round(($time + microtime(true)) * 1000);
},
'unboost' => function () {
$time = -microtime(true);
$count = 0;
\Bnomei\BoostCache::beginTransaction();
foreach ($this as $page) {
$count += $page->unboost() ? 1 : 0;
}
\Bnomei\BoostCache::endTransaction();
return round(($time + microtime(true)) * 1000);
},
'boostIndexAdd' => function () {
$time = -microtime(true);
foreach ($this as $page) {
Expand All @@ -158,6 +176,7 @@ function siteIndexFilterByBoostID(string $id): ?\Kirby\Cms\Page
if ($page->hasBoost()) {
// uuid and a field to force reading from cache
$str .= $page->diruri() . $page->modified() . $page->boostIDField()->value();
$page->boostIndexAdd();
$count++;
}
}
Expand Down
147 changes: 147 additions & 0 deletions tests/BoostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Kirby\Cms\Page;
use PHPUnit\Framework\TestCase;

final class BoostTest extends TestCase
{
public function randomPage(): ?Page
{
return site()->index()->notTemplate('home')->shuffle()->first();
}

public function randomPageWithChildren(): ?Page
{
$page = null;
while(!$page) {
$rp = $this->randomPage();
if ($rp->hasChildren()) {
$page = $rp;
}
}
return $page;
}

public function testHelperBolt()
{
$randomPage = $this->randomPage();
$randomPage->boost();
$this->assertEquals($randomPage->id(), bolt($randomPage->diruri())->id());
}

public function testHelperModified()
{
$randomPage = $this->randomPage();
$randomPage->boost();
$this->assertEquals($randomPage->modified(), modified($randomPage->id()));
}

public function testHelperBoost()
{
$randomPage = $this->randomPage();
$randomPage->boost();
$this->assertEquals($randomPage->id(), boost($randomPage->boostid()->value())->id());
}

public function testPageMethodBolt()
{
$randomPage = $this->randomPageWithChildren();
$lastChild = $randomPage->children()->last();
$this->assertEquals(
$lastChild->id(),
$randomPage->bolt($lastChild->slug())->id()
);
}

public function testPageMethodBoost()
{
$randomPage = $this->randomPage();
$this->assertTrue($randomPage->boost());
}

public function testPageMethodIsBoosted()
{
$randomPage = $this->randomPage();
$randomPage->deleteContentCache();

$this->assertFalse($randomPage->isBoosted());
$this->assertTrue($randomPage->boost());
$this->assertTrue($randomPage->isBoosted());
}

public function testPageMethodBoostIDField()
{
$randomPage = $this->randomPage();
$this->assertEquals($randomPage->boostid()->value(), $randomPage->boostIDField()->value());

$this->assertEquals($randomPage->boostid()->value(), $randomPage->BOOSTID());
}

public function testPageMethodTinyUrl()
{
$randomPage = $this->randomPage();
$this->assertStringEndsWith('/x/' . $randomPage->boostid()->value(), $randomPage->tinyUrl());
}

public function testPagesMethodBoost()
{
$randomPage = $this->randomPageWithChildren();

\Bnomei\BoostIndex::singleton()->index(true);
$before = \Bnomei\BoostIndex::singleton()->count();
$this->assertIsFloat($randomPage->children()->boostIndexRemove());
$after = \Bnomei\BoostIndex::singleton()->count();
$this->assertEquals($after, $before - $randomPage->children()->count());

$before = \Bnomei\BoostIndex::singleton()->count();
$this->assertIsFloat($randomPage->children()->boostIndexAdd());
$after = \Bnomei\BoostIndex::singleton()->count();
$this->assertEquals($after, $before + $randomPage->children()->count());

$this->assertIsFloat($randomPage->children()->boost());
$this->assertIsFloat(site()->boost());
}

public function testPagesMethodBoostmark()
{
$randomPage = $this->randomPageWithChildren();

\Bnomei\BoostIndex::singleton()->index(true);
\Bnomei\BoostIndex::singleton()->flush();
$this->assertIsFloat($randomPage->children()->unboost());
$before = \Bnomei\BoostIndex::singleton()->count();
$boostmark = $randomPage->children()->boostmark();
$this->assertIsArray($boostmark);
$this->assertEquals($randomPage->children()->count(), $boostmark['count']);
$after = \Bnomei\BoostIndex::singleton()->count();
$this->assertEquals($after, $before + $randomPage->children()->count());
$boostmark2 = $randomPage->children()->boostmark();
$this->assertEquals($boostmark2['checksum'], $boostmark['checksum']);

// trigger file write
kirby()->impersonate('kirby');
$randomPage->update([
'title' => $randomPage->title()->value(),
]);
$boostmark3 = site()->boostmark();
$this->assertIsArray($boostmark3);
$this->assertNotEquals($boostmark3['checksum'], $boostmark['checksum']);
}

public function testFieldMethodsBoost()
{
$randomPage = $this->randomPage();
$many = $randomPage->related()->fromBoostIDs();
$this->assertNotNull($many);

kirby()->impersonate('kirby');
$randomPage = $randomPage->update([
'related' => $randomPage->related()->split()[0],
]);
$one = $randomPage->related()->fromBoostID();
$this->assertNotNull($many);
}

}
8 changes: 4 additions & 4 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php return array(
'root' => array(
'pretty_version' => '1.4.1',
'version' => '1.4.1.0',
'pretty_version' => '1.5.0',
'version' => '1.5.0.0',
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -20,8 +20,8 @@
'dev_requirement' => false,
),
'bnomei/kirby3-boost' => array(
'pretty_version' => '1.4.1',
'version' => '1.4.1.0',
'pretty_version' => '1.5.0',
'version' => '1.5.0.0',
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down

0 comments on commit f3040ce

Please sign in to comment.