mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-08-30 06:39:40 +02:00
Refactored cache tags and invalidation
This commit is contained in:
parent
08a1ce5f64
commit
b7af08503c
8 changed files with 159 additions and 63 deletions
70
src/Services/Cache/ElementCacheTagGenerator.php
Normal file
70
src/Services/Cache/ElementCacheTagGenerator.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 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 Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\Services\Cache;
|
||||
|
||||
use Doctrine\Persistence\Proxy;
|
||||
|
||||
/**
|
||||
* The purpose of this class is to generate cache tags for elements.
|
||||
* E.g. to easily invalidate all caches for a given element type.
|
||||
*/
|
||||
class ElementCacheTagGenerator
|
||||
{
|
||||
private array $cache = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a cache tag for the given element type, which can be used to invalidate all caches for this element type.
|
||||
* @param string|object $element
|
||||
* @return string
|
||||
*/
|
||||
public function getElementTypeCacheTag(string|object $element): string
|
||||
{
|
||||
//Ensure that the given element is a class name
|
||||
if (is_object($element)) {
|
||||
$element = get_class($element);
|
||||
} else { //And that the class exists
|
||||
if (!class_exists($element)) {
|
||||
throw new \InvalidArgumentException("The given class '$element' does not exist!");
|
||||
}
|
||||
}
|
||||
|
||||
//Check if the tag is already cached
|
||||
if (isset($this->cache[$element])) {
|
||||
return $this->cache[$element];
|
||||
}
|
||||
|
||||
//If the element is a proxy, then get the real class name of the underlying object
|
||||
if ($element instanceof Proxy || str_starts_with($element, 'Proxies\\')) {
|
||||
$element = get_parent_class($element);
|
||||
}
|
||||
|
||||
//Replace all backslashes with underscores to prevent problems with the cache and save the result
|
||||
$this->cache[$element] = str_replace('\\', '_', $element);
|
||||
return $this->cache[$element];
|
||||
}
|
||||
}
|
66
src/Services/Cache/UserCacheKeyGenerator.php
Normal file
66
src/Services/Cache/UserCacheKeyGenerator.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2022 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 Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Cache;
|
||||
|
||||
use App\Entity\UserSystem\User;
|
||||
use Locale;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
/**
|
||||
* Purpose of this service is to generate a key unique for a user, to use in Cache keys and tags.
|
||||
*/
|
||||
class UserCacheKeyGenerator
|
||||
{
|
||||
public function __construct(protected Security $security, protected RequestStack $requestStack)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a key for the given user.
|
||||
*
|
||||
* @param User|null $user The user for which the key should be generated. When set to null, the currently logged in
|
||||
* user is used.
|
||||
*/
|
||||
public function generateKey(?User $user = null): string
|
||||
{
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
//Retrieve the locale from the request, if possible, otherwise use the default locale
|
||||
$locale = $request instanceof Request ? $request->getLocale() : Locale::getDefault();
|
||||
|
||||
//If no user was specified, use the currently used one.
|
||||
if (!$user instanceof User) {
|
||||
$user = $this->security->getUser();
|
||||
}
|
||||
|
||||
//If the user is null, then treat it as anonymous user.
|
||||
//When the anonymous user is passed as user then use this path too.
|
||||
if (!($user instanceof User) || User::ID_ANONYMOUS === $user->getID()) {
|
||||
return 'user$_'.User::ID_ANONYMOUS;
|
||||
}
|
||||
|
||||
//In the most cases we can just use the username (its unique)
|
||||
return 'user_'.$user->getUsername().'_'.$locale;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue