Created
September 20, 2013 14:16
-
-
Save tomgullo/6638257 to your computer and use it in GitHub Desktop.
Google Guava cache example
This file contains 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
import com.google.common.cache.* | |
import java.util.concurrent.TimeUnit | |
LoadingCache<String, String> mycache = CacheBuilder.newBuilder() | |
.maximumSize(1000) | |
.recordStats() | |
.expireAfterWrite(5, TimeUnit.SECONDS) | |
.build( | |
new CacheLoader<String, String>() { | |
public String load(String key) throws Exception { | |
return "{'test', 'this string'}" | |
} | |
}); | |
def test = "test" | |
try { | |
println mycache.get(test) | |
println mycache.stats() | |
sleep 2000 | |
println mycache.get(test) | |
println mycache.stats() | |
sleep 6000 | |
println mycache.get(test) | |
println mycache.stats() | |
} catch (Exception e) { | |
println "error " + e | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment