Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 35 additions and 9 deletions.
  1. +3 −7 app/controllers/Galleries.java
  2. +32 −2 app/models/Photo.java
View
10 app/controllers/Galleries.java
@@ -1,18 +1,14 @@
package controllers;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import com.google.gson.JsonObject;
-
import models.Gallery;
import models.Photo;
-import play.Logger;
import play.data.binding.As;
import play.data.validation.Match;
import play.data.validation.Required;
@@ -83,13 +79,13 @@ private static String cleanHashtag(String hashtag) {
public static void details(Long galleryId) {
Gallery gallery = Gallery.findById(galleryId);
- List<Photo> photos = Photo.findByGalleryAndRevalidate(gallery, 0L, 100L, 10);
+ List<Photo> photos = Photo.findByGalleryAndRevalidate(gallery, 10);
render(gallery, photos);
}
public static void getNewerPhoto(Long id, Long lastId){
Gallery gallery = Gallery.findById(id);
- List<Photo> photos = Photo.findByGalleryAndRevalidate(gallery, lastId, 0L, 10);
+ List<Photo> photos = Photo.findNewerByGalleryAndRevalidate(gallery, lastId, 10);
Map<String, Object> photoMap = new HashMap<String, Object>();
List<Object> photoList = new ArrayList<Object>();
Collections.reverse(photos);
@@ -108,7 +104,7 @@ public static void getNewerPhoto(Long id, Long lastId){
public static void getOlderPhoto(Long id, Long startId){
Gallery gallery = Gallery.findById(id);
- List<Photo> photos = Photo.findByGalleryAndRevalidate(gallery, 0L, startId, 10);
+ List<Photo> photos = Photo.findOlderByGalleryAndRevalidate(gallery, startId, 10);
Map<String, Object> photoMap = new HashMap<String, Object>();
List<Object> photoList = new ArrayList<Object>();
for (Photo photo : photos) {
View
34 app/models/Photo.java
@@ -60,10 +60,40 @@ public boolean hasExpired(long currentTimeMillis) {
return expires != null && currentTimeMillis > expires;
}
- public static List<Photo> findByGalleryAndRevalidate(Gallery gallery, Long startId, Long endId, int limit){
+ public static List<Photo> findByGalleryAndRevalidate(Gallery gallery, int limit) {
+ List<Photo> photos = Photo.find("gallery = ? ORDER BY id DESC", gallery).fetch(limit);
+ for (Photo photo : photos) {
+ if (photo.hasExpired(System.currentTimeMillis() + TEN_SECONDS)) {
+ revalidate(photo);
+ }
+ }
+ return photos;
+ }
+
+ public static List<Photo> findOlderByGalleryAndRevalidate(Gallery gallery, Long idOffset, int limit) {
+ List<Photo> photos = Photo.find("gallery = ? AND id < ? ORDER BY id DESC", gallery, idOffset).fetch(limit);
+ for (Photo photo : photos) {
+ if (photo.hasExpired(System.currentTimeMillis() + TEN_SECONDS)) {
+ revalidate(photo);
+ }
+ }
+ return photos;
+ }
+
+ public static List<Photo> findNewerByGalleryAndRevalidate(Gallery gallery, Long idOffset, int limit) {
+ List<Photo> photos = Photo.find("gallery = ? AND id > ? ORDER BY id DESC", gallery, idOffset).fetch(limit);
+ for (Photo photo : photos) {
+ if (photo.hasExpired(System.currentTimeMillis() + TEN_SECONDS)) {
+ revalidate(photo);
+ }
+ }
+ return photos;
+ }
+
+ public static List<Photo> findByGalleryAndRevalidate(Gallery gallery, Long startId, Long endId, int limit) {
startId = startId > 0 ? startId : 0;
endId = endId > 0 ? endId : startId + 50;
- List<Photo> photos = Photo.find("gallery = ? AND id > ? AND id < ? order by id DESC", gallery, startId, endId).fetch(limit);
+ List<Photo> photos = Photo.find("gallery = ? AND id > ? AND id < ? ORDER BY id DESC", gallery, startId, endId).fetch(limit);
for (Photo photo : photos) {
if (photo.hasExpired(System.currentTimeMillis() + TEN_SECONDS)) {
revalidate(photo);

No commit comments for this range

Something went wrong with that request. Please try again.