示例#1
0
文件: HttpPath.java 项目: dlitz/resin
  protected CacheEntry getCache() {
    if (_cacheEntry == null) {
      synchronized (_cache) {
        _cacheEntry = _cache.get(getPath());
        if (_cacheEntry == null) {
          _cacheEntry = new CacheEntry();
          _cache.put(getPath(), _cacheEntry);
        }
      }
    }

    long now;

    now = Alarm.getCurrentTime();

    synchronized (_cacheEntry) {
      try {
        if (_cacheEntry.expires > now) return _cacheEntry;

        HttpStreamWrapper stream = (HttpStreamWrapper) openReadImpl();
        stream.setHead(true);
        stream.setSocketTimeout(120000);

        String status = (String) stream.getAttribute("status");
        if (status.equals("200")) {
          String lastModified = (String) stream.getAttribute("last-modified");

          _cacheEntry.lastModified = 0;
          if (lastModified != null) {
            QDate date = QDate.getGlobalDate();
            synchronized (date) {
              _cacheEntry.lastModified = date.parseDate(lastModified);
            }
          }

          String length = (String) stream.getAttribute("content-length");
          _cacheEntry.length = 0;
          if (length != null) {
            _cacheEntry.length = Integer.parseInt(length);
          }
        } else _cacheEntry.lastModified = -1;

        _cacheEntry.expires = now + 5000;

        stream.close();
        return _cacheEntry;
      } catch (Exception e) {
        _cacheEntry.lastModified = -1;
        _cacheEntry.expires = now + 5000;

        return _cacheEntry;
      }
    }
  }
示例#2
0
 /*
  * (non-Javadoc)
  *
  * @see ch.elexis.data.cache.IPersistentObjectCache#purge()
  */
 public void purge() {
   synchronized (cache) {
     Iterator<Entry<K, CacheEntry>> it = cache.entrySet().iterator();
     long freeBefore = Runtime.getRuntime().freeMemory();
     while (it.hasNext()) {
       Entry<K, CacheEntry> e = it.next();
       CacheEntry ce = e.getValue();
       ce.expires = 0;
       ce.get();
       it.remove();
     }
     if (Hub.plugin.DEBUGMODE) {
       long freeAfter = Runtime.getRuntime().freeMemory();
       StringBuilder sb = new StringBuilder();
       sb.append("Cache purge: Free memore before: ")
           .append(freeBefore)
           .append(", free memory after: ")
           .append(freeAfter)
           .append("\n");
       Hub.log.log(sb.toString(), Log.INFOS);
     }
   }
 }