Skip to content

Commit

Permalink
Add caching for docs content
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeTowers committed Jul 20, 2023
1 parent 94e6e55 commit e8f8bcd
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions classes/BaseDocumentation.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php namespace Winter\Docs\Classes;

use ApplicationException;
use Cache;
use Config;
use DirectoryIterator;
use File;
use Http;
use Illuminate\Contracts\Filesystem\Filesystem;
use Lang;
use Config;
use Storage;
use DirectoryIterator;
use ApplicationException;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use Illuminate\Contracts\Filesystem\Filesystem;
use RecursiveIteratorIterator;
use Storage;
use Winter\Docs\Classes\Contracts\Documentation;
use Winter\Docs\Classes\Contracts\Page;
use Winter\Storm\Filesystem\Zip;
Expand Down Expand Up @@ -112,6 +113,14 @@ public function __construct(string $identifier, array $config = [])
$this->repositoryEditUrl = $config['repository']['editUrl'] ?? null;
}

/**
* Gets the cache key for this documentation.
*/
public function getCacheKey(string $suffix = ''): string
{
return !empty($suffix) ? $this->getIdentifier() . '.' . $suffix : $this->getIdentifier();
}

/**
* Gets the identifier of the documentation.
*/
Expand Down Expand Up @@ -159,10 +168,12 @@ public function isAvailable(): bool
return $this->available;
}

return $this->available = (
$this->getStorageDisk()->exists($this->getProcessedPath('page-map.json'))
&& $this->getStorageDisk()->exists($this->getProcessedPath('toc.json'))
);
return $this->available = Cache::rememberForever($this->getCacheKey('available'), function () {
return (
$this->getStorageDisk()->exists($this->getProcessedPath('page-map.json'))
&& $this->getStorageDisk()->exists($this->getProcessedPath('toc.json'))
);
});
}

/**
Expand Down Expand Up @@ -532,9 +543,11 @@ public function getProcessedAssetsPath(string $suffix = ''): string
*/
public function getProcessedFile(string $path): ?string
{
return $this->getStorageDisk()->get(
$this->getProcessedPath($path)
) ?? null;
return Cache::rememberForever('winter.docs::storage.processedFileContents.' . $this->getCacheKey($path), function () use ($path) {
return $this->getStorageDisk()->get(
$this->getProcessedPath($path)
) ?? null;
});
}

/**
Expand Down

0 comments on commit e8f8bcd

Please sign in to comment.