Exemplo n.º 1
0
  /** 取一个缓存 */
  public <F> V hget(K key, F field) {
    readLock.lock();
    try {
      Map<?, CacheObject<K, V>> coMap = _hCache.get(key);

      if (null == coMap) {
        return null;
      }

      CacheObject<K, V> co = coMap.get(field);

      if (null == co) {
        return null;
      }

      if (co.isExpired() == true) {
        coMap.remove(field);
        return null;
      }

      return co.getValue();
    } finally {
      readLock.unlock();
    }
  }
  public synchronized CacheObject searchObject(String name) {
    for (CacheObject mObject : getCache()) {
      if (mObject.getName().compareTo(name) == 0) {
        return mObject;
      }
    }

    return null;
  }
Exemplo n.º 3
0
 public void put(CacheObject r) {
   if (r.isStream()) {
     fifo.put(r);
   } else if (recentlyUsed.get(r.getPos()) != null) {
     lru.put(r);
   } else {
     fifo.put(r);
     lastUsed = r.getPos();
   }
 }
  public synchronized void printObjects() {
    logger.info("Logging metric objects ...");
    for (CacheObject mObject : getCache()) {
      logger.info("Name : " + mObject.getName());
      Iterator<Object> iob = mObject.getCacheObject().iterator();

      while (iob.hasNext()) {
        logger.info("Value : " + iob.next().toString());
      }
    }
  }
Exemplo n.º 5
0
 /** 取一个缓存 */
 public V get(K key) {
   readLock.lock();
   try {
     CacheObject<K, V> co = _mCache.get(key);
     if (co == null) {
       return null;
     }
     if (co.isExpired() == true) {
       _mCache.remove(key);
       return null;
     }
     return co.getValue();
   } finally {
     readLock.unlock();
   }
 }
Exemplo n.º 6
0
  /*
   * Remove operation with a flag so we can tell coherence if the remove was
   * caused by cache internal processing such as eviction or loading
   */
  public synchronized V remove(Object key, boolean internal) {
    //noinspection SuspiciousMethodCalls
    CacheObject<V> cacheObject = map.remove(key);
    // If the object is not in cache, stop trying to remove it.
    if (cacheObject == null) {
      return null;
    }
    // Remove from the cache order list
    cacheObject.lastAccessedListNode.remove();
    cacheObject.ageListNode.remove();
    // Remove references to linked list nodes
    cacheObject.ageListNode = null;
    cacheObject.lastAccessedListNode = null;

    return cacheObject.object;
  }
Exemplo n.º 7
0
 public CacheObject get(int pos) {
   CacheObject r = lru.find(pos);
   if (r != null) {
     return r;
   }
   r = fifo.find(pos);
   if (r != null && !r.isStream()) {
     if (recentlyUsed.get(pos) != null) {
       if (lastUsed != pos) {
         fifo.remove(pos);
         lru.put(r);
       }
     } else {
       recentlyUsed.put(pos, this);
     }
     lastUsed = pos;
   }
   return r;
 }
Exemplo n.º 8
0
  public synchronized V put(K key, V value) {
    V oldValue = null;
    // Delete an old entry if it exists.
    if (map.containsKey(key)) {
      oldValue = remove(key, true);
    }

    CacheObject<V> cacheObject = new CacheObject<V>(value);
    map.put(key, cacheObject);
    // Make an entry into the cache order list.
    // Store the cache order list entry so that we can get back to it
    // during later lookups.
    cacheObject.lastAccessedListNode = lastAccessedList.addFirst(key);
    // Add the object to the age list
    LinkedListNode ageNode = ageList.addFirst(key);
    ageNode.timestamp = System.currentTimeMillis();
    cacheObject.ageListNode = ageNode;

    // If cache is too full, remove least used cache entries until it is not too full.
    cullCache();

    return oldValue;
  }
Exemplo n.º 9
0
  /** {@inheritDoc} */
  public V get(K key) {
    readLock.lock();

    try {
      CacheObject<K, V> co = cacheMap.get(key);
      if (co == null) {
        missCount++;
        return null;
      }
      if (co.isExpired() == true) {
        // remove(key);		// can't upgrade the lock
        cacheMap.remove(key);

        missCount++;
        return null;
      }

      hitCount++;
      return co.getObject();
    } finally {
      readLock.unlock();
    }
  }
Exemplo n.º 10
0
  @Override
  public void doFilter(
      ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
      throws IOException, ServletException {

    HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
    HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

    String url = httpServletRequest.getRequestURI();

    httpServletResponse.setHeader(CACHE_HEADER, CacheState.SKIPPED.toString());

    if (!isURLAccepted(url)
        || !isQueryStringAccepted(httpServletRequest.getQueryString())
        || !isUserAgentAccepted(httpServletRequest.getHeader(Constants.HTTP_USER_AGENT_HEADER))) {
      LOGGER.debug("Skipping Cache filter for: {}?{}", url, httpServletRequest.getQueryString());
      LOGGER.debug("URL, QueryString or UserAgent not accepted");
      filterChain.doFilter(servletRequest, servletResponse);
      return;
    }

    long now = new Date().getTime();

    CacheObject cacheObject = cache.getIfPresent(url);

    boolean expireCache = httpServletRequest.getParameter(Constants.PARAM_EXPIRE_CACHE) != null;

    if (expireCache) {
      LOGGER.trace("Removing Cache for {}  due to URL parameter.", url);
      cache.invalidate(url);
    }

    boolean resetCache =
        httpServletRequest.getParameter(Constants.PARAM_RESET_CACHE) != null
            || resetTime > 0 && (now - lastResetTime) / 1000 > resetTime;

    if (resetCache) {
      LOGGER.trace("Resetting whole Cache for {} due to URL parameter.", url);
      cache.invalidateAll(); // fixme: we don't need reset since cache values are soft referenced.
      lastResetTime = now;
    }

    boolean skipCache =
        httpServletRequest.getParameter(Constants.PARAM_DEBUG) != null
            || httpServletRequest.getParameter(Constants.PARAM_SKIP_CACHE) != null;

    if (skipCache) {
      filterChain.doFilter(servletRequest, servletResponse);
      LOGGER.trace("Skipping Cache for {} due to URL parameter.", url);
      return;
    }

    List<String> requestedResources =
        findResourcesToMerge(httpServletRequest.getContextPath(), url);
    ServletContext context = filterConfig.getServletContext();
    String extensionOrPath = detectExtension(url); // in case of non js/css files it null
    if (extensionOrPath == null) {
      extensionOrPath =
          requestedResources.get(
              0); // non grouped i.e. non css/js file, we refer it's path in that case
    }

    JSCSSMergeServlet.ResourceStatus status =
        JSCSSMergeServlet.isNotModified(context, httpServletRequest, requestedResources, false);
    if (status.isNotModified()) {
      LOGGER.trace("Resources Not Modified. Sending 304.");
      cache.invalidate(url);
      JSCSSMergeServlet.sendNotModified(
          httpServletResponse,
          extensionOrPath,
          status.getActualETag(),
          DEFAULT_EXPIRES_MINUTES,
          DEFAULT_CACHE_CONTROL);
      return;
    }

    boolean cacheFound = false;

    if (cacheObject != null && cacheObject.getWebUtilitiesResponseWrapper() != null) {
      if (requestedResources != null
          && isAnyResourceModifiedSince(requestedResources, cacheObject.getTime(), context)) {
        LOGGER.trace("Some resources have been modified since last cache: {}", url);
        cache.invalidate(url);
        cacheFound = false;
      } else {
        LOGGER.trace("Found valid cached response.");
        // cacheObject.increaseAccessCount();
        cacheFound = true;
      }
    }

    if (cacheFound) {
      LOGGER.debug("Returning Cached response.");
      cacheObject.getWebUtilitiesResponseWrapper().fill(httpServletResponse);
      httpServletResponse.setHeader(CACHE_HEADER, CacheState.FOUND.toString());
      // fillResponseFromCache(httpServletResponse, cacheObject.getModuleResponse());
    } else {
      LOGGER.trace("Cache not found or invalidated");
      httpServletResponse.setHeader(CACHE_HEADER, CacheState.NOT_FOUND.toString());
      WebUtilitiesResponseWrapper wrapper = new WebUtilitiesResponseWrapper(httpServletResponse);
      filterChain.doFilter(servletRequest, wrapper);

      // some filters return no status code, but we believe that it is "200 OK"
      if (wrapper.getStatus() == 0) {
        wrapper.setStatus(200);
      }

      if (isMIMEAccepted(wrapper.getContentType())
          && !expireCache
          && !resetCache
          && wrapper.getStatus() == 200) { // Cache only 200 status response
        cache.put(url, new CacheObject(getLastModifiedFor(requestedResources, context), wrapper));
        LOGGER.debug("Cache added for: {}", url);
        httpServletResponse.setHeader(CACHE_HEADER, CacheState.ADDED.toString());
      } else {
        LOGGER.trace("Cache NOT added for: {}", url);
        LOGGER.trace("is MIME not accepted: {}", isMIMEAccepted(wrapper.getContentType()));
        LOGGER.trace("is expireCache: {}", expireCache);
        LOGGER.trace("is resetCache: {}", resetCache);
      }
      wrapper.fill(httpServletResponse);
    }
  }
Exemplo n.º 11
0
 public void put(CacheObject r) {
   baseCache.put(r);
   map.put(r.getPos(), r);
 }