1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class StringCacheInvalidator {
  4. private static final Map<String, String> cache = new HashMap<>(); // Simple string cache
  5. /**
  6. * Invalidates the cache for a given key.
  7. *
  8. * @param key The key of the string to invalidate.
  9. */
  10. public static void invalidateCache(String key) {
  11. cache.remove(key); // Remove the entry with the given key
  12. }
  13. /**
  14. * Clears the entire cache.
  15. */
  16. public static void clearCache() {
  17. cache.clear(); // Remove all entries from the cache
  18. }
  19. public static void main(String[] args) {
  20. //Example Usage
  21. cache.put("key1", "value1");
  22. cache.put("key2", "value2");
  23. System.out.println("Cache before invalidation: " + cache);
  24. invalidateCache("key1");
  25. System.out.println("Cache after invalidating key1: " + cache);
  26. clearCache();
  27. System.out.println("Cache after clearing: " + cache);
  28. }
  29. }

Add your comment