我在一个基于JSP web的应用程序中使用Sesame,我想知道是否有任何方法可以缓存一些一致使用的查询。
发布于 2014-11-21 08:55:57
我假设您希望“缓存”的是具有特定值的给定查询的查询结果。您可以很容易地自己构建这样的缓存。只需为一般查询创建一个类,该类在内部保持对从值键(例如示例查询的placeid )到查询结果的HashMap的引用:
HashMap<URI, TupleQueryResult> cache = new HashMap<>();然后,您所要做的就是检查给定的位置id是否存在于缓存中。如果不是,则执行查询,返回结果并将其物化为MutableTupleQueryResult,然后可以将其放入缓存中:
if (!cache.contains(placeId)) {
// reuse the prepared query with the specific binding for which we want a result
preparedQuery.setBinding("placeid", placeId);
// execute the query and add the result to a result object we can reuse multiple times
TupleQueryResult result = new MutableTupleQueryResult(preparedQuery.evaluate());
// put the result in the cache.
cache.put(placeId, result);
}
return cache.get(placeId);如果你想要一些更复杂的东西(例如,在一段时间后抛出缓存的项目,或者对你的缓存设置大小限制),我会考虑使用像Guava Cache这样的东西而不是简单的HashMap,但基本的设置将保持不变。
https://stackoverflow.com/questions/27028969
复制相似问题