Like in the basic hello world Play! app, you can do:
object Application extends Controller {
var ak = 0
import views.Application._
def index = {
ak = ak + 1
html.index("Your Scala application is ready!" + ak)
}
}
And every time you refresh the page, the number increments. Does anyone know if this behavior is something I can rely on, or if it’s something I should totally avoid? It seems like I could use it to do some local caching of simple calculations while avoiding having to serialize/transmit the data to memcached
Since Play! is designed to be a stateless webframework I wouldn’t rely on Controller lifecycle to handle such state.
It’s highly probable that the container will stop and restart the controller, specially on Cloud infrastructure.
You should seriously look into alternative (like memcached as you mentioned).
Check section “Session and Flash scopes” on page (showing some facilities for caching):
http://www.playframework.org/documentation/1.1.1/controllers