Skip to content

Commit

Permalink
refactor: caching eviction/put during update
Browse files Browse the repository at this point in the history
Took 27 minutes
  • Loading branch information
vianneynara committed Dec 9, 2024
1 parent 8dfaf04 commit c9b8715
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import dev.kons.kuenyawz.services.entity.TransactionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
Expand All @@ -29,6 +30,7 @@ public class MidtransWebhookServiceImpl implements MidtransWebhookService {
private final WhatsappApiService whatsappApiService;

@Override
@CacheEvict(value = "purchasesCache", allEntries = true)
public void processNotification(MidtransNotification notification) {
printNotification(notification); // TODO: remove in production
MidtransWebhookService.validateSignatureKey(notification, properties.midtrans().getServerKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
Expand Down Expand Up @@ -51,6 +54,7 @@ public class OrderingServiceImpl implements OrderingService {
private final CartItemService cartItemService;

@Override
@CacheEvict(value = "purchasesCache", allEntries = true)
public PurchaseDto processOrder(PurchasePostDto purchasePostDto) {
// Initialize required entities
Account account = AuthService.getAuthenticatedAccount();
Expand Down Expand Up @@ -149,6 +153,8 @@ public PurchaseDto processOrder(PurchasePostDto purchasePostDto) {
}

@Override
@CachePut(value = "purchaseCache", key = "#purchaseId")
@CacheEvict(value = "purchasesCache", allEntries = true)
public PurchaseDto cancelOrder(Long purchaseId) {
Purchase purchase = purchaseService.getById(purchaseId);

Expand Down Expand Up @@ -194,6 +200,8 @@ public PurchaseDto cancelOrder(Long purchaseId) {
}

@Override
@CachePut(value = "purchaseCache", key = "#purchaseId")
@CacheEvict(value = "purchasesCache", allEntries = true)
public PurchaseDto confirmOrder(Long purchaseId) {
AuthService.validateIsAdmin();

Expand Down Expand Up @@ -236,6 +244,8 @@ public PurchaseDto confirmOrder(Long purchaseId) {
}

@Override
@CachePut(value = "purchaseCache", key = "#purchaseId")
@CacheEvict(value = "purchasesCache", allEntries = true)
public PurchaseDto refundOrder(Long purchaseId) {
AuthService.validateIsAdmin();

Expand Down Expand Up @@ -268,6 +278,8 @@ public PurchaseDto refundOrder(Long purchaseId) {
}

@Override
@CachePut(value = "purchaseCache", key = "#purchaseId")
@CacheEvict(value = "purchasesCache", allEntries = true)
public PurchaseDto changeOrderStatus(Long purchaseId, String status) {
AuthService.validateIsAdmin();

Expand All @@ -276,6 +288,8 @@ public PurchaseDto changeOrderStatus(Long purchaseId, String status) {
}

@Override
@CachePut(value = "purchaseCache", key = "#purchaseId")
@CacheEvict(value = "purchasesCache", allEntries = true)
public PurchaseDto upgradeOrderStatus(Long purchaseId) {
AuthService.validateIsAdmin();

Expand All @@ -301,6 +315,7 @@ public Page<PurchaseDto> findAll(PurchaseService.PurchaseSearchCriteria criteria
}

@Override
@Cacheable(value = "purchaseCache", key = "#purchaseId")
public PurchaseDto findPurchase(Long purchaseId) {
validateOwnershipOrAdmin(purchaseId);

Expand Down

0 comments on commit c9b8715

Please sign in to comment.