Skip to content

Instantly share code, notes, and snippets.

@CrypticMessenger
Last active September 30, 2025 18:45
Show Gist options
  • Save CrypticMessenger/5ee5a2b072c3b426b6e2aa882636e2f0 to your computer and use it in GitHub Desktop.
Save CrypticMessenger/5ee5a2b072c3b426b6e2aa882636e2f0 to your computer and use it in GitHub Desktop.
UnsafeMemorizer
package concurrentcache.cache;
import java.util.HashMap;
import java.util.Map;
import concurrentcache.computable.Computable;
/**
* A basic memoization implementation that caches computed results.
* This implementation is not thread-safe and should not be used in concurrent environments.
*/
public class UnsafeMemoizer<A,V> implements Computable<A,V> {
private final Map<A,V> cache = new HashMap<>();
private final Computable<A,V> computable;
public UnsafeMemoizer(Computable<A,V> computable) {
this.computable = computable;
}
@Override
public V compute(A arg) throws InterruptedException {
V result = cache.get(arg);
if (result == null) {
result = computable.compute(arg);
cache.put(arg, result);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment