简体   繁体   中英

Cache solution to manage external api calls using play framework

I have some external paid api that I use to retrieve data based on some id, and some of the calls I will perform will probably have the same id, so im looking for the best practice in play to manage this kind of scenario with cache. also I want to be able to delete the cache after 24 hrs.

any recommendations ?

thanks!

Play documentation gives (imo) clear indications about what to do : https://www.playframework.com/documentation/2.6.x/ScalaCache

The gist of it is :

  • Inject a cache instance where needed :
  • Use your instance to cache stuff :

So basiscally :

import play.api.cache._
import play.api.mvc._
import javax.inject.Inject

class Application @Inject() (cache: AsyncCacheApi, cc:ControllerComponents) extends AbstractController(cc) {

[...]
val result: Future[Done] = cache.set("item.key", connectedUser, 24.hours)
val futureMaybeUser: Future[Option[User]] = cache.get[User]("item.key")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM