fix: invalidate booking data caches after updating personal data
This commit is contained in:
@@ -12,8 +12,8 @@ use App\Entity\User;
|
||||
use App\Form\Model\BookingDto;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
||||
|
||||
/**
|
||||
* Handles loading and initializing booking data for edit mode.
|
||||
@@ -23,6 +23,8 @@ use Symfony\Contracts\Cache\ItemInterface;
|
||||
*/
|
||||
class BookingEditDataLoaderService
|
||||
{
|
||||
public const CACHE_TAG_USER_PREFIX = 'user_bookings_';
|
||||
|
||||
private bool $draftWasRestored = false;
|
||||
|
||||
public function __construct(
|
||||
@@ -32,7 +34,7 @@ class BookingEditDataLoaderService
|
||||
private readonly BookingFingerprintService $fingerprintService,
|
||||
private readonly TravelDataService $travelDataService,
|
||||
private readonly BookingEditDraftService $draftService,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly TagAwareCacheInterface $bpnCache,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -101,7 +103,7 @@ class BookingEditDataLoaderService
|
||||
*/
|
||||
public function initializeFromApi(Request $request, int $bookingId, string $email, string $password, User $user): ?BookingDto
|
||||
{
|
||||
$bookingData = $this->fetchBookingData($email, $password, $bookingId);
|
||||
$bookingData = $this->fetchBookingData($email, $password, $bookingId, $user);
|
||||
|
||||
if (null === $bookingData || $bookingData instanceof Notification) {
|
||||
return null;
|
||||
@@ -145,13 +147,26 @@ class BookingEditDataLoaderService
|
||||
|
||||
/**
|
||||
* Fetches booking data from API with caching.
|
||||
*
|
||||
* Cache entries are tagged with the user ID to allow bulk invalidation
|
||||
* when the user updates their personal data.
|
||||
*
|
||||
* @param string $email User email for API authentication
|
||||
* @param string $password User password for API authentication
|
||||
* @param int $bookingId The booking ID to fetch
|
||||
* @param User $user The user for cache tagging
|
||||
*
|
||||
* @return Booking|Notification|null The booking data, notification on error, or null on cache failure
|
||||
*/
|
||||
public function fetchBookingData(string $email, string $password, int $bookingId): Booking|Notification|null
|
||||
public function fetchBookingData(string $email, string $password, int $bookingId, User $user): Booking|Notification|null
|
||||
{
|
||||
$cacheKey = sprintf('bpn_booking_%d', $bookingId);
|
||||
$userTag = self::CACHE_TAG_USER_PREFIX.$user->getId();
|
||||
|
||||
try {
|
||||
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($email, $password, $bookingId) {
|
||||
return $this->bpnCache->get($cacheKey, function (ItemInterface $item) use ($email, $password, $bookingId, $userTag) {
|
||||
$item->expiresAfter(300);
|
||||
$item->tag([$userTag]);
|
||||
|
||||
return $this->apiClient->getBooking($email, $password, $bookingId);
|
||||
});
|
||||
@@ -162,14 +177,34 @@ class BookingEditDataLoaderService
|
||||
|
||||
/**
|
||||
* Invalidates the booking cache after successful update.
|
||||
*
|
||||
* @param int $bookingId The booking ID to invalidate
|
||||
*/
|
||||
public function invalidateBookingCache(int $bookingId): void
|
||||
{
|
||||
try {
|
||||
$cacheKey = sprintf('bpn_booking_%d', $bookingId);
|
||||
$this->cache->delete($cacheKey);
|
||||
$this->bpnCache->delete($cacheKey);
|
||||
} catch (InvalidArgumentException) {
|
||||
// Ignore cache deletion errors
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidates all cached bookings for a user.
|
||||
*
|
||||
* Called when the user updates their personal data to ensure
|
||||
* booking edits show the latest applicant information.
|
||||
*
|
||||
* @param User $user The user whose booking caches should be invalidated
|
||||
*/
|
||||
public function invalidateUserBookingCaches(User $user): void
|
||||
{
|
||||
try {
|
||||
$userTag = self::CACHE_TAG_USER_PREFIX.$user->getId();
|
||||
$this->bpnCache->invalidateTags([$userTag]);
|
||||
} catch (InvalidArgumentException) {
|
||||
// Ignore cache invalidation errors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user