|
| 1 | +package com.bootcamptoprod.cache.management.service; |
| 2 | + |
| 3 | +import com.bootcamptoprod.cache.management.Entity.Product; |
| 4 | +import com.bootcamptoprod.cache.management.repository.ProductRepository; |
| 5 | +import org.springframework.cache.annotation.CacheEvict; |
| 6 | +import org.springframework.cache.annotation.CachePut; |
| 7 | +import org.springframework.cache.annotation.Cacheable; |
| 8 | +import org.springframework.cache.annotation.Caching; |
| 9 | +import org.springframework.scheduling.annotation.Scheduled; |
| 10 | +import org.springframework.stereotype.Service; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +@Service |
| 15 | +public class ProductService { |
| 16 | + |
| 17 | + public static final String PRODUCTS_CACHE_KEY = "products"; |
| 18 | + |
| 19 | + private final ProductRepository productRepository; |
| 20 | + |
| 21 | + public ProductService(ProductRepository productRepository) { |
| 22 | + this.productRepository = productRepository; |
| 23 | + } |
| 24 | + |
| 25 | + @Cacheable(value = "products", key = "#root.target.PRODUCTS_CACHE_KEY") |
| 26 | + public List<Product> getAllProducts() { |
| 27 | + return productRepository.findAll(); |
| 28 | + } |
| 29 | + |
| 30 | + @Cacheable(value = "products", key = "#productId", condition = "#productId > 0") |
| 31 | + public Product getProductById(long productId) { |
| 32 | + return productRepository.findById(productId).orElse(null); |
| 33 | + } |
| 34 | + |
| 35 | + @CachePut(value = "products", key = "#product.id") |
| 36 | + public Product updateProduct(Product product) { |
| 37 | + Product updatedProduct = productRepository.save(product); |
| 38 | + return updatedProduct; |
| 39 | + } |
| 40 | + |
| 41 | + @CacheEvict(value = "products", key = "#productId") |
| 42 | + public void deleteProduct(long productId) { |
| 43 | + productRepository.deleteById(productId); |
| 44 | + } |
| 45 | + |
| 46 | + @Caching( |
| 47 | + cacheable = {@Cacheable(value = "products", key = "#product.id")}, |
| 48 | + evict = {@CacheEvict(value = "recentProducts", allEntries = true)} |
| 49 | + ) |
| 50 | + public Product getProductDetails(Product product) { |
| 51 | + // Logic to fetch detailed product information |
| 52 | + return productRepository.findById(product.getId()).orElse(null); |
| 53 | + } |
| 54 | + |
| 55 | + @Cacheable(value = "products", key = "#productId", unless = "#result.price < 100") |
| 56 | + public Product getProductWithPriceCheck(long productId) { |
| 57 | + return productRepository.findById(productId).orElse(null); |
| 58 | + } |
| 59 | + |
| 60 | + @CacheEvict(value = "products", allEntries = true) |
| 61 | + public void clearProductCache() { |
| 62 | + System.out.println("Product cache was cleared successfully"); |
| 63 | + } |
| 64 | + |
| 65 | + @CacheEvict(value = "products", allEntries = true) |
| 66 | + @Scheduled(fixedRateString = "43200000") |
| 67 | + public void clearProductsCache() { |
| 68 | + System.out.println("Clearing products cache"); |
| 69 | + } |
| 70 | +} |
0 commit comments