Skip to content

Commit

Permalink
Merge pull request #123 from KejawenLab/main
Browse files Browse the repository at this point in the history
Improve cache management
  • Loading branch information
ad3n authored Jan 30, 2022
2 parents daf77a2 + cbcc17f commit de89496
Show file tree
Hide file tree
Showing 54 changed files with 400 additions and 372 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ RUN apk add --no-cache libpng-dev openssl-dev nghttp2-dev hiredis-dev rabbitmq-c

## Install Pecl Extension
RUN pecl channel-update pecl.php.net
RUN pecl install igbinary inotify apcu
RUN pecl install https://github.com/0x450x6c/php-amqp/raw/7323b3c9cc2bcb8343de9bb3c2f31f6efbc8894b/amqp-1.10.3.tgz
RUN pecl install igbinary inotify apcu amqp
RUN pecl bundle redis && cd redis && phpize && ./configure --enable-redis-igbinary && make && make install
RUN docker-php-ext-enable igbinary redis inotify amqp apcu

Expand Down
3 changes: 3 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ tasks:
insight:
cmds:
- docker-compose -f docker-compose.yml exec app bash -c "php vendor/bin/phpinsights"
style:
cmds:
- docker-compose -f docker-compose.yml exec app bash -c "php vendor/bin/php-cs-fixer fix lib"
clean:
cmds:
- docker-compose -f docker-compose.yml exec app bash -c "php bin/console cache:clear"
Expand Down
75 changes: 19 additions & 56 deletions lib/Admin/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace KejawenLab\ApiSkeleton\Admin\Controller;

use DateInterval;
use InvalidArgumentException;
use KejawenLab\ApiSkeleton\ApiClient\Model\ApiClientInterface;
use KejawenLab\ApiSkeleton\Audit\Audit;
use KejawenLab\ApiSkeleton\Pagination\Paginator;
use KejawenLab\ApiSkeleton\SemartApiSkeleton;
use KejawenLab\ApiSkeleton\Service\Model\ServiceInterface;
use KejawenLab\ApiSkeleton\Util\CacheFactory;
use KejawenLab\ApiSkeleton\Util\StringUtil;
use Psr\Cache\CacheItemPoolInterface;
use ReflectionClass;
use ReflectionProperty;
use SebastianBergmann\ObjectReflector\ObjectReflector;
Expand All @@ -31,15 +29,15 @@ abstract class AbstractController extends Base
public function __construct(
private readonly Request $request,
private readonly ServiceInterface $service,
private readonly CacheItemPoolInterface $cache,
private readonly CacheFactory $cache,
private readonly ?Paginator $paginator = null,
) {
}

protected function renderView(string $view, array $parameters = []): string
{
if (!empty($this->request->query->get(SemartApiSkeleton::DISABLE_VIEW_CACHE_QUERY_STRING))) {
parent::renderView($view, $parameters);
if ($this->cache->isDisableViewCache()) {
return parent::renderView($view, $parameters);
}

$params = [];
Expand All @@ -52,34 +50,14 @@ protected function renderView(string $view, array $parameters = []): string
$params = array_merge($params, $this->request->getSession()->all());
$params = array_merge($params, $this->request->query->all());

$deviceId = $this->getDeviceId();
$key = sprintf('%s_%s_%s', $deviceId, sha1($view), sha1(serialize($params)));

$pool = $this->cache->getItem($deviceId);
$item = $this->cache->getItem($key);
$keys = [];

if ($pool->isHit()) {
if ($item->isHit()) {
return $item->get();
}

$keys = $pool->get();
$key = sprintf('%s_%s', sha1($view), sha1(serialize($params)));
$data = $this->cache->getCache($key, 'view');
if (0 !== count($data)) {
return $data['content'];
}

$item = $this->cache->getItem($key);

$keys = array_merge($keys, [$key => true]);

$pool->set($keys);
$pool->expiresAfter(new DateInterval(SemartApiSkeleton::STATIC_VIEW_CACHE_PERIOD));
$this->cache->save($pool);

$content = parent::renderView($view, $parameters);

$item->set($content);
$item->expiresAfter(new DateInterval(SemartApiSkeleton::STATIC_VIEW_CACHE_PERIOD));
$this->cache->save($item);
$this->cache->setCache($key, 'view', $content, true, SemartApiSkeleton::VIEW_CACHE_PERIOD);

return $content;
}
Expand Down Expand Up @@ -111,14 +89,18 @@ protected function renderList(FormInterface $form, Request $request, ReflectionC
]);
}

private function getDeviceId(): string
private function renderWithAudit(Audit $audit, ReflectionClass $class, string $template): Response
{
$deviceId = $this->request->getSession()->get(SemartApiSkeleton::USER_DEVICE_ID, '');
if ($deviceId === ApiClientInterface::DEVICE_ID) {
return '';
}
$context = StringUtil::lowercase($class->getShortName());
$audits = $audit->toArray();

return $deviceId;
return $this->render(sprintf('%s/%s.html.twig', $context, $template), [
'page_title' => sprintf('sas.page.%s.view', $context),
'context' => $context,
'properties' => $class->getProperties(ReflectionProperty::IS_PRIVATE),
'data' => $audits['entity'],
'audits' => $audits['items'],
]);
}

private function canBeSerialized($variable): bool
Expand Down Expand Up @@ -157,11 +139,6 @@ private function canBeSerialized($variable): bool
return true;
}

/**
* @param $variable
*
* @return array
*/
private function enumerateObjectsAndResources($variable): array
{
$processed = func_get_args()[1] ?? new Context();
Expand Down Expand Up @@ -207,18 +184,4 @@ private function enumerateObjectsAndResources($variable): array

return $result;
}

private function renderWithAudit(Audit $audit, ReflectionClass $class, string $template): Response
{
$context = StringUtil::lowercase($class->getShortName());
$audits = $audit->toArray();

return $this->render(sprintf('%s/%s.html.twig', $context, $template), [
'page_title' => sprintf('sas.page.%s.view', $context),
'context' => $context,
'properties' => $class->getProperties(ReflectionProperty::IS_PRIVATE),
'data' => $audits['entity'],
'audits' => $audits['items'],
]);
}
}
8 changes: 6 additions & 2 deletions lib/Admin/Controller/ApiClient/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
#[Permission(menu: 'AUDIT', actions: [Permission::VIEW])]
final class Audit extends AbstractController
{
public function __construct(private readonly ApiClientService $service, private readonly UserService $userService, private readonly AuditService $audit, private readonly Reader $reader)
{
public function __construct(
private readonly ApiClientService $service,
private readonly UserService $userService,
private readonly AuditService $audit,
private readonly Reader $reader,
) {
}

#[Route(path: '/users/{userId}/api-clients/{id}/audit', name: Audit::class, methods: ['GET'], priority: -255)]
Expand Down
7 changes: 5 additions & 2 deletions lib/Admin/Controller/ApiClient/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
#[Permission(menu: 'APICLIENT', actions: [Permission::VIEW])]
final class Download extends AbstractController
{
public function __construct(private readonly ApiClientService $service, private readonly UserService $userService, private readonly SerializerInterface $serializer)
{
public function __construct(
private readonly ApiClientService $service,
private readonly UserService $userService,
private readonly SerializerInterface $serializer,
) {
}

#[Route(path: '/users/{userId}/api-clients/download', name: Download::class, methods: ['GET'])]
Expand Down
7 changes: 5 additions & 2 deletions lib/Admin/Controller/ApiClient/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
#[Permission(menu: 'APICLIENT', actions: [Permission::VIEW])]
final class Report extends AbstractController
{
public function __construct(private readonly ApiClientRequestService $service, private readonly UserService $userService, private readonly Paginator $paginator)
{
public function __construct(
private readonly ApiClientRequestService $service,
private readonly UserService $userService,
private readonly Paginator $paginator,
) {
}

#[Route(path: '/users/{userId}/api-clients/{id}/logs', name: Report::class, methods: ['GET'])]
Expand Down
4 changes: 2 additions & 2 deletions lib/Admin/Controller/Cron/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use KejawenLab\ApiSkeleton\Cron\CronService;
use KejawenLab\ApiSkeleton\Entity\Cron;
use KejawenLab\ApiSkeleton\Security\Annotation\Permission;
use Psr\Cache\CacheItemPoolInterface;
use KejawenLab\ApiSkeleton\Util\CacheFactory;
use ReflectionClass;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -26,7 +26,7 @@ final class Audit extends AbstractController
public function __construct(
private readonly RequestStack $requestStack,
private readonly CronService $service,
private readonly CacheItemPoolInterface $cache,
private readonly CacheFactory $cache,
private readonly AuditService $audit,
private readonly Reader $reader,
) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Admin/Controller/Cron/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use KejawenLab\ApiSkeleton\Cron\Model\CronInterface;
use KejawenLab\ApiSkeleton\Entity\Cron;
use KejawenLab\ApiSkeleton\Security\Annotation\Permission;
use Psr\Cache\CacheItemPoolInterface;
use KejawenLab\ApiSkeleton\Util\CacheFactory;
use ReflectionClass;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -28,7 +28,7 @@ final class Get extends AbstractController
public function __construct(
private readonly RequestStack $requestStack,
private readonly CronService $service,
private readonly CacheItemPoolInterface $cache,
private readonly CacheFactory $cache,
private readonly AuditService $audit,
private readonly Reader $reader,
) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Admin/Controller/Cron/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use KejawenLab\ApiSkeleton\Form\CronType;
use KejawenLab\ApiSkeleton\Pagination\Paginator;
use KejawenLab\ApiSkeleton\Security\Annotation\Permission;
use Psr\Cache\CacheItemPoolInterface;
use KejawenLab\ApiSkeleton\Util\CacheFactory;
use ReflectionClass;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -26,7 +26,7 @@ final class Main extends AbstractController
public function __construct(
private readonly RequestStack $requestStack,
private readonly CronService $service,
private readonly CacheItemPoolInterface $cache,
private readonly CacheFactory $cache,
private readonly Paginator $paginator,
) {
parent::__construct($this->requestStack->getCurrentRequest(), $this->service, $this->cache, $this->paginator);
Expand Down
4 changes: 2 additions & 2 deletions lib/Admin/Controller/Group/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use KejawenLab\ApiSkeleton\Entity\Group;
use KejawenLab\ApiSkeleton\Security\Annotation\Permission;
use KejawenLab\ApiSkeleton\Security\Service\GroupService;
use Psr\Cache\CacheItemPoolInterface;
use KejawenLab\ApiSkeleton\Util\CacheFactory;
use ReflectionClass;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -26,7 +26,7 @@ final class Audit extends AbstractController
public function __construct(
private readonly RequestStack $requestStack,
private readonly GroupService $service,
private readonly CacheItemPoolInterface $cache,
private readonly CacheFactory $cache,
private readonly AuditService $audit,
private readonly Reader $reader,
) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Admin/Controller/Group/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use KejawenLab\ApiSkeleton\Security\Annotation\Permission;
use KejawenLab\ApiSkeleton\Security\Model\GroupInterface;
use KejawenLab\ApiSkeleton\Security\Service\GroupService;
use Psr\Cache\CacheItemPoolInterface;
use KejawenLab\ApiSkeleton\Util\CacheFactory;
use ReflectionClass;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -28,7 +28,7 @@ final class Get extends AbstractController
public function __construct(
private readonly RequestStack $requestStack,
private readonly GroupService $service,
private readonly CacheItemPoolInterface $cache,
private readonly CacheFactory $cache,
private readonly AuditService $audit,
private readonly Reader $reader,
) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Admin/Controller/Group/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use KejawenLab\ApiSkeleton\Pagination\Paginator;
use KejawenLab\ApiSkeleton\Security\Annotation\Permission;
use KejawenLab\ApiSkeleton\Security\Service\GroupService;
use Psr\Cache\CacheItemPoolInterface;
use KejawenLab\ApiSkeleton\Util\CacheFactory;
use ReflectionClass;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -26,7 +26,7 @@ final class Main extends AbstractController
public function __construct(
private readonly RequestStack $requestStack,
private readonly GroupService $service,
private readonly CacheItemPoolInterface $cache,
private readonly CacheFactory $cache,
private readonly Paginator $paginator,
) {
parent::__construct($this->requestStack->getCurrentRequest(), $this->service, $this->cache, $this->paginator);
Expand Down
7 changes: 5 additions & 2 deletions lib/Admin/Controller/Me/CreateApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
*/
final class CreateApiClient extends AbstractController
{
public function __construct(private readonly UserProviderFactory $userProviderFactory, private readonly SettingService $setting, private readonly ApiClientService $service)
{
public function __construct(
private readonly UserProviderFactory $userProviderFactory,
private readonly SettingService $setting,
private readonly ApiClientService $service,
) {
}

#[Route(path: '/me/api-clients', name: CreateApiClient::class, methods: ['POST'])]
Expand Down
6 changes: 4 additions & 2 deletions lib/Admin/Controller/Me/DeleteApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use KejawenLab\ApiSkeleton\Admin\AdminContext;
use KejawenLab\ApiSkeleton\ApiClient\ApiClientService;
use KejawenLab\ApiSkeleton\ApiClient\Model\ApiClientInterface;
use KejawenLab\ApiSkeleton\Security\Model\UserInterface;
use KejawenLab\ApiSkeleton\Security\Service\UserProviderFactory;
use KejawenLab\ApiSkeleton\Security\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand All @@ -33,8 +34,9 @@ public function __invoke(Request $request, string $id): Response
}

$user = $this->userProviderFactory->getRealUser($user);
$name = $request->request->get('name');
if ('' === $name) {
/** @var UserInterface $user */
$name = $request->request->get('name', null);
if (empty($name)) {
$this->addFlash('error', 'sas.page.api_client.name_not_provided');

return new RedirectResponse($this->generateUrl(Profile::class));
Expand Down
4 changes: 2 additions & 2 deletions lib/Admin/Controller/Me/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use KejawenLab\ApiSkeleton\Security\Service\UserService;
use KejawenLab\ApiSkeleton\Security\User;
use KejawenLab\ApiSkeleton\Setting\SettingService;
use KejawenLab\ApiSkeleton\Util\CacheFactory;
use KejawenLab\ApiSkeleton\Util\StringUtil;
use Psr\Cache\CacheItemPoolInterface;
use ReflectionClass;
use ReflectionProperty;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand All @@ -39,7 +39,7 @@ public function __construct(
private readonly ApiClientService $apiClientService,
private readonly RequestStack $requestStack,
private readonly UserService $service,
private readonly CacheItemPoolInterface $cache,
private readonly CacheFactory $cache,
private readonly Paginator $paginator,
) {
parent::__construct($this->requestStack->getCurrentRequest(), $this->service, $this->cache, $this->paginator);
Expand Down
4 changes: 2 additions & 2 deletions lib/Admin/Controller/Media/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use KejawenLab\ApiSkeleton\Media\MediaService;
use KejawenLab\ApiSkeleton\Media\Model\MediaInterface;
use KejawenLab\ApiSkeleton\Security\Annotation\Permission;
use Psr\Cache\CacheItemPoolInterface;
use KejawenLab\ApiSkeleton\Util\CacheFactory;
use ReflectionClass;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -27,7 +27,7 @@ final class Audit extends AbstractController
public function __construct(
private readonly RequestStack $requestStack,
private readonly MediaService $service,
private readonly CacheItemPoolInterface $cache,
private readonly CacheFactory $cache,
private readonly AuditService $audit,
private readonly Reader $reader,
) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Admin/Controller/Media/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __invoke(Request $request, string $path): Response

$response = new BinaryFileResponse($file->getRealPath());
$response->setPrivate();
$response->setMaxAge(SemartApiSkeleton::STATIC_PAGE_CACHE_LIFETIME);
$response->setMaxAge(SemartApiSkeleton::PAGE_CACHE_LIFETIME);

if ($request->query->get('f')) {
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $file->getFilename());
Expand Down
Loading

0 comments on commit de89496

Please sign in to comment.