Skip to content

Commit

Permalink
cache
Browse files Browse the repository at this point in the history
  • Loading branch information
matfish3 committed Aug 31, 2022
1 parent e2c6f15 commit cafb88c
Showing 1 changed file with 67 additions and 43 deletions.
110 changes: 67 additions & 43 deletions src/EntryMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace matfish\EntryMeta;

use Closure;
use Craft;
use craft\base\Element;
use craft\base\Plugin;
Expand Down Expand Up @@ -96,80 +97,90 @@ protected function getOptions()

protected function getEnabled(): array
{
$res = [];
$migrator = new MetadataColumnMigrator();
$detector = new MetadataTableDetector();
foreach (array_keys(ClassesMap::LOOK_UP) as $key) {

try {
$table = $detector->detect($key);
} catch (\Exception $e) {
continue;
}
return $this->eMcache('emEnabled', function () {
$res = [];
$migrator = new MetadataColumnMigrator();
$detector = new MetadataTableDetector();
foreach (array_keys(ClassesMap::LOOK_UP) as $key) {

try {
$table = $detector->detect($key);
} catch (\Exception $e) {
continue;
}

if ($migrator->columnExists($table)) {
$res[] = $key;
if ($migrator->columnExists($table)) {
$res[] = $key;
}
}
}

return $res;
return $res;
});
}

private function getActiveRecordFromElementClass($elClass)
{
foreach ($this->getAllEnabled() as $val) {
if ($val[1] === $elClass) {
return $val[0];
return $this->eMcache('emActiveRecordFromElementClass' . $elClass, function () use ($elClass) {
foreach ($this->getAllEnabled() as $val) {
if ($val[1] === $elClass) {
return $val[0];
}
}
}

throw new Exception("Cannot retrieve active record class for element {$elClass}");
throw new Exception("Cannot retrieve active record class for element {$elClass}");
});
}

public function getAllEnabled()
{
$res = [];
return $this->eMcache('emAllEnabled', function () {
$res = [];

foreach ($this->getEnabled() as $key) {
$res[] = ClassesMap::LOOK_UP[$key];
}
foreach ($this->getEnabled() as $key) {
$res[] = ClassesMap::LOOK_UP[$key];
}

foreach ($this->settings->enabledForCustom as $val) {
$res[] = array_reverse($val);
}
foreach ($this->settings->enabledForCustom as $val) {
$res[] = array_reverse($val);
}

return $res;
return $res;
});
}

public function getEnabledActiveRecords(): array
{
$res = [];
return $this->eMcache('emEnabledActiveRecords', function () {
$res = [];

foreach ($this->getEnabled() as $key) {
$res[] = ClassesMap::LOOK_UP[$key][0];
}
foreach ($this->getEnabled() as $key) {
$res[] = ClassesMap::LOOK_UP[$key][0];
}

foreach ($this->settings->enabledForCustom as $val) {
$res[] = $val[1];
}
foreach ($this->settings->enabledForCustom as $val) {
$res[] = $val[1];
}

return $res;
return $res;
});
}


private function getEnabledElements(): array
{
$res = [];
return $this->eMcache('emEnabledElements', function () {
$res = [];

foreach ($this->getEnabled() as $key) {
$res[] = ClassesMap::LOOK_UP[$key][1];
}
foreach ($this->getEnabled() as $key) {
$res[] = ClassesMap::LOOK_UP[$key][1];
}

foreach ($this->settings->enabledForCustom as $val) {
$res[] = $val[0];
}
foreach ($this->settings->enabledForCustom as $val) {
$res[] = $val[0];
}

return $res;
return $res;
});
}

public function afterSaveSettings(): void
Expand All @@ -189,6 +200,12 @@ public function afterSaveSettings(): void
$migrator->add($table);
}

$cache = \Craft::$app->cache;
$cache->delete('emEnabledElements');
$cache->delete('emEnabledActiveRecords');
$cache->delete('emAllEnabled');
$cache->delete('emEnabled');

parent::afterSaveSettings();
}

Expand Down Expand Up @@ -242,4 +259,11 @@ function (DefineHtmlEvent $event) {

}

private function eMcache($handle, Closure $callback)
{
return \Craft::$app->cache->getOrSet($handle, function () use ($handle, $callback) {
return $callback($handle);
}, 60 * 60 * 24 * 365);
}

}

0 comments on commit cafb88c

Please sign in to comment.