Search in sources :

Example 1 with UDFCacheItem

use of lucee.runtime.cache.tag.udf.UDFCacheItem in project Lucee by lucee.

the class UDFImpl method _callCachedWithin.

private Object _callCachedWithin(PageContext pc, Collection.Key calledName, Object[] args, Struct values, boolean doIncludePath) throws PageException {
    PageContextImpl pci = (PageContextImpl) pc;
    Object cachedWithin = getCachedWithin(pc);
    String cacheId = CacheHandlerCollectionImpl.createId(this, args, values);
    CacheHandler cacheHandler = pc.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_FUNCTION, null).getInstanceMatchingObject(getCachedWithin(pc), null);
    if (cacheHandler instanceof CacheHandlerPro) {
        CacheItem cacheItem = ((CacheHandlerPro) cacheHandler).get(pc, cacheId, cachedWithin);
        if (cacheItem instanceof UDFCacheItem) {
            UDFCacheItem entry = (UDFCacheItem) cacheItem;
            try {
                pc.write(entry.output);
            } catch (IOException e) {
                throw Caster.toPageException(e);
            }
            return entry.returnValue;
        }
    } else if (cacheHandler != null) {
        // TODO this else block can be removed when all cache handlers implement CacheHandlerPro
        CacheItem cacheItem = cacheHandler.get(pc, cacheId);
        if (cacheItem instanceof UDFCacheItem) {
            UDFCacheItem entry = (UDFCacheItem) cacheItem;
            // if(entry.creationdate+properties.cachedWithin>=System.currentTimeMillis()) {
            try {
                pc.write(entry.output);
            } catch (IOException e) {
                throw Caster.toPageException(e);
            }
            return entry.returnValue;
        // }
        // cache.remove(id);
        }
    }
    // cached item not found, process and cache result if needed
    long start = System.nanoTime();
    // execute the function
    BodyContent bc = pci.pushBody();
    try {
        Object rtn = _call(pci, calledName, args, values, doIncludePath);
        if (cacheHandler != null) {
            String out = bc.getString();
            cacheHandler.set(pc, cacheId, cachedWithin, new UDFCacheItem(out, rtn, getFunctionName(), getSource(), System.nanoTime() - start));
        }
        return rtn;
    } finally {
        BodyContentUtil.flushAndPop(pc, bc);
    }
}
Also used : UDFCacheItem(lucee.runtime.cache.tag.udf.UDFCacheItem) BodyContent(javax.servlet.jsp.tagext.BodyContent) CacheHandlerPro(lucee.runtime.cache.tag.CacheHandlerPro) PageContextImpl(lucee.runtime.PageContextImpl) CacheItem(lucee.runtime.cache.tag.CacheItem) UDFCacheItem(lucee.runtime.cache.tag.udf.UDFCacheItem) IOException(java.io.IOException) CacheHandler(lucee.runtime.cache.tag.CacheHandler)

Aggregations

IOException (java.io.IOException)1 BodyContent (javax.servlet.jsp.tagext.BodyContent)1 PageContextImpl (lucee.runtime.PageContextImpl)1 CacheHandler (lucee.runtime.cache.tag.CacheHandler)1 CacheHandlerPro (lucee.runtime.cache.tag.CacheHandlerPro)1 CacheItem (lucee.runtime.cache.tag.CacheItem)1 UDFCacheItem (lucee.runtime.cache.tag.udf.UDFCacheItem)1