Skip to content

Instantly share code, notes, and snippets.

@martijnvg
Created November 16, 2012 21:28
Show Gist options
  • Save martijnvg/4091080 to your computer and use it in GitHub Desktop.
Save martijnvg/4091080 to your computer and use it in GitHub Desktop.
soft cache - Ran with -Xmx128M
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
/**
*/
public class Testje {
public static void main(String[] args) {
CacheBuilder<String, Integer> cacheBuilder = CacheBuilder.newBuilder().softValues().removalListener(new RemovalListener<String, Integer>() {
@Override
public void onRemoval(RemovalNotification<String, Integer> notification) {
System.out.println("About to remove: " + notification.getKey());
}
});
Cache<String, Integer> cache = cacheBuilder.build();
for (int i = 0; i < Integer.MAX_VALUE; i++) {
cache.put("key" + i, i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment