Invalidation messages may be duplicated, delayed, or delivered out of order. Version watermarks let a cache consume those events idempotently without allowing an old event to remove newer data or an old fill to resurrect invalidated data.
Design a VersionedInvalidationCache class:
boolean put(String key, int version) tries to cache a version.boolean invalidate(String key, int eventVersion) processes an event and returns whether a cached entry was removed.int getVersion(String key) returns the cached version, or -1 when absent.
Store the highest invalidation version observed per key, including absent keys. Ignore events at or below that watermark. A newer event removes the cached entry only when its version is at most the event version.
put rejects a version at or below the invalidation watermark or below the currently cached version. Equal-version puts are accepted as idempotent; newer safe versions replace the cache.
Example 1:
Example 2:
Constraints
1 <= key.length <= 400 <= version, eventVersion <= 10^9- At most
500 total method calls are made.