Refactored cache tags and invalidation

This commit is contained in:
Jan Böhmer 2023-11-29 20:49:16 +01:00
parent 08a1ce5f64
commit b7af08503c
8 changed files with 159 additions and 63 deletions

View file

@ -25,17 +25,18 @@ namespace App\Services\Trees;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\ProjectSystem\Project;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\StorageLocation;
use App\Entity\Parts\Supplier;
use App\Entity\ProjectSystem\Project;
use App\Helpers\Trees\TreeViewNode;
use App\Helpers\Trees\TreeViewNodeIterator;
use App\Repository\StructuralDBElementRepository;
use App\Services\Cache\ElementCacheTagGenerator;
use App\Services\Cache\UserCacheKeyGenerator;
use App\Services\EntityURLGenerator;
use App\Services\UserSystem\UserCacheKeyGenerator;
use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;
use RecursiveIteratorIterator;
@ -51,25 +52,37 @@ use function count;
*/
class TreeViewGenerator
{
public function __construct(protected EntityURLGenerator $urlGenerator, protected EntityManagerInterface $em, protected TagAwareCacheInterface $cache,
protected UserCacheKeyGenerator $keyGenerator, protected TranslatorInterface $translator, private UrlGeneratorInterface $router,
protected bool $rootNodeExpandedByDefault, protected bool $rootNodeEnabled)
{
public function __construct(
protected EntityURLGenerator $urlGenerator,
protected EntityManagerInterface $em,
protected TagAwareCacheInterface $cache,
protected ElementCacheTagGenerator $tagGenerator,
protected UserCacheKeyGenerator $keyGenerator,
protected TranslatorInterface $translator,
private UrlGeneratorInterface $router,
protected bool $rootNodeExpandedByDefault,
protected bool $rootNodeEnabled,
) {
}
/**
* Gets a TreeView list for the entities of the given class.
*
* @param string $class The class for which the treeView should be generated
* @param AbstractStructuralDBElement|null $parent The root nodes in the tree should have this element as parent (use null, if you want to get all entities)
* @param string $mode The link type that will be generated for the hyperlink section of each node (see EntityURLGenerator for possible values).
* @param string $class The class for which the treeView should be generated
* @param AbstractStructuralDBElement|null $parent The root nodes in the tree should have this element as parent (use null, if you want to get all entities)
* @param string $mode The link type that will be generated for the hyperlink section of each node (see EntityURLGenerator for possible values).
* Set to empty string, to disable href field.
* @param AbstractDBElement|null $selectedElement The element that should be selected. If set to null, no element will be selected.
* @param AbstractDBElement|null $selectedElement The element that should be selected. If set to null, no element will be selected.
*
* @return TreeViewNode[] an array of TreeViewNode[] elements of the root elements
*/
public function getTreeView(string $class, ?AbstractStructuralDBElement $parent = null, string $mode = 'list_parts', ?AbstractDBElement $selectedElement = null): array
{
public function getTreeView(
string $class,
?AbstractStructuralDBElement $parent = null,
string $mode = 'list_parts',
?AbstractDBElement $selectedElement = null
): array {
$head = [];
$href_type = $mode;
@ -110,7 +123,7 @@ class TreeViewGenerator
}
if ($item->getNodes() !== null && $item->getNodes() !== []) {
$item->addTag((string) count($item->getNodes()));
$item->addTag((string)count($item->getNodes()));
}
if ($href_type !== '' && null !== $item->getId()) {
@ -155,12 +168,12 @@ class TreeViewGenerator
{
$icon = "fa-fw fa-treeview fa-solid ";
return match ($class) {
Category::class => $icon . 'fa-tags',
StorageLocation::class => $icon . 'fa-cube',
Footprint::class => $icon . 'fa-microchip',
Manufacturer::class => $icon . 'fa-industry',
Supplier::class => $icon . 'fa-truck',
Project::class => $icon . 'fa-archive',
Category::class => $icon.'fa-tags',
StorageLocation::class => $icon.'fa-cube',
Footprint::class => $icon.'fa-microchip',
Manufacturer::class => $icon.'fa-industry',
Supplier::class => $icon.'fa-truck',
Project::class => $icon.'fa-archive',
default => null,
};
}
@ -170,8 +183,8 @@ class TreeViewGenerator
* Gets a tree of TreeViewNode elements. The root elements has $parent as parent.
* The treeview is generic, that means the href are null and ID values are set.
*
* @param string $class The class for which the tree should be generated
* @param AbstractStructuralDBElement|null $parent the parent the root elements should have
* @param string $class The class for which the tree should be generated
* @param AbstractStructuralDBElement|null $parent the parent the root elements should have
*
* @return TreeViewNode[]
*/
@ -192,13 +205,12 @@ class TreeViewGenerator
return $repo->getGenericNodeTree($parent);
}
$secure_class_name = str_replace('\\', '_', $class);
$secure_class_name = $this->tagGenerator->getElementTypeCacheTag($class);
$key = 'treeview_'.$this->keyGenerator->generateKey().'_'.$secure_class_name;
return $this->cache->get($key, function (ItemInterface $item) use ($repo, $parent, $secure_class_name) {
// Invalidate when groups, an element with the class or the user changes
$item->tag(['groups', 'tree_treeview', $this->keyGenerator->generateKey(), $secure_class_name]);
return $repo->getGenericNodeTree($parent);
});
}