Exemple #1
0
  /* ------------------------------------------------------------ */
  private HttpContent load(String pathInContext, Resource resource) throws IOException {
    Content content = null;

    if (resource == null || !resource.exists()) return null;

    // Will it fit in the cache?
    if (!resource.isDirectory() && isCacheable(resource)) {
      // Create the Content (to increment the cache sizes before adding the content
      content = new Content(pathInContext, resource);

      // reduce the cache to an acceptable size.
      shrinkCache();

      // Add it to the cache.
      Content added = _cache.putIfAbsent(pathInContext, content);
      if (added != null) {
        content.invalidate();
        content = added;
      }

      return content;
    }

    return new HttpContent.ResourceAsHttpContent(
        resource,
        _mimeTypes.getMimeByExtension(resource.toString()),
        getMaxCachedFileSize(),
        _etags);
  }
Exemple #2
0
  /* ------------------------------------------------------------ */
  private void shrinkCache() {
    // While we need to shrink
    while (_cache.size() > 0
        && (_cachedFiles.get() > _maxCachedFiles || _cachedSize.get() > _maxCacheSize)) {
      // Scan the entire cache and generate an ordered list by last accessed time.
      SortedSet<Content> sorted =
          new TreeSet<Content>(
              new Comparator<Content>() {
                public int compare(Content c1, Content c2) {
                  if (c1._lastAccessed < c2._lastAccessed) return -1;

                  if (c1._lastAccessed > c2._lastAccessed) return 1;

                  if (c1._length < c2._length) return -1;

                  return c1._key.compareTo(c2._key);
                }
              });
      for (Content content : _cache.values()) sorted.add(content);

      // Invalidate least recently used first
      for (Content content : sorted) {
        if (_cachedFiles.get() <= _maxCachedFiles && _cachedSize.get() <= _maxCacheSize) break;
        if (content == _cache.remove(content.getKey())) content.invalidate();
      }
    }
  }
Exemple #3
0
 /* ------------------------------------------------------------ */
 public void flushCache() {
   if (_cache != null) {
     while (_cache.size() > 0) {
       for (String path : _cache.keySet()) {
         Content content = _cache.remove(path);
         if (content != null) content.invalidate();
       }
     }
   }
 }