Search in sources :

Example 1 with IncludeCacheItem

use of lucee.runtime.cache.tag.include.IncludeCacheItem 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

IOException (java.io.IOException)1 BodyContent (javax.servlet.jsp.tagext.BodyContent)1 CacheHandler (lucee.runtime.cache.tag.CacheHandler)1 CacheHandlerPro (lucee.runtime.cache.tag.CacheHandlerPro)1 CacheItem (lucee.runtime.cache.tag.CacheItem)1 IncludeCacheItem (lucee.runtime.cache.tag.include.IncludeCacheItem)1 ErrorPage (lucee.runtime.err.ErrorPage)1 DevNullBodyContent (lucee.runtime.writer.DevNullBodyContent)1