Permalink
Browse files

cache everything for the home page

  • Loading branch information...
1 parent 1f15b8c commit ab532444258f494d40a7126716c6ac7190b80a98 @jamesward committed Mar 27, 2013
View
12 app/controllers/ApplicationController.java
@@ -2,19 +2,27 @@
import models.Region;
import models.User;
+import play.cache.Cache;
import play.data.Form;
import play.data.validation.Constraints;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Security;
import play.mvc.With;
+import java.util.List;
+
@With(CurrentUser.class)
public class ApplicationController extends Controller {
public static Result index() {
- return ok(views.html.index.render(Region.find.all()));
+ List<Region> regions = (List<Region>)Cache.get("regions");
+ if (regions == null) {
+ regions = Region.find.all();
+ Cache.set("regions", regions);
+ }
+ return ok(views.html.index.render(regions));
}
public static Result signupForm() {
@@ -48,7 +56,6 @@ public static Result loginForm() {
}
public static Result login() {
-
Form<Login> loginForm = Form.form(Login.class).bindFromRequest();
if (loginForm.hasErrors()) {
@@ -72,6 +79,7 @@ public static Result login() {
@Security.Authenticated(Secured.class)
public static Result logout() {
+ Cache.remove(session().get("token"));
session().remove("token");
return redirect(routes.ApplicationController.index());
}
View
11 app/controllers/CurrentUser.java
@@ -1,6 +1,7 @@
package controllers;
import models.User;
+import play.cache.Cache;
import play.mvc.Action;
import play.mvc.Http;
import play.mvc.Result;
@@ -9,8 +10,14 @@
public Result call(Http.Context ctx) throws Throwable {
String token = ctx.session().get("token");
- models.User user = models.User.findByToken(token);
- ctx.args.put("user", user);
+ if (token != null) {
+ User user = (User)Cache.get(token);
+ if (user == null) {
+ user = models.User.findByToken(token);
+ Cache.set(token, user);
+ }
+ ctx.args.put("user", user);
+ }
return delegate.call(ctx);
}
View
5 app/controllers/RegionController.java
@@ -5,6 +5,7 @@
import com.sun.syndication.io.SyndFeedOutput;
import models.*;
import play.Logger;
+import play.cache.Cache;
import play.data.Form;
import play.mvc.*;
@@ -108,6 +109,7 @@ public static Result unsubscribe(String urlFriendlyRegionName) {
@Security.Authenticated(Secured.class)
public static Result addRegion() {
+ Cache.remove("regions");
return ok(views.html.regionForm.render(Form.form(Region.class)));
}
@@ -142,6 +144,7 @@ public static Result saveRegion() {
S3Photo photo = new S3Photo(photoFilePart.getFile(), 300);
region.photo = photo;
region.save();
+ Cache.remove("regions");
return redirect(routes.RegionController.getRegionHtml(region.getUrlFriendlyName(), "name"));
} catch (IOException e) {
Logger.error(e.getMessage());
@@ -166,6 +169,8 @@ public static Result deleteRegion(String urlFriendlyRegionName) {
region.delete();
+ Cache.remove("regions");
+
return redirect(routes.ApplicationController.index());
}
View
4 app/controllers/RouteController.java
@@ -1,7 +1,7 @@
package controllers;
-import ch.qos.logback.core.pattern.parser.FormattingNode;
import models.*;
+import play.cache.Cache;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
@@ -56,6 +56,7 @@ public static Result saveComment(String urlFriendlyRegionName, String urlFriendl
comment.user = user;
comment.route = route;
comment.save();
+ Cache.remove("five-most-recent-comments");
}
return redirect(routes.RouteController.getRouteHtml(urlFriendlyRegionName, urlFriendlyRouteName));
@@ -75,6 +76,7 @@ public static Result deleteRoute(String urlFriendlyRegionName, String urlFriendl
}
route.delete();
+ Cache.remove("five-most-recent-comments");
return redirect(routes.RegionController.getRegionHtml(urlFriendlyRegionName, "name"));
}
View
8 app/models/Comment.java
@@ -1,6 +1,7 @@
package models;
import com.avaje.ebean.FetchConfig;
+import play.cache.*;
import play.data.validation.Constraints;
import play.db.ebean.Model;
@@ -48,7 +49,12 @@ public Comment(User user, Route route, String value) {
public static Finder<Long, Comment> find = new Finder<Long, Comment>(Long.class, Comment.class);
public static List<Comment> fiveMostRecent() {
- return find.orderBy("creationDate desc").fetch("route.region", new FetchConfig().query()).setMaxRows(5).findList();
+ List<Comment> comments = (List<Comment>)play.cache.Cache.get("five-most-recent-comments");
+ if (comments == null) {
+ comments = find.orderBy("creationDate desc").fetch("route.region", new FetchConfig().query()).setMaxRows(5).findList();
+ play.cache.Cache.set("five-most-recent-comments", comments);
+ }
+ return comments;
}
}
View
7 conf/application.conf
@@ -22,8 +22,9 @@ application.langs="en"
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#
-# db.default.driver=org.h2.Driver
-# db.default.url="jdbc:h2:mem:play"
+#db.default.driver=org.h2.Driver
+#db.default.url="jdbc:h2:mem:play"
+#db.default.logStatements=true
db.default.driver=org.postgresql.Driver
db.default.url="postgres://hikeubertracks:password@localhost/hikeubertracks"
db.default.partitionCount=1
@@ -60,6 +61,8 @@ logger.play=INFO
# Logger provided to your application:
logger.application=DEBUG
+logger.com.jolbox=DEBUG
+
aws.access.key=${?AWS_ACCESS_KEY}
aws.secret.key=${?AWS_SECRET_KEY}
aws.s3.bucket=com.ubertracks.hike.dev

0 comments on commit ab53244

Please sign in to comment.