Skip to content

Commit

Permalink
improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ad3n committed Jan 23, 2022
1 parent ad0bd2d commit 9b4b1e8
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 18 deletions.
1 change: 1 addition & 0 deletions config/packages/cache.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ framework:
pools:
doctrine.result_cache_pool:
adapter: cache.app
provider: snc_redis.cache
doctrine.system_cache_pool:
adapter: cache.system
10 changes: 9 additions & 1 deletion lib/EventSubscriber/LogoutSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace KejawenLab\ApiSkeleton\EventSubscriber;

use KejawenLab\ApiSkeleton\Admin\AdminContext;
use KejawenLab\ApiSkeleton\Setting\Model\SettingInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand All @@ -15,7 +17,7 @@
*/
final class LogoutSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly UrlGeneratorInterface $urlGenerator)
public function __construct(private readonly UrlGeneratorInterface $urlGenerator, private readonly CacheItemPoolInterface $cache)
{
}

Expand All @@ -25,6 +27,12 @@ public function redirect(LogoutEvent $event): void
return;
}

$this->cache->deleteItem(SettingInterface::CACHE_ID_CACHE_LIFETIME);
$this->cache->deleteItem(SettingInterface::CACHE_ID_PAGE_FIELD);
$this->cache->deleteItem(SettingInterface::CACHE_ID_PER_PAGE_FIELD);
$this->cache->deleteItem(SettingInterface::CACHE_ID_PER_PAGE);
$this->cache->deleteItem(SettingInterface::CACHE_ID_MAX_API_PER_USER);

$event->setResponse(new RedirectResponse($this->urlGenerator->generate(AdminContext::ADMIN_ROUTE)));
}

Expand Down
23 changes: 19 additions & 4 deletions lib/Repository/PermissionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use KejawenLab\ApiSkeleton\Security\Model\MenuInterface;
use KejawenLab\ApiSkeleton\Security\Model\PermissionInterface;
use KejawenLab\ApiSkeleton\Security\Model\PermissionRepositoryInterface;
use KejawenLab\ApiSkeleton\SemartApiSkeleton;
use Swoole\Coroutine;

