I use Guava LoadCache to store results from database queries. However, despite the fact that you do not set the eviction policy, executing get cache via getFromCache () causes my debug point in the CacheLoader load method to be pressed every time, so there is also a debug point in the getKeyFromDatabase database query method ( ) hitting each time.
Here is my code:
private final LoadingCache<String, QueryResult> cache;
public MyDao() {
cache = CacheBuilder.newBuilder()
.maximumSize(40)
.build(new CacheLoader<String, QueryResult>() {
@Override
public QueryResult load(String key) throws DatabaseException {
return getKeyFromDatabase(key);
}
});
}
public QueryResult getFromCache(String key) throws DatabaseException {
try {
return cache.get(key);
} catch (ExecutionException e) {
throw new DatabaseException(e);
}
}
private QueryResult getKeyFromDatabase(String key) throws DatabaseException {
try {
...
return new QueryResult();
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
Did I miss something obvious here?
ddxue source
share