【发布时间】:2017-10-03 07:37:52
【问题描述】:
我正在使用 Play 2.6.3,并且我最近从 Play 2.5.x 升级。
我在使用 session 和 HTTPExecutionContext 操作缓存时遇到问题,因为新的 Play 2.6.x 使用 EhCache。
在 Play 2.5 中,通过以下方式获取或更新缓存非常简单:
User currentUser = cache.getOrElse(session("email"), () -> {
User user = User.find.byId(session("email"));
cache.set(user.email, user, Constants.CACHE_TIMEOUT);
return user;
});
这不起作用,因为他们已弃用 getOrElse 方法,而是创建了使用缓存 API 的 getOrElseUpdate 并且由 AsyncCacheApi 和 SyncCacheApi 接口定义。 所以,简而言之,新缓存返回 CompletionStage 而不是直接返回 T。
新玩法2.6代码:
CompletionStage<User> maybeCached = cache.getOrElseUpdate(session("email"), () -> lookUpUser());
return maybeCached.thenApplyAsync(op -> {
return ok(views.html.dashboard.render("Dashboard", op, op.organization));
}, httpExecutionContext.current());
public CompletionStage<User> lookUpUser() {
return CompletableFuture.supplyAsync(() -> User.find.byId(session("email")),httpExecutionContext.current());
}
lookUpUser 函数抛出错误:java.lang.RuntimeException: There is no HTTP Context available from here.
任何帮助将不胜感激。
【问题讨论】:
标签: java caching playframework