WeakCache

Introduction

Weak reference cache. For a given key, the existence of its mapping does not prevent the garbage collector from discarding the key, which makes the key terminable and can be terminated and then reclaimed. When a key is discarded, its entry is effectively removed from the mapping. This class uses WeakHashMap as its implementation, and the cleanup of the cache depends on the garbage collection of the JVM.

Usage

The usage is consistent with TimedCache:

WeakCache<String, String> weakCache = CacheUtil.newWeakCache(DateUnit.SECOND.getMillis() * 3);

WeakCache can also be used like TimedCache to set a timed cleanup time, while also having garbage collection cleanup.