简体   繁体   中英

Play Framework - Saving object state across multiple calls

I have a Play! framework (2.3.7) application that has several routes:

/api/set_x
/api/set_y
/api/set_z
/api/launch

Each one of these calls sets a property in an instance of MyObject . When /api/launch is called, the instance of MyObject is validated and if everything is ok it will be persisted and 200 OK will be returned to the user.

How to I keep the state of the instance across multiple calls until /api/launch is called?

  1. Using the session seems bad since in play the session is just a cookie, limited to 4KB.
  2. Using the cache - if the cache is evicted I have no way of reconstructing the instance again, which means that the user can supply all the properties but before calling /api/launch the cache was evicted and a bad request instead of OK will be returned to the user.

Is there an elegant way to implement such a thing in Play?

There's no the best solution for your problem and it mostly depends on a specific use case. To keep data between requests you have three possible options:

  • Play's session or cookies
  • some cache layer
  • a database

One of the main feature of Play framework is being stateless on a server side and that should be always in mind while designing an application. So if you are looking for an 'elegant' solution the answer is simple - there's no such. It's all up to a specific case.

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