Building lightweight in memory caches with Google Guava – no more putIfAbsent « EclipseSource Blog
Building lightweight in memory caches with Google Guava – no more putIfAbsent I can’t count the number of times I found myself implementing some sort of cache. In many situations caching is really useful e.g. when the computation of a value is expensive or when loading of resources is involved. I bet you found yourself implementing a cache many times too. Probably you have also used a Java Map for storing the values. One disadvantage of using Maps for caching is that you have to implement the eviction of entries yourself, e.g. to keep the size to a given limit. When you develop for a concurrent environment the task gets more complicated and a simple Map is not sufficient. You need to switch to a thread safe solution, e.g. a ConcurrentHashMap ConcurrentHashMap s solve the concurrency problem, but the code gets ugly. You have to deal with the fact that keys can be added multiple times from different threads concurrently. A few months ago I learned about Google Guava’s Caches .Read full article from Building lightweight in memory caches with Google Guava – no more putIfAbsent « EclipseSource Blog
No comments:
Post a Comment