Skip to content

Commit

Permalink
add fallback for building of cache
Browse files Browse the repository at this point in the history
  • Loading branch information
xini committed Dec 18, 2019
1 parent b6bd7f7 commit 0b6a07f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
20 changes: 13 additions & 7 deletions src/ListedPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Fromholdio\Listings\Extensions\ListedPageExtension;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Middleware\FlushMiddleware;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Flushable;
Expand All @@ -23,11 +24,15 @@ class ListedPages implements Flushable
public static function get_classes($includeSubclasses = true)
{
$classes = [];
$cache = self::get_cache();

// retrieve classes from cache
if ($cache->has(self::get_cache_key('Classes'))) {
$classes = $cache->get(self::get_cache_key('Classes'));
$cache = self::get_cache();
$cacheKey = self::get_cache_key('Classes');
if (!$cache->has($cacheKey)) {
self::build_cache();
}
if ($cache->has($cacheKey)) {
$classes = $cache->get($cacheKey);
}

if ($includeSubclasses) {
Expand Down Expand Up @@ -136,9 +141,8 @@ public static function get_common_class($classes = null)

protected static function add_subclasses($classes)
{
$cache = self::get_cache();

// retrieve classes from cache
$cache = self::get_cache();
$cacheKey = self::get_cache_key('IncludeSubclasses', $classes);
if ($cache->has($cacheKey)) {

Expand Down Expand Up @@ -230,7 +234,10 @@ public static function validate_classes($classes)
public static function flush()
{
self::get_cache()->clear();

self::build_cache();
}

private static function build_cache() {
// build pages cache
$pages = [];
$classes = ClassInfo::subclassesFor(SiteTree::class);
Expand All @@ -244,7 +251,6 @@ public static function flush()
$cacheKey = self::get_cache_key('Classes');
$cache->set($cacheKey, $pages);
self::add_subclasses($pages);

}

private static function get_cache() {
Expand Down
16 changes: 12 additions & 4 deletions src/ListingsRoots.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Fromholdio\Listings\Extensions\ListingsRootPageExtension;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Middleware\FlushMiddleware;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Flushable;
Expand All @@ -22,11 +23,15 @@ class ListingsRoots implements Flushable
public static function get_classes($includeSubclasses = true)
{
$classes = [];
$cache = self::get_cache();

// retrieve classes from cache
if ($cache->has(self::get_cache_key('Classes'))) {
$classes = $cache->get(self::get_cache_key('Classes'));
$cache = self::get_cache();
$cacheKey = self::get_cache_key('Classes');
if (!$cache->has($cacheKey)) {
self::build_cache();
}
if ($cache->has($cacheKey)) {
$classes = $cache->get($cacheKey);
}

if ($includeSubclasses) {
Expand Down Expand Up @@ -218,7 +223,10 @@ public static function validate_classes($classes)
public static function flush()
{
self::get_cache()->clear();

self::build_cache();
}

private static function build_cache() {
// build pages cache
$pages = [];
$classes = ClassInfo::subclassesFor(SiteTree::class);
Expand Down

0 comments on commit 0b6a07f

Please sign in to comment.