Search in sources :

Example 1 with IHttpResourceCache

use of org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResourceCache in project scout.rt by eclipse.

the class PrebuildFiles method buildResources.

/**
 * Pre-builds the HTML files and the referenced scripts (JS, CSS) in the HTML document. Each document is put into the
 * HTTP resource cache, so we don't have to build them again later. However, we have to make a few assumptions here.
 * Since we have no HTTP request or session at this point, we assume:
 * <ul>
 * <li>the default locale</li>
 * <li>the default theme</li>
 * <li>minifying is enabled</li>
 * <li>caching is enabled</li>
 * </ul>
 */
protected void buildResources() {
    long t0 = System.nanoTime();
    List<String> files = CONFIG.getPropertyValue(UiPrebuildFilesProperty.class);
    IHttpResourceCache httpResourceCache = BEANS.get(GlobalHttpResourceCache.class);
    for (String file : files) {
        LOG.info("Pre-building resource '{}'...", file);
        try {
            HttpCacheObject cacheObject = loadResource(file);
            httpResourceCache.put(cacheObject);
        } catch (IOException e) {
            LOG.error("Failed to load resource", e);
        }
    }
    LOG.info("Finished pre-building of {} web resources in {} ms", files.size(), StringUtility.formatNanos(System.nanoTime() - t0));
}
Also used : IOException(java.io.IOException) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) IHttpResourceCache(org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResourceCache)

Example 2 with IHttpResourceCache

use of org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResourceCache in project scout.rt by eclipse.

the class ResourceRequestHandler method resolveResourceFromCache.

protected HttpCacheObject resolveResourceFromCache(HttpServletRequest req, String pathInfoEx, IResourceLoader resourceLoader) throws IOException {
    // Create cache key for resource and check if resource exists in cache
    HttpCacheKey cacheKey = resourceLoader.createCacheKey(pathInfoEx);
    // When caching is disabled, always load resource
    if (!UrlHints.isCacheHint(req)) {
        LOG.debug("Requested resource with cacheKey={}. Caching is disabled by URL hint", cacheKey);
        return resourceLoader.loadResource(cacheKey);
    }
    IHttpResourceCache resourceCache = resourceLoader.getCache(cacheKey);
    if (resourceCache == null) {
        LOG.debug("Loader for resource with cacheKey={} does not support caching.", cacheKey);
        return resourceLoader.loadResource(cacheKey);
    }
    String cacheResultMsg;
    HttpCacheObject resource = resourceCache.get(cacheKey);
    if (resource == null) {
        // Cache miss: resource not found in cache --> load it
        resource = resourceLoader.loadResource(cacheKey);
        if (resource == null) {
            cacheResultMsg = "Resource is not cached (cache miss), could not load resource (not added to the cache)";
        } else {
            resourceCache.put(resource);
            cacheResultMsg = "Resource is not cached (cache miss), resource loaded and added to the cache";
        }
    } else {
        // Cache hit
        cacheResultMsg = "Resource found in cache (cache hit), using cached resource";
    }
    LOG.debug("Requested resource with cacheKey={}. {}", cacheKey, cacheResultMsg);
    return resource;
}
Also used : HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) IHttpResourceCache(org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResourceCache)

Aggregations

HttpCacheObject (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)2 IHttpResourceCache (org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResourceCache)2 IOException (java.io.IOException)1 HttpCacheKey (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey)1