Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Merge branch 'cache-attachment' of dlab/hive
Browse files Browse the repository at this point in the history
from pull-request 1460

* refs/heads/cache-attachment:
  Added AttachmentCache

Reviewed-by: 백기선 <keesun.baik@navercorp.com>
  • Loading branch information
백기선 committed Feb 5, 2015
2 parents f6c0ae2 + 0844910 commit 0a863e5
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 4 deletions.
38 changes: 34 additions & 4 deletions app/models/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package models;

import utils.AttachmentCache;
import controllers.AttachmentApp;
import models.enumeration.ResourceType;
import models.resource.GlobalResource;
Expand Down Expand Up @@ -108,9 +109,16 @@ public static boolean exists(String hash) {
*/
public static List<Attachment> findByContainer(
ResourceType containerType, String containerId) {
return find.where()
List<Attachment> cachedData = AttachmentCache.get(containerType, containerId);
if (cachedData != null) {
return cachedData;
}

List<Attachment> list = find.where()
.eq("containerType", containerType)
.eq("containerId", containerId).findList();
AttachmentCache.set(containerType.name() + containerId, list);
return list;
}

/**
Expand All @@ -120,7 +128,14 @@ public static List<Attachment> findByContainer(
* @return attachments of the container
*/
public static List<Attachment> findByContainer(Resource container) {
return findByContainer(container.getType(), container.getId());
List<Attachment> cachedData = AttachmentCache.get(container);
if (cachedData != null) {
return cachedData;
}

List<Attachment> list = findByContainer(container.getType(), container.getId());
AttachmentCache.set(container, list);
return list;
}

/**
Expand Down Expand Up @@ -289,7 +304,8 @@ public static boolean fileExists(String hash) {
}

/**
* Deletes this file.
* Deletes this file and remove cache that contains it.
* However, the cache can not be removed if Ebean.delete() is directly used or called by cascading.
*
* This method is used when an user delete an attachment or its container.
*/
Expand All @@ -311,6 +327,18 @@ public void delete() {
play.Logger.error("Failed to delete: " + this, e);
}
}

AttachmentCache.remove(this);
}

/**
* Update this file and remove cache that contains it.
* However, the cache can not be removed if Ebean.update() is directly used or called by cascading.
*/
@Override
public void update() {
super.update();
AttachmentCache.remove(this);
}


Expand Down Expand Up @@ -434,7 +462,7 @@ private String removeUserTemporaryFiles() {
attachment.delete();
deletedFileCount++;
}
if( attachmentList.size() != deletedFileCount) {
if (attachmentList.size() != deletedFileCount) {
play.Logger.error(
String.format("Failed to delete user temporary files.\nExpected: %d Actual: %d",
attachmentList.size(), deletedFileCount)
Expand Down Expand Up @@ -524,6 +552,8 @@ private boolean save(File file, String fileName, Resource container) throws
this.name = fileName;
}

AttachmentCache.remove(this);

// Add the attachment into the Database only if there is no same record.
Attachment sameAttach = Attachment.findBy(this);
if (sameAttach == null) {
Expand Down
104 changes: 104 additions & 0 deletions app/utils/AttachmentCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package utils;

import models.Attachment;
import models.enumeration.ResourceType;
import models.resource.Resource;
import play.cache.Cache;

import java.util.List;

/**
* Utility for caching attachments in a container {@link models.resource.Resource}.
*
* The key is made by the name of the type of the Resource and Resource's id.
* i.e) resource.getType().name() + resource.getId()
*
* The value is a collection of attachments.
*
* @author Keeun Baik
*/
public class AttachmentCache {

/**
* Play's Cache API allows expiration time in seconds.
*
* @see {@link play.cache.Cache#set(String, Object, int)}
*/
private static final int ONE_DAY = 60 * 60 * 24;

/**
* Find cached attachments with the key is generated by
* combining {@code containerType} and {@code containerId}.
*
* @param containerType
* @param containerId
* @return found cached data or null if there is no cached data.
*/
public static List<Attachment> get(ResourceType containerType, String containerId) {
String cacheKey = containerType.name() + containerId;
Object cachedData = Cache.get(cacheKey);
if (cachedData != null) {
return (List<Attachment>) cachedData;
} else {
return null;
}
}

/**
* Cache attachments with the key
*
* @param key The key should be generated by combining {@code containerType} and {@code containerId}.
* @param list
*/
public static void set(String key, List<Attachment> list) {
Cache.set(key, list, ONE_DAY);
}

/**
* Cache attachments with the key is generated by {@code container}
*
* @param container
* @param list
*/
public static void set(Resource container, List<Attachment> list) {
Cache.set(cacheKey(container), list, ONE_DAY);
}

/**
* Find cached attachments with the key is generated by {@code container}
*
* @param container
* @return
*/
public static List<Attachment> get(Resource container) {
String cacheKey = cacheKey(container);
Object cachedData = Cache.get(cacheKey);
if (cachedData != null) {
return (List<Attachment>) cachedData;
} else {
return null;
}
}

private static String cacheKey(Resource container) {
return container.getType().name() + container.getId();
}

/**
* Remove cached attachments with the key is generated by {@code container}
*
* @param container
*/
public static void remove(Resource container) {
Cache.remove(cacheKey(container));
}

/**
* Remove cache that contains the {@code attachment}
*
* @param attachment
*/
public static void remove(Attachment attachment) {
Cache.remove(attachment.containerType.name() + attachment.containerId);
}
}
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ libraryDependencies ++= Seq(
javaCore,
javaJdbc,
javaEbean,
cache,
// Add your project dependencies here,
"com.h2database" % "h2" % "1.4.184",
// Core Library
Expand Down

0 comments on commit 0a863e5

Please sign in to comment.