2
我有一個控制器,其中有一個構造函數,我在注入一個緩存,但是我想在實例創建時調用構造函數中的一個方法。我知道我們可以創造一些輔助的構造與構造函數中的調用方法
def this(foo:Foo){}
但在我的情況下,因爲是遊戲框架一個實例我的引導是更復雜一點。
這裏我的代碼
class SteamController @Inject()(cache: CacheApi) extends BaseController {
private val GAME_IDS_LIST_API: String = "api.steampowered.com/ISteamApps/GetAppList/v2"
private val GAME_API: String = "store.steampowered.com/api/appdetails?appids="
private val GAME_KEY: String = "games"
def games = Action { implicit request =>
var fromRequest = request.getQueryString("from")
if (fromRequest.isEmpty) {
fromRequest = Option("0")
}
val from = Integer.parseInt(fromRequest.get) * 10
val to = from + 10
loadGameIds()
Ok(html.games(SteamStore.gamesIds(cache.getVal[JSONArray](GAME_KEY), from, to), cache.jsonArraySize(GAME_KEY)/10))
}
private def loadGameIds(): Unit = {
val games = cache.get(GAME_KEY)
if (games.isEmpty) {
get(s"$GAME_IDS_LIST_API", asJsonGamesId)
cache.set(GAME_KEY, lastResponse.get, 60.minutes)
}
}
我想的是,loadGameIds會被調用,當類實例化緩存。
有什麼建議嗎?
問候。
非常感謝我嘗試的對象init {loadGameIds}但沒有。奇怪的是,我沒有在我讀過的任何博客中瞭解到這一點。 – paul