Esempio n. 1
0
  @RequestMapping("{cacheName}/{key}/details")
  @ResponseBody
  public Object keyDetail(
      @PathVariable("cacheName") String cacheName, @PathVariable("key") String key, Model model) {

    Element element = cacheManager.getCache(cacheName).get(key);

    String dataPattern = "yyyy-MM-dd hh:mm:ss";
    Map<String, Object> data = Maps.newHashMap();
    data.put("objectValue", element.getObjectValue().toString());
    data.put("size", PrettyMemoryUtils.prettyByteSize(element.getSerializedSize()));
    data.put("hitCount", element.getHitCount());

    Date latestOfCreationAndUpdateTime = new Date(element.getLatestOfCreationAndUpdateTime());
    data.put(
        "latestOfCreationAndUpdateTime",
        DateFormatUtils.format(latestOfCreationAndUpdateTime, dataPattern));
    Date lastAccessTime = new Date(element.getLastAccessTime());
    data.put("lastAccessTime", DateFormatUtils.format(lastAccessTime, dataPattern));
    if (element.getExpirationTime() == Long.MAX_VALUE) {
      data.put("expirationTime", "不过期");
    } else {
      Date expirationTime = new Date(element.getExpirationTime());
      data.put("expirationTime", DateFormatUtils.format(expirationTime, dataPattern));
    }

    data.put("timeToIdle", element.getTimeToIdle());
    data.put("timeToLive", element.getTimeToLive());
    data.put("version", element.getVersion());

    return data;
  }
Esempio n. 2
0
  private void writeElement(Element element, Serializable key) throws IOException {
    try {
      int bufferLength;
      long expirationTime = element.getExpirationTime();

      MemoryEfficientByteArrayOutputStream buffer = null;
      try {
        buffer = serializeEntry(element);

        bufferLength = buffer.size();
        DiskElement diskElement = checkForFreeBlock(bufferLength);

        // Write the record
        randomAccessFile.seek(diskElement.position);
        randomAccessFile.write(buffer.toByteArray(), 0, bufferLength);
        buffer = null;

        // Add to index, update stats
        diskElement.payloadSize = bufferLength;
        diskElement.key = key;
        diskElement.expiryTime = expirationTime;
        diskElement.hitcount = element.getHitCount();
        totalSize += bufferLength;
        lastElementSize = bufferLength;
        synchronized (diskElements) {
          diskElements.put(key, diskElement);
        }
      } catch (OutOfMemoryError e) {
        LOG.error("OutOfMemoryError on serialize: " + key);
      }

    } catch (Exception e) {
      // Catch any exception that occurs during serialization
      LOG.error(
          name
              + "Cache: Failed to write element to disk '"
              + key
              + "'. Initial cause was "
              + e.getMessage(),
          e);
    }
  }
Esempio n. 3
0
  public CacheStats getCacheStats() {
    CacheStats cs = new CacheStats();

    if (query.isCacheable()) {
      Cache cache = ccf.get(query);
      if (cache != null) {
        if (cache.isKeyInCache(getId())) {
          Element el = cache.get(getId());
          cs.setExpired(el == null || el.isExpired());

          if (el != null && !cs.isExpired()) {
            cs.setLastUpdate(el.getLastUpdateTime());
            cs.setExpTime(el.getExpirationTime());
            cs.setHitCount(el.getHitCount());
          }
        }
      }
    }
    return cs;
  }
Esempio n. 4
0
  /**
   * Constructor which takes an Ehcache core Element.
   *
   * <p>The {@link #mimeType} and {@link #value} are stored in the core Ehcache <code>value</code>
   * field using {@link net.sf.ehcache.MimeTypeByteArray}
   *
   * <p>If the MIME Type is not set, an attempt is made to set a sensible value. The rules for
   * setting the Mime Type are:
   *
   * <ol>
   *   <li>If the value in element is null, the <code>mimeType</code> is set to null.
   *   <li>If we stored the mimeType in ehcache, then <code>mimeType</code> is set with it.
   *   <li>If no mimeType was set and the value is a <code>byte[]</code> the <code>mimeType</code>
   *       is set to "application/octet-stream".
   *   <li>If no mimeType was set and the value is a <code>String</code> the <code>mimeType</code>
   *       is set to "text/plain".
   * </ol>
   *
   * @param element the ehcache core Element
   * @throws CacheException if an Exception occurred in the underlying cache.
   */
  public Element(net.sf.ehcache.Element element) throws CacheException {

    key = element.getKey();
    expirationDate = element.getExpirationTime();

    Object ehcacheValue = element.getObjectValue();

    if (ehcacheValue == null) {
      this.value = null;
      this.mimeType = null;
    }
    if (ehcacheValue instanceof MimeTypeByteArray) {
      // we have Mime Type data to extract
      mimeType = ((MimeTypeByteArray) ehcacheValue).getMimeType();
      if (mimeType == null) {
        // Jersey is returning */* for these which I think is wrong. Reported to Paul Sandoz.
        mimeType = "application/octet-stream";
      }
      this.value = ((MimeTypeByteArray) ehcacheValue).getValue();

    } else if (ehcacheValue instanceof byte[]) {
      // already a byte[]
      this.value = (byte[]) ehcacheValue;
      mimeType = "application/octet-stream";
    } else if (ehcacheValue instanceof String) {
      // a String such as XML
      this.value = ((String) ehcacheValue).getBytes();
      this.mimeType = "text/plain";
    } else {
      // A type we do not handle therefore serialize using Java Serialization
      MemoryEfficientByteArrayOutputStream stream = null;
      try {
        stream = MemoryEfficientByteArrayOutputStream.serialize(element.getValue());
      } catch (IOException e) {
        throw new CacheException(e);
      }
      this.value = stream.getBytes();
      this.mimeType = "application/x-java-serialized-object";
    }
  }