1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class CacheInvalidator {
  4. private static final Map<String, Long> urlCache = new HashMap<>(); // Cache to store URL parameters and expiration times
  5. /**
  6. * Invalidates the cache for a given URL parameter.
  7. * @param urlParameter The URL parameter to invalidate.
  8. */
  9. public static void invalidateCache(String urlParameter) {
  10. urlCache.remove(urlParameter); // Remove the entry from the cache
  11. }
  12. /**
  13. * Invalidates the cache for a specific duration (in milliseconds).
  14. * @param urlParameter The URL parameter to invalidate.
  15. * @param expirationTime The expiration time in milliseconds.
  16. */
  17. public static void invalidateCache(String urlParameter, long expirationTime) {
  18. urlCache.put(urlParameter, System.currentTimeMillis() + expirationTime); // Set expiration time
  19. }
  20. public static void main(String[] args) {
  21. // Example Usage:
  22. //Invalidate cache for a specific URL parameter
  23. invalidateCache("productId");
  24. //Invalidate cache with a specified expiration time
  25. invalidateCache("category", 60000); // Expires in 60 seconds
  26. }
  27. }

Add your comment