feat: removed caching of list view

This commit is contained in:
Björn Fromme
2025-01-25 15:25:34 +01:00
parent f9e7f28b6b
commit 289668bcbd
2 changed files with 9 additions and 28 deletions
+3 -5
View File
@@ -39,10 +39,9 @@ class EditController extends AbstractController
} }
// Fetch original bookingData data via API and cache result for a short ttl // Fetch original bookingData data via API and cache result for a short ttl
$cacheKeyList = sprintf('bpn_bookings_%s', sha1($bpnUser->getUserIdentifier())); $cacheKey = sprintf('bpn_booking_%d', $id);
$cacheKeySingle = sprintf('bpn_booking_%d', $id);
try { try {
$bookingData = $this->cache->get($cacheKeySingle, function (ItemInterface $item) use ($bpnUser, $id) { $bookingData = $this->cache->get($cacheKey, function (ItemInterface $item) use ($bpnUser, $id) {
$item->expiresAfter(300); $item->expiresAfter(300);
return $this->apiClient->getBooking($bpnUser->getEmail(), $bpnUser->getPassword(), $id); return $this->apiClient->getBooking($bpnUser->getEmail(), $bpnUser->getPassword(), $id);
@@ -90,8 +89,7 @@ class EditController extends AbstractController
$this->apiClient->updateBooking($formData); $this->apiClient->updateBooking($formData);
try { try {
$this->cache->delete($cacheKeySingle); $this->cache->delete($cacheKey);
$this->cache->delete($cacheKeyList);
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
} }
+6 -23
View File
@@ -4,23 +4,19 @@ namespace App\Controller\Booking;
use App\BusProNet\ApiClient; use App\BusProNet\ApiClient;
use App\BusProNet\XmlLoader\TravelLoader; use App\BusProNet\XmlLoader\TravelLoader;
use Psr\Cache\InvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security; use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted; use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
class IndexController extends AbstractController class IndexController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly ApiClient $apiClient, private readonly ApiClient $apiClient,
private readonly TravelLoader $travelDataLoader, private readonly TravelLoader $travelDataLoader,
private readonly CacheInterface $cache, private readonly Security $security,
private readonly Security $security,
) { ) {
} }
@@ -34,24 +30,11 @@ class IndexController extends AbstractController
return $this->security->logout(); return $this->security->logout();
} }
// Cache bookings for a short ttl $bookings = $this->apiClient->getBookings($bpnUser->getEmail(), $bpnUser->getPassword());
$cacheKey = sprintf('bpn_bookings_%s', sha1($bpnUser->getUserIdentifier())); $this->travelDataLoader->patchBookings($bookings);
try {
$bookings = $this->cache->get($cacheKey, function (ItemInterface $item) use ($bpnUser) {
$item->expiresAfter(300);
$bookings = $this->apiClient->getBookings($bpnUser->getEmail(), $bpnUser->getPassword());
$this->travelDataLoader->patchBookings($bookings);
return $bookings;
});
$items = $bookings->getItems();
} catch (InvalidArgumentException $e) {
$items = [];
}
return $this->render('booking/index.html.twig', [ return $this->render('booking/index.html.twig', [
'bookings' => $items, 'bookings' => $bookings->getItems(),
]); ]);
} }
} }