Search in sources :

Example 6 with CacheHandlerPro

use of lucee.runtime.cache.tag.CacheHandlerPro 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)

Example 7 with CacheHandlerPro

use of lucee.runtime.cache.tag.CacheHandlerPro in project Lucee by lucee.

the class PageContextImpl method _doInclude.

// IMPORTANT!!! we do not getCachedWithin in this method, because Modern|ClassicAppListener is calling this method and in this case it should not be used
public void _doInclude(PageSource[] sources, boolean runOnce, Object cachedWithin) throws PageException {
    if (cachedWithin == null) {
        _doInclude(sources, runOnce);
        return;
    }
    // ignore call when runonce an it is not first call
    if (runOnce) {
        Page currentPage = PageSourceImpl.loadPage(this, sources);
        if (runOnce && includeOnce.contains(currentPage.getPageSource()))
            return;
    }
    // get cached data
    String cacheId = CacheHandlerCollectionImpl.createId(sources);
    CacheHandler cacheHandler = config.getCacheHandlerCollection(Config.CACHE_TYPE_INCLUDE, null).getInstanceMatchingObject(cachedWithin, null);
    if (cacheHandler instanceof CacheHandlerPro) {
        CacheItem cacheItem = ((CacheHandlerPro) cacheHandler).get(this, cacheId, cachedWithin);
        if (cacheItem instanceof IncludeCacheItem) {
            try {
                write(((IncludeCacheItem) cacheItem).getOutput());
                return;
            } catch (IOException e) {
                throw Caster.toPageException(e);
            }
        }
    } else if (cacheHandler != null) {
        // TODO this else block can be removed when all cache handlers implement CacheHandlerPro
        CacheItem cacheItem = cacheHandler.get(this, cacheId);
        if (cacheItem instanceof IncludeCacheItem) {
            try {
                write(((IncludeCacheItem) cacheItem).getOutput());
                return;
            } catch (IOException e) {
                throw Caster.toPageException(e);
            }
        }
    }
    // cached item not found, process and cache result if needed
    long start = System.nanoTime();
    BodyContent bc = pushBody();
    try {
        _doInclude(sources, runOnce);
        String out = bc.getString();
        if (cacheHandler != null) {
            CacheItem cacheItem = new IncludeCacheItem(out, ArrayUtil.isEmpty(sources) ? null : sources[0], System.nanoTime() - start);
            cacheHandler.set(this, cacheId, cachedWithin, cacheItem);
            return;
        }
    } finally {
        BodyContentUtil.flushAndPop(this, bc);
    }
}
Also used : BodyContent(javax.servlet.jsp.tagext.BodyContent) DevNullBodyContent(lucee.runtime.writer.DevNullBodyContent) IncludeCacheItem(lucee.runtime.cache.tag.include.IncludeCacheItem) CacheHandlerPro(lucee.runtime.cache.tag.CacheHandlerPro) ErrorPage(lucee.runtime.err.ErrorPage) CacheItem(lucee.runtime.cache.tag.CacheItem) IncludeCacheItem(lucee.runtime.cache.tag.include.IncludeCacheItem) IOException(java.io.IOException) CacheHandler(lucee.runtime.cache.tag.CacheHandler)

Aggregations

CacheHandler (lucee.runtime.cache.tag.CacheHandler)7 CacheHandlerPro (lucee.runtime.cache.tag.CacheHandlerPro)7 CacheItem (lucee.runtime.cache.tag.CacheItem)7 IOException (java.io.IOException)4 PageContextImpl (lucee.runtime.PageContextImpl)4 ApplicationException (lucee.runtime.exp.ApplicationException)4 Struct (lucee.runtime.type.Struct)3 Iterator (java.util.Iterator)2 BodyContent (javax.servlet.jsp.tagext.BodyContent)2 Log (lucee.commons.io.log.Log)2 ConfigImpl (lucee.runtime.config.ConfigImpl)2 SQLImpl (lucee.runtime.db.SQLImpl)2 DatabaseException (lucee.runtime.exp.DatabaseException)2 PageException (lucee.runtime.exp.PageException)2 Array (lucee.runtime.type.Array)2 ArrayImpl (lucee.runtime.type.ArrayImpl)2 Key (lucee.runtime.type.Collection.Key)2 QueryImpl (lucee.runtime.type.QueryImpl)2 StructImpl (lucee.runtime.type.StructImpl)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1