Skip to content

Instantly share code, notes, and snippets.

@tomgullo
Created September 20, 2013 14:16
Show Gist options
  • Save tomgullo/6638257 to your computer and use it in GitHub Desktop.
Save tomgullo/6638257 to your computer and use it in GitHub Desktop.
Google Guava cache example
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