Cache the tree nodes list generated for StructuralEntityType.

This commit is contained in:
Jan Böhmer 2019-08-20 12:34:43 +02:00
parent 76abef57be
commit f75f17c92b
6 changed files with 153 additions and 7 deletions

View file

@ -2,7 +2,7 @@ framework:
cache:
# Put the unique name of your app here: the prefix seed
# is used to compute stable namespaces for cache keys.
#prefix_seed: your_vendor_name/app_name
prefix_seed: Part-DB/Part-DB
# The app cache caches to the filesystem by default.
# Other options include:
@ -15,5 +15,8 @@ framework:
#app: cache.adapter.apcu
# Namespaced pools use the above "app" backend by default
#pools:
#my.dedicated.cache: ~
pools:
# Here all things related to cache the tree structures
tree.cache:
adapter: cache.app
tags: true

View file

@ -37,6 +37,12 @@ services:
tags:
- { name: "doctrine.orm.entity_listener" }
tree_invalidation_listener:
class: App\EntityListeners\TreeCacheInvalidationListener
tags:
- { name: doctrine.orm.entity_listener }
App\Command\UpdateExchangeRatesCommand:
arguments:
$base_current: '%default_currency%'

View file

@ -72,6 +72,8 @@ use Symfony\Component\Serializer\Annotation\Groups;
*
* @ORM\MappedSuperclass(repositoryClass="App\Repository\StructuralDBElementRepository")
*
* @ORM\EntityListeners({"App\Security\EntityListeners\ElementPermissionListener", "App\EntityListeners\TreeCacheInvalidationListener"})
*
* @UniqueEntity(fields={"name", "parent"}, ignoreNull=false, message="structural.entity.unique_name")
*/
abstract class StructuralDBElement extends AttachmentContainingDBElement

View file

@ -0,0 +1,84 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\EntityListeners;
use App\Entity\Base\DBElement;
use App\Entity\Base\StructuralDBElement;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
class TreeCacheInvalidationListener
{
protected $cache;
public function __construct(TagAwareCacheInterface $treeCache)
{
$this->cache = $treeCache;
}
/**
* @ORM\PostUpdate()
* @ORM\PostPersist()
* @ORM\PostRemove()
*
* @param DBElement $element
* @param LifecycleEventArgs $event
*/
public function invalidate(DBElement $element, LifecycleEventArgs $event)
{
//If an element was changed, then invalidate all cached trees with this element class
if ($element instanceof StructuralDBElement) {
$secure_class_name = str_replace("\\", '_', get_class($element));
$this->cache->invalidateTags([$secure_class_name]);
}
//If a user change, then invalidate all cached trees for him
if ($element instanceof User) {
$tag = "user_" . $element->getUsername();
$this->cache->invalidateTags([$tag]);
}
/* If any group change, then invalidate all cached trees. Users Permissions can be inherited from groups,
so a change in any group can cause big permisssion changes for users. So to be sure, invalidate all trees */
if($element instanceof Group) {
$tag = "groups";
$this->cache->invalidateTags([$tag]);
}
}
}

View file

@ -35,6 +35,7 @@ namespace App\Form\Type;
use App\Entity\Base\StructuralDBElement;
use App\Entity\Parts\Storelocation;
use App\Repository\StructuralDBElementRepository;
use App\Services\TreeBuilder;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
@ -54,10 +55,13 @@ class StructuralEntityType extends AbstractType
{
protected $em;
protected $options;
/** @var TreeBuilder */
protected $builder;
public function __construct(EntityManagerInterface $em)
public function __construct(EntityManagerInterface $em, TreeBuilder $builder)
{
$this->em = $em;
$this->builder = $builder;
}
public function configureOptions(OptionsResolver $resolver)
@ -122,9 +126,12 @@ class StructuralEntityType extends AbstractType
{
$this->options = $options;
$choices = $this->builder->typeToNodesList($options['class'], null);
/** @var StructuralDBElementRepository $repo */
$repo = $this->em->getRepository($options['class']);
$choices = $repo->toNodesList(null);
/*$repo = $this->em->getRepository($options['class']);
$choices = $repo->toNodesList(null); */
return $choices;
}

View file

@ -37,7 +37,16 @@ use App\Entity\Base\StructuralDBElement;
use App\Helpers\TreeViewNode;
use App\Repository\StructuralDBElementRepository;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
@ -50,12 +59,17 @@ class TreeBuilder
protected $url_generator;
protected $em;
protected $translator;
protected $cache;
protected $security;
public function __construct(EntityURLGenerator $URLGenerator, EntityManagerInterface $em, TranslatorInterface $translator)
public function __construct(EntityURLGenerator $URLGenerator, EntityManagerInterface $em,
TranslatorInterface $translator, TagAwareCacheInterface $treeCache, Security $security)
{
$this->url_generator = $URLGenerator;
$this->em = $em;
$this->translator = $translator;
$this->security = $security;
$this->cache = $treeCache;
}
/**
@ -149,4 +163,34 @@ class TreeBuilder
return $array;
}
/**
* Gets a flattened hierachical tree. Useful for generating option lists.
* In difference to the Repository Function, the results here are cached.
* @param string $class_name The class name of the entity you want to retrieve.
* @param StructuralDBElement|null $parent This entity will be used as root element. Set to null, to use global root
* @return StructuralDBElement[] A flattened list containing the tree elements.
* @throws \Psr\Cache\InvalidArgumentException
*/
public function typeToNodesList(string $class_name, ?StructuralDBElement $parent = null): array
{
$username = $this->security->getUser()->getUsername();
$parent_id = $parent != null ? $parent->getID() : "0";
// Backslashes are not allowed in cache keys
$secure_class_name = str_replace("\\", '_', $class_name);
$key = "list_" . $username . "_" . $secure_class_name . $parent_id;
$ret = $this->cache->get($key, function (ItemInterface $item) use ($class_name, $parent, $secure_class_name, $username) {
// Invalidate when groups, a element with the class or the user changes
$item->tag(['groups', 'tree_list', 'user_' . $username, $secure_class_name]);
/**
* @var $repo StructuralDBElementRepository
*/
$repo = $this->em->getRepository($class_name);
return $repo->toNodesList($parent);
});
return $ret;
}
}