Created
February 28, 2012 09:22
-
-
Save fehmicansaglam/1931497 to your computer and use it in GitHub Desktop.
Cache Manager for play.db.jpa.Model static methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void stats() { | |
Long userCount = CacheManager.get(CacheKey.USER_COUNT); | |
Long roleCount = CacheManager.get(CacheKey.ROLE_COUNT); | |
Long eventCount = CacheManager.get(CacheKey.EVENT_COUNT); | |
Long cumleCount = CacheManager.get(CacheKey.CUMLE_COUNT); | |
Long cumleEnabledCount = CacheManager.get(CacheKey.CUMLE_ENABLED_COUNT); | |
render(userCount, roleCount, eventCount, cumleCount, cumleEnabledCount); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CacheManager { | |
public enum CacheKey { | |
USER_COUNT(User.class, "count"), | |
EVENT_COUNT(Event.class, "count"), | |
//Callable parametre örneği | |
ROLE_COUNT(new Callable<Long>() { | |
@Override | |
public Long call() throws Exception { | |
return Role.count(); | |
} | |
}), | |
CUMLE_COUNT(Cumle.class, "count"), | |
CUMLE_ENABLED_COUNT(Cumle.class, | |
"count", | |
new Class<?>[] { String.class, Object[].class }, | |
"enabled=?", | |
new Object[] { Boolean.TRUE }); | |
private Method method; | |
private Object[] params; | |
private Callable<? extends Serializable> callable; | |
private CacheKey(Class<? extends Model> model, String method, Class<?>[] parameterTypes, Object... params) { | |
try { | |
this.method = model.getDeclaredMethod(method, parameterTypes); | |
this.params = params; | |
} catch (SecurityException e) { | |
throw new UnexpectedException(e); | |
} catch (NoSuchMethodException e) { | |
throw new UnexpectedException(e); | |
} | |
} | |
private CacheKey(Class<? extends Model> model, String method) { | |
this(model, method, null); | |
} | |
private CacheKey(Callable<? extends Serializable> callable) { | |
this.callable = callable; | |
} | |
public <T> T invoke() throws Exception { | |
if (this.callable == null) | |
return (T) this.method.invoke(null, params); | |
else | |
return (T) callable.call(); | |
} | |
} | |
private static final String EXPIRE = "5mn"; | |
private CacheManager() { | |
} | |
public static <T extends Serializable> T setIfAbsentAndGet(final CacheKey cacheKey) { | |
final String key = cacheKey.name(); | |
T value = (T) Cache.get(key); | |
if (value == null) { | |
Logger.info("CACHE MISS: %s", key); | |
try { | |
value = cacheKey.invoke(); | |
} catch (Exception e) { | |
throw new UnexpectedException(e); | |
} | |
Cache.set(key, value, EXPIRE); | |
} else { | |
Logger.info("CACHE HIT: %s", key); | |
} | |
return value; | |
} | |
public static Long get(final CacheKey cacheKey) { | |
return setIfAbsentAndGet(cacheKey); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment