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

@ -22,14 +22,13 @@ declare(strict_types=1);
namespace App\Services\Trees;
use App\Entity\Attachments\AttachmentContainingDBElement;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Repository\AttachmentContainingDBElementRepository;
use App\Repository\DBElementRepository;
use App\Repository\StructuralDBElementRepository;
use App\Services\UserSystem\UserCacheKeyGenerator;
use App\Services\Cache\ElementCacheTagGenerator;
use App\Services\Cache\UserCacheKeyGenerator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
@ -40,8 +39,12 @@ use Symfony\Contracts\Cache\TagAwareCacheInterface;
*/
class NodesListBuilder
{
public function __construct(protected EntityManagerInterface $em, protected TagAwareCacheInterface $cache, protected UserCacheKeyGenerator $keyGenerator)
{
public function __construct(
protected EntityManagerInterface $em,
protected TagAwareCacheInterface $cache,
protected UserCacheKeyGenerator $keyGenerator,
protected ElementCacheTagGenerator $tagGenerator,
) {
}
/**
@ -50,9 +53,9 @@ class NodesListBuilder
*
* @template T of AbstractDBElement
*
* @param string $class_name the class name of the entity you want to retrieve
* @param string $class_name the class name of the entity you want to retrieve
* @phpstan-param class-string<T> $class_name
* @param AbstractStructuralDBElement|null $parent This entity will be used as root element. Set to null, to use global root
* @param AbstractStructuralDBElement|null $parent This entity will be used as root element. Set to null, to use global root
*
* @return AbstractDBElement[] a flattened list containing the tree elements
* @phpstan-return list<T>
@ -86,7 +89,7 @@ class NodesListBuilder
{
$parent_id = $parent instanceof AbstractStructuralDBElement ? $parent->getID() : '0';
// Backslashes are not allowed in cache keys
$secure_class_name = str_replace('\\', '_', $class_name);
$secure_class_name = $this->tagGenerator->getElementTypeCacheTag($class_name);
$key = 'list_'.$this->keyGenerator->generateKey().'_'.$secure_class_name.$parent_id;
return $this->cache->get($key, function (ItemInterface $item) use ($class_name, $parent, $secure_class_name) {
@ -105,7 +108,7 @@ class NodesListBuilder
* The value is cached for performance reasons.
*
* @template T of AbstractStructuralDBElement
* @param T $element
* @param T $element
* @return AbstractStructuralDBElement[]
*
* @phpstan-return list<T>

View file

@ -22,9 +22,7 @@ declare(strict_types=1);
namespace App\Services\Trees;
use Symfony\Bundle\SecurityBundle\Security;
use App\Entity\Attachments\AttachmentType;
use App\Entity\ProjectSystem\Project;
use App\Entity\LabelSystem\LabelProfile;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
@ -34,10 +32,12 @@ use App\Entity\Parts\Part;
use App\Entity\Parts\StorageLocation;
use App\Entity\Parts\Supplier;
use App\Entity\PriceInformations\Currency;
use App\Entity\ProjectSystem\Project;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use App\Helpers\Trees\TreeViewNode;
use App\Services\UserSystem\UserCacheKeyGenerator;
use App\Services\Cache\UserCacheKeyGenerator;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

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);
});
}