我有一个play框架应用程序,我已经迁移到play框架2.4.2上运行.它为javascript/html前端提供RESTful API.现在我在引入缓存方面遇到了一些问题.
LibraryController(将JSON/HTTP请求转换为JSON/HTTP响应):
public class LibraryController extends Controller {
public Result getBook(String isbn) {
Book book = LibraryManager.getBook(isbn);
BookDto bookDto = DtoMapper.book2BookDtos(book);
return ok(Json.toJson(bookDto));
}
}
Run Code Online (Sandbox Code Playgroud)
LibraryManager(将域模型请求转换为域模型响应):
public class LibraryManager {
@Inject CacheApi cache;
public static Book getBook(String isbn) {
Book book = cache.get(isbn);
// ...
}
Run Code Online (Sandbox Code Playgroud)
我在这里遇到的问题是我得到了
non-static variable cache cannot be referenced from a static context
Run Code Online (Sandbox Code Playgroud)
我注入缓存的方式是根据Play 2.4.2缓存API文档.我根据Play 2.2.x Cache API文档使用缓存时没有遇到此问题.该版本有一个我可以调用的静态方法.
我该怎么办?我应该使getBook非静态应用一些单例模式吗?或者我应该以其他方式访问缓存?示例代码肯定会有所帮助!