/**
Expand Down Expand Up @@ -80,8 +81,15 @@ public function findPermissions(GroupInterface $group, iterable $menus): iterabl
/**
* @return Iterator<MenuInterface|null>
*/
public function findAllowedMenusByGroup(GroupInterface $group, bool $parentOnly = false): iterable
public function findAllowedMenusByGroup(GroupInterface $group, bool $parentOnly = false, bool $maxCache = false): iterable
{
$cacheLifetime = self::MICRO_CACHE;
$cacheKey = sprintf("%s_%s_%s", sha1(self::class), sha1(__METHOD__), $group->getId());
if ($maxCache) {
$cacheLifetime = SemartApiSkeleton::STATIC_CACHE_LIFETIME;
$cacheKey = sprintf('%s_max', $cacheKey);
}

$queryBuilder = $this->createQueryBuilder('o');
$queryBuilder->innerJoin('o.group', 'g');
$queryBuilder->innerJoin('o.menu', 'm');
Expand All @@ -99,7 +107,7 @@ public function findAllowedMenusByGroup(GroupInterface $group, bool $parentOnly

$query = $queryBuilder->getQuery();
$query->useQueryCache(true);
$query->enableResultCache(self::MICRO_CACHE, sprintf("%s_%s_%s", sha1(self::class), sha1(__METHOD__), $group->getId()));
$query->enableResultCache($cacheLifetime, $cacheKey);

/** @var PermissionInterface[] $permissions */
$permissions = $query->getResult();
Expand All @@ -111,8 +119,15 @@ public function findAllowedMenusByGroup(GroupInterface $group, bool $parentOnly
/**
* @return Iterator<MenuInterface|null>
*/
public function findAllowedChildMenusByGroupAndMenu(GroupInterface $group, MenuInterface $menu): iterable
public function findAllowedChildMenusByGroupAndMenu(GroupInterface $group, MenuInterface $menu, bool $maxCache = false): iterable
{
$cacheLifetime = self::MICRO_CACHE;
$cacheKey = sprintf("%s_%s_%s_%s", sha1(self::class), sha1(__METHOD__), $group->getId(), $menu->getId());
if ($maxCache) {
$cacheLifetime = SemartApiSkeleton::STATIC_CACHE_LIFETIME;
$cacheKey = sprintf('%s_max', $cacheKey);
}

$queryBuilder = $this->createQueryBuilder('o');
$queryBuilder->innerJoin('o.group', 'g');
$queryBuilder->innerJoin('o.menu', 'm');
Expand All @@ -129,7 +144,7 @@ public function findAllowedChildMenusByGroupAndMenu(GroupInterface $group, MenuI

$query = $queryBuilder->getQuery();
$query->useQueryCache(true);
$query->enableResultCache(self::MICRO_CACHE, sprintf("%s_%s_%s_%s", sha1(self::class), sha1(__METHOD__), $group->getId(), $menu->getId()));
$query->enableResultCache($cacheLifetime, $cacheKey);

/** @var PermissionInterface[] $permissions */
$permissions = $query->getResult();
Expand Down
4 changes: 2 additions & 2 deletions lib/Security/Model/PermissionRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public function findPermission(GroupInterface $group, MenuInterface $menu, bool

public function findPermissions(GroupInterface $group, array $menus): iterable;

public function findAllowedMenusByGroup(GroupInterface $group, bool $parentOnly = false): iterable;
public function findAllowedMenusByGroup(GroupInterface $group, bool $parentOnly = false, bool $maxCache = false): iterable;

public function findAllowedChildMenusByGroupAndMenu(GroupInterface $group, MenuInterface $menu): iterable;
public function findAllowedChildMenusByGroupAndMenu(GroupInterface $group, MenuInterface $menu, bool $maxCache = false): iterable;

public function removeByGroup(GroupInterface $group): void;

Expand Down
6 changes: 3 additions & 3 deletions lib/Security/Service/MenuService.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getParentMenu(): iterable
return [];
}

return $this->permissionRepository->findAllowedMenusByGroup($group, true);
return $this->permissionRepository->findAllowedMenusByGroup($group, true, true);
}

public function hasChildMenu(MenuInterface $menu): bool
Expand All @@ -51,7 +51,7 @@ public function hasChildMenu(MenuInterface $menu): bool
return false;
}

$childMenus = $this->permissionRepository->findAllowedChildMenusByGroupAndMenu($group, $menu);
$childMenus = $this->permissionRepository->findAllowedChildMenusByGroupAndMenu($group, $menu, true);

return [] !== iterator_to_array($childMenus, false);
}
Expand All @@ -62,7 +62,7 @@ public function getChildsMenu(MenuInterface $menu): iterable
return [];
}

return $this->permissionRepository->findAllowedChildMenusByGroupAndMenu($group, $menu);
return $this->permissionRepository->findAllowedChildMenusByGroupAndMenu($group, $menu, true);
}

private function getGroup(): ?GroupInterface
Expand Down
8 changes: 5 additions & 3 deletions lib/SemartApiSkeleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
*/
final class SemartApiSkeleton
{
final public const STATIC_CACHE_LIFETIME = 1027;

final public const CODENAME = 'Dodol Duren';

final public const VERSION = '5.7.7';
final public const VERSION = '5.8.0';

final public const VERSION_MAYOR = 50000;

final public const VERSION_MINOR = 700;
final public const VERSION_MINOR = 800;

final public const VERSION_PATCH = 7;
final public const VERSION_PATCH = 0;

public static function getVersionNumber(): int
{
Expand Down
10 changes: 10 additions & 0 deletions lib/Setting/Model/SettingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
*/
interface SettingInterface extends EntityInterface
{
final public const CACHE_ID_CACHE_LIFETIME = 'de35124f15a4ef9f5a50ce1dd9027556e12a221d';

final public const CACHE_ID_PAGE_FIELD = 'd5c8846a9629410a4930ecbe67e075dfa02020ea';

final public const CACHE_ID_PER_PAGE_FIELD = '964c2f9a9b4747561a06c6064f3ccd874fea04ab';

final public const CACHE_ID_PER_PAGE = '872bea19cf26dc5b03267f3327e888bf0993d702';

final public const CACHE_ID_MAX_API_PER_USER = '95afbfd9c39da2360855134e2bc998deb1bc4c20';

public function getGroup(): ?string;

public function getParameter(): ?string;
Expand Down
68 changes: 63 additions & 5 deletions lib/Setting/SettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace KejawenLab\ApiSkeleton\Setting;

use KejawenLab\ApiSkeleton\Pagination\AliasHelper;
use KejawenLab\ApiSkeleton\SemartApiSkeleton;
use KejawenLab\ApiSkeleton\Service\AbstractService;
use KejawenLab\ApiSkeleton\Service\Model\ServiceInterface;
use KejawenLab\ApiSkeleton\Setting\Model\SettingInterface;
use KejawenLab\ApiSkeleton\Setting\Model\SettingRepositoryInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Messenger\MessageBusInterface;

/**
Expand All @@ -21,6 +23,7 @@ public function __construct(
SettingRepositoryInterface $repository,
AliasHelper $aliasHelper,
private readonly SettingGroupFactory $groupFactory,
private readonly CacheItemPoolInterface $cache,
) {
parent::__construct($messageBus, $repository, $aliasHelper);
}
Expand Down Expand Up @@ -49,26 +52,81 @@ public function getPublicSetting(string $id): ?SettingInterface

public function getCacheLifetime(): int
{
return (int) $this->getSetting('CACHE_LIFETIME')->getValue();
$cache = $this->cache->getItem(SettingInterface::CACHE_ID_CACHE_LIFETIME);
if ($cache->isHit()) {
return $cache->get();
}

$setting = (int) $this->getSetting('CACHE_LIFETIME')->getValue();

$cache->set($setting);
$cache->expiresAfter(SemartApiSkeleton::STATIC_CACHE_LIFETIME);
$this->cache->save($cache);

return $setting;
}

public function getPageField(): ?string
{
return $this->getSetting('PAGE_FIELD')->getValue();
$cache = $this->cache->getItem(SettingInterface::CACHE_ID_PAGE_FIELD);
if ($cache->isHit()) {
return $cache->get();
}

$setting = $this->getSetting('PAGE_FIELD')->getValue();

$cache->set($setting);
$cache->expiresAfter(SemartApiSkeleton::STATIC_CACHE_LIFETIME);
$this->cache->save($cache);

return $setting;
}

public function getPerPageField(): ?string
{
return $this->getSetting('PER_PAGE_FIELD')->getValue();
$cache = $this->cache->getItem(SettingInterface::CACHE_ID_PER_PAGE_FIELD);
if ($cache->isHit()) {
return $cache->get();
}

$setting = $this->getSetting('PER_PAGE_FIELD')->getValue();

$cache->set($setting);
$cache->expiresAfter(SemartApiSkeleton::STATIC_CACHE_LIFETIME);
$this->cache->save($cache);

return $setting;
}

public function getRecordPerPage(): int
{
return (int) $this->getSetting('PER_PAGE')->getValue();
$cache = $this->cache->getItem(SettingInterface::CACHE_ID_PER_PAGE);
if ($cache->isHit()) {
return $cache->get();
}

$setting = (int) $this->getSetting('PER_PAGE')->getValue();

$cache->set($setting);
$cache->expiresAfter(SemartApiSkeleton::STATIC_CACHE_LIFETIME);
$this->cache->save($cache);

return $setting;
}

public function getMaxApiPerUser(): int
{
return (int) $this->getSetting('MAX_API_PER_USER')->getValue();
$cache = $this->cache->getItem(SettingInterface::CACHE_ID_MAX_API_PER_USER);
if ($cache->isHit()) {
return $cache->get();
}

$setting = (int) $this->getSetting('MAX_API_PER_USER')->getValue();

$cache->set($setting);
$cache->expiresAfter(SemartApiSkeleton::STATIC_CACHE_LIFETIME);
$this->cache->save($cache);

return $setting;
}
}

0 comments on commit 9b4b1e8

Please sign in to comment.