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());
}
}
Aggregations