import java.util.HashMap;
import java.util.Map;
public class StringCacheInvalidator {
private static final Map<String, String> cache = new HashMap<>(); // Simple string cache
/**
* Invalidates the cache for a given key.
*
* @param key The key of the string to invalidate.
*/
public static void invalidateCache(String key) {
cache.remove(key); // Remove the entry with the given key
}
/**
* Clears the entire cache.
*/
public static void clearCache() {
cache.clear(); // Remove all entries from the cache
}
public static void main(String[] args) {
//Example Usage
cache.put("key1", "value1");
cache.put("key2", "value2");
System.out.println("Cache before invalidation: " + cache);
invalidateCache("key1");
System.out.println("Cache after invalidating key1: " + cache);
clearCache();
System.out.println("Cache after clearing: " + cache);
}
}
Add your comment