Search in sources :

Example 1 with FileCacheItem

use of lucee.runtime.cache.tag.file.FileCacheItem in project Lucee by lucee.

the class FileTag method actionRead.

/**
 * read source file
 * @throws PageException
 */
private void actionRead(boolean isBinary) throws PageException {
    if (variable == null)
        throw new ApplicationException("attribute variable is not defined for tag file");
    // check if we can use cache
    if (StringUtil.isEmpty(cachedWithin)) {
        Object tmp = ((PageContextImpl) pageContext).getCachedWithin(ConfigWeb.CACHEDWITHIN_FILE);
        if (tmp != null)
            setCachedwithin(tmp);
    }
    String cacheId = createCacheId(isBinary);
    CacheHandler cacheHandler = null;
    if (cachedWithin != null) {
        cacheHandler = pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_FILE, null).getInstanceMatchingObject(cachedWithin, null);
        if (cacheHandler instanceof CacheHandlerPro) {
            CacheItem cacheItem = ((CacheHandlerPro) cacheHandler).get(pageContext, cacheId, cachedWithin);
            if (cacheItem instanceof FileCacheItem) {
                pageContext.setVariable(variable, ((FileCacheItem) cacheItem).getData());
                return;
            }
        } else if (cacheHandler != null) {
            // TODO this else block can be removed when all cache handlers implement CacheHandlerPro
            CacheItem cacheItem = cacheHandler.get(pageContext, cacheId);
            if (cacheItem instanceof FileCacheItem) {
                pageContext.setVariable(variable, ((FileCacheItem) cacheItem).getData());
                return;
            }
        }
    }
    // cache not found, process and cache result if needed
    checkFile(pageContext, securityManager, file, serverPassword, false, false, true, false);
    try {
        long start = System.nanoTime();
        Object data = isBinary ? IOUtil.toBytes(file) : IOUtil.toString(file, CharsetUtil.toCharset(charset));
        pageContext.setVariable(variable, data);
        if (cacheHandler != null)
            cacheHandler.set(pageContext, cacheId, cachedWithin, FileCacheItem.getInstance(file.getAbsolutePath(), data, System.nanoTime() - start));
    } catch (IOException e) {
        throw new ApplicationException("can't read file [" + file.toString() + "]", e.getMessage());
    }
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) CacheHandlerPro(lucee.runtime.cache.tag.CacheHandlerPro) FileCacheItem(lucee.runtime.cache.tag.file.FileCacheItem) PageContextImpl(lucee.runtime.PageContextImpl) FileCacheItem(lucee.runtime.cache.tag.file.FileCacheItem) CacheItem(lucee.runtime.cache.tag.CacheItem) IOException(java.io.IOException) CacheHandler(lucee.runtime.cache.tag.CacheHandler)

Aggregations

IOException (java.io.IOException)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 FileCacheItem (lucee.runtime.cache.tag.file.FileCacheItem)1 ApplicationException (lucee.runtime.exp.ApplicationException)1