Skip to content

Commit

Permalink
fix user context cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ad3n committed Jan 24, 2022
1 parent 441b1ec commit b7afbba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
7 changes: 6 additions & 1 deletion lib/EventSubscriber/SingleLoginSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use KejawenLab\ApiSkeleton\Util\Encryptor;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTDecodedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand Down Expand Up @@ -57,6 +58,8 @@ public function validate(RequestEvent $event): void
return;
}

$event->getRequest()->getSession()->clear();

$event->setResponse(new RedirectResponse($this->urlGenerator->generate('admin_logout')));
}

Expand Down Expand Up @@ -146,9 +149,11 @@ public function create(JWTCreatedEvent $event): void
public static function getSubscribedEvents(): array
{
return [
RequestEvent::class => [['validate', -255]],
RequestEvent::class => [['validate', 127]],
JWTCreatedEvent::class => 'create',
JWTDecodedEvent::class => 'decode',
Events::JWT_CREATED => 'create',
Events::JWT_DECODED => 'decode',
];
}
}
24 changes: 14 additions & 10 deletions lib/EventSubscriber/ViewCacheSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public function serve(RequestEvent $event): void
}

$cached = $this->getCache($event);
if (empty($cached)) {
if (null === $cached) {
return;
}

$event->setResponse(new Response($cached));
$event->setResponse($cached);
$event->stopPropagation();
}

Expand Down Expand Up @@ -76,7 +76,7 @@ public static function getSubscribedEvents(): array
{
return [
RequestEvent::class => [
['serve', 127],
['serve', 125],
['invalidate', 17],
],
ResponseEvent::class => [['populate', -27]],
Expand Down Expand Up @@ -111,7 +111,7 @@ private function setCache(ResponseEvent $event): void
$keys = $pool->get();
}

$keys = array_merge($keys, [$key => true]);
$keys = array_merge($keys, [$key => $response->headers->get('Content-Type')]);

$pool->set($keys);
$pool->expiresAfter(new \DateInterval(SemartApiSkeleton::STATIC_CACHE_PERIOD));
Expand All @@ -122,30 +122,34 @@ private function setCache(ResponseEvent $event): void
$this->cache->save($cache);
}

private function getCache(KernelEvent $event): string
private function getCache(KernelEvent $event): ?Response
{
$deviceId = $this->getDeviceId($event);
if (empty($deviceId)) {
return '';
return null;
}

$pool = $this->cache->getItem($deviceId);
if (!$pool->isHit()) {
return '';
return null;
}

$keys = $pool->get();
$cacheId = $this->getCacheKey($event);
if (!array_key_exists($cacheId, $keys)) {
return '';
return null;
}

$item = $this->cache->getItem($cacheId);
if (!$item->isHit()) {
return '';
return null;
}

return $item->get();
$response = new Response($item->get());
$response->headers->set('Content-Type', $keys[$cacheId]);
$response->headers->set(SemartApiSkeleton::STATIC_CACHE_HEADER, $cacheId);

return $response;
}

private function getCacheKey(KernelEvent $event): string
Expand Down
6 changes: 4 additions & 2 deletions lib/SemartApiSkeleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ final class SemartApiSkeleton

final public const STATIC_CACHE_PERIOD = 'P17M';

final public const STATIC_CACHE_HEADER = 'X-Semart-Content-ID';

final public const CODENAME = 'Dodol Duren';

final public const VERSION = '5.8.3';
final public const VERSION = '5.8.4';

final public const VERSION_MAYOR = 50000;

final public const VERSION_MINOR = 800;

final public const VERSION_PATCH = 3;
final public const VERSION_PATCH = 4;

public static function getVersionNumber(): int
{
Expand Down

0 comments on commit b7afbba

Please sign in to comment.