Skip to content

Instantly share code, notes, and snippets.

@satomikko94
Last active December 13, 2015 05:33
Show Gist options
  • Save satomikko94/eba9a0bba3b67e13265f to your computer and use it in GitHub Desktop.
Save satomikko94/eba9a0bba3b67e13265f to your computer and use it in GitHub Desktop.
PlayFrameworkでキャッシュの値の有無によってViewを出し分けするサンプル ref: http://qiita.com/satomikko94/items/c46be49f8833a03339e1
static Form<Task> taskFrom = Form.form(Task.class);
public Result index() {
return ok(setCache.render(taskFrom));
}
public Result setTask() {
// get values from form
Form<Task> filledForm = taskFrom.bindFromRequest();
Task task = filledForm.get();
// set Task model to cache
Cache.set("task", task, 60 * 1);
return redirect(routes.Application.index());
}
@(taskForm: Form[Task])
<!DOCTYPE html>
<html lang="en">
<head>
<title>Play framework - Cache sample</title>
</head>
<body>
<!-- send data from form -->
@form(routes.Application.setTask()) {
@inputText(taskForm("id"))
@inputText(taskForm("taskName"))
<input type="submit">
}
<!-- show cache data -->
@showCache()
</body>
</html>
@(task : Task = Cache.get("task").asInstanceOf[Task])
<!-- if cache is not null, show cache data -->
@if(task == null) {
<h1>Cache has no data!</h1>
} else {
<h1>Task data from Cache</h1>
id : @task.id<br>
taskName : @task.taskName<br>
<br>
Cache will expire after 1 minutes.
}
public class Task {
public String id;
public String taskName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment