Search in sources :

Example 1 with Cached

use of play.cache.Cached in project japid42 by branaway.

the class CacheablePlayActionRunner method fillCacheFor.

//	/**
//	 * @param jr
//	 */
//	@Override
//	protected void cacheResult(RenderResult jr) {
//		if (shouldCache()) {
//			// already cached
//		}
//		else {
//			super.cacheResult(jr);
//		}
//	}
//	/**
//	 * check the cache for the action. The cache should have been caused by the CacheFor annotation
//	 * 
//	 * @param class1
//	 * @param actionName
//	 * @deprecated the logic is not robust. new logic has been implemented in the super class.  
//	 * 
//	 */
//	protected void checkActionCacheFor(Class<? extends JapidController> class1, String actionName) {
//		fillCacheFor(class1, actionName);
//		
//		if (cacheForVal != null && cacheForVal.length() > 0) {
//			String key = super.keyString;
//			try {
////			Object v = play.cache.Cache.get(key);
//				 RenderResult v = RenderResultCache.get(key);
//				if (v != null) {
//					System.out.println("got from cache");
//					this.gotFromCacheForCache = true;
//					throw new JapidResult(v);
////					if (v instanceof JapidResult) {
////						throw ((JapidResult) v);
////					} else if (v instanceof CachedRenderResult) {
////						throw new JapidResult(((CachedRenderResult) v).rr);
////					} else {
//////								throw new RuntimeException("got something from the cache but not sure what it is: "
//////										+ v.getClass().getName());
////					}
//				}
//			} catch (ShouldRefreshException e) {
//			}
//			return;
//		}				
//	}
/**
	 * @param class1
	 * @param actionName
	 */
private void fillCacheFor(Class<? extends JapidController> class1, String actionName) {
    String className = class1.getName();
    String cacheForKey = className + "_" + actionName;
    Integer cacheForVal = (Integer) JapidRenderer.getCache().get(cacheForKey);
    if (cacheForVal == null) {
        // the cache has not been filled up yet.
        Method[] mths = class1.getDeclaredMethods();
        for (Method m : mths) {
            if (m.getName().equalsIgnoreCase(actionName) && Modifier.isPublic(m.getModifiers())) {
                Cached cacheFor = m.getAnnotation(Cached.class);
                if (cacheFor == null) {
                    // well no annotation level cache spec
                    cacheForVal = 0;
                } else {
                    cacheForVal = cacheFor.duration();
                }
                JapidRenderer.getCache().put(cacheForKey, cacheForVal);
            }
        }
    }
    if (cacheForVal > 0) {
        super.noCache = false;
        // only override ttlAbs if it's not specified.
        if (super.ttlAbs == null || super.ttlAbs.length() == 0)
            super.ttlAbs = cacheForVal + "s";
    }
}
Also used : Cached(play.cache.Cached) Method(java.lang.reflect.Method)

Example 2 with Cached

use of play.cache.Cached in project japid42 by branaway.

the class GlobalSettingsWithJapid method onRequest.

@Override
public Action<?> onRequest(Request request, final Method actionMethod) {
    final String actionName = actionMethod.getDeclaringClass().getName() + "." + actionMethod.getName();
    final Map<String, String> threadData = JapidController.threadData.get();
    if (!cacheResponse) {
        return new Action.Simple() {

            public Promise<SimpleResult> call(Context ctx) throws Throwable {
                // pass the FQN to the japid controller to determine the
                // template to use
                // will be cleared right when the value is retrieved in the
                // japid controller
                // assuming the delegate call will take place in the same
                // thread
                threadData.put(ACTION_METHOD, actionName);
                Promise<SimpleResult> call = delegate.call(ctx);
                threadData.remove(ACTION_METHOD);
                return call;
            }
        };
    }
    return new Action<Cached>() {

        public Promise<SimpleResult> call(Context ctx) {
            try {
                beforeActionInvocation(ctx, actionMethod);
                SimpleResult result = null;
                Request req = ctx.request();
                String method = req.method();
                int duration = 0;
                String key = null;
                Cached cachAnno = actionMethod.getAnnotation(Cached.class);
                // Check the cache (only for GET or HEAD)
                if ((method.equals("GET") || method.equals("HEAD")) && cachAnno != null) {
                    key = cachAnno.key();
                    if ("".equals(key) || key == null) {
                        key = "urlcache:" + req.uri() + ":" + req.queryString();
                    }
                    duration = cachAnno.duration();
                    result = (SimpleResult) Cache.get(key);
                }
                if (result == null) {
                    // pass the action name hint to japid controller
                    threadData.put(ACTION_METHOD, actionName);
                    Promise<SimpleResult> ps = delegate.call(ctx);
                    threadData.remove(ACTION_METHOD);
                    if (!StringUtils.isEmpty(key) && duration > 0) {
                        result = ps.get(1, TimeUnit.MILLISECONDS);
                        Cache.set(key, result, duration);
                    }
                    onActionInvocationResult(ctx);
                    return ps;
                } else {
                    onActionInvocationResult(ctx);
                    return Promise.pure(result);
                }
            } catch (RuntimeException e) {
                throw e;
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
// return new Action.Simple() {
// public Result call(Context ctx) throws Throwable {
// beforeActionInvocation(ctx, actionMethod);
// dumpIt(ctx.request(), actionMethod);
// Result call = delegate.call(ctx);
// onActionInvocationResult(ctx);
// return call;
// }
// };
}
Also used : Context(play.mvc.Http.Context) Action(play.mvc.Action) Request(play.mvc.Http.Request) Cached(play.cache.Cached) SimpleResult(play.mvc.SimpleResult)

Aggregations

Cached (play.cache.Cached)2 Method (java.lang.reflect.Method)1 Action (play.mvc.Action)1 Context (play.mvc.Http.Context)1 Request (play.mvc.Http.Request)1 SimpleResult (play.mvc.SimpleResult)1