Last active
September 30, 2025 18:45
-
-
Save CrypticMessenger/5ee5a2b072c3b426b6e2aa882636e2f0 to your computer and use it in GitHub Desktop.
UnsafeMemorizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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