public void replace(String key, Object value, int expiration) {
   if (cache.get(key) == null) {
     return;
   }
   Element element = new Element(key, value);
   element.setTimeToLive(expiration);
   cache.put(element);
 }
Beispiel #2
0
 /**
  * Constructor
  *
  * @param key any non null value
  * @param value any value, including nulls
  * @param timeToIdleSeconds seconds to idle
  * @param timeToLiveSeconds seconds to live
  * @since 2.7.1
  */
 public Element(
     final Object key,
     final Object value,
     final int timeToIdleSeconds,
     final int timeToLiveSeconds) {
   this(key, value);
   setTimeToIdle(timeToIdleSeconds);
   setTimeToLive(timeToLiveSeconds);
 }
 public synchronized long incr(String key, int by) {
   Element e = cache.get(key);
   if (e == null) {
     return -1;
   }
   long newValue = ((Number) e.getValue()).longValue() + by;
   Element newE = new Element(key, newValue);
   newE.setTimeToLive(e.getTimeToLive());
   cache.put(newE);
   return newValue;
 }
Beispiel #4
0
  @Override
  public void addTicket(final Ticket ticketToAdd) {
    final Ticket ticket = encodeTicket(ticketToAdd);
    final Element element = new Element(ticket.getId(), ticket);

    final int idleValue = ticketToAdd.getExpirationPolicy().getTimeToIdle().intValue();
    element.setTimeToIdle(idleValue);

    final int aliveValue = ticketToAdd.getExpirationPolicy().getTimeToIdle().intValue();
    element.setTimeToLive(aliveValue);

    logger.debug(
        "Adding ticket {} to the cache {}", ticket.getId(), this.ehcacheTicketsCache.getName());
    this.ehcacheTicketsCache.put(element);
  }
Beispiel #5
0
 /**
  * Constructor used by ehcache-server
  *
  * @param key any non null value
  * @param value any value, including nulls
  * @param eternal specify as non-null to override cache configuration
  * @param timeToIdleSeconds specify as non-null to override cache configuration
  * @param timeToLiveSeconds specify as non-null to override cache configuration
  */
 public Element(
     Object key,
     Object value,
     Boolean eternal,
     Integer timeToIdleSeconds,
     Integer timeToLiveSeconds) {
   this.key = key;
   this.value = value;
   if (eternal != null) {
     setEternal(eternal.booleanValue());
   }
   if (timeToIdleSeconds != null) {
     setTimeToIdle(timeToIdleSeconds.intValue());
   }
   if (timeToLiveSeconds != null) {
     setTimeToLive(timeToLiveSeconds.intValue());
   }
   creationTime = System.currentTimeMillis();
 }
Beispiel #6
0
 /**
  * Constructor used by ehcache-server
  *
  * <p>timeToIdleSeconds and timeToLiveSeconds will have precedence over eternal. Which means that
  * what ever eternal says, non-null timeToIdleSeconds or timeToLiveSeconds will result in the
  * element not being eternal
  *
  * @param key any non null value
  * @param value any value, including nulls
  * @param eternal specify as non-null to override cache configuration
  * @param timeToIdleSeconds specify as non-null to override cache configuration
  * @param timeToLiveSeconds specify as non-null to override cache configuration
  * @deprecated
  */
 @Deprecated
 public Element(
     final Object key,
     final Object value,
     final Boolean eternal,
     final Integer timeToIdleSeconds,
     final Integer timeToLiveSeconds) {
   this.key = key;
   this.value = value;
   if (eternal != null) {
     setEternal(eternal.booleanValue());
   }
   if (timeToIdleSeconds != null) {
     setTimeToIdle(timeToIdleSeconds.intValue());
   }
   if (timeToLiveSeconds != null) {
     setTimeToLive(timeToLiveSeconds.intValue());
   }
   this.creationTime = getCurrentTime();
 }
Beispiel #7
0
  /**
   * Checks that the expiry thread runs and expires elements which has the effect of preventing the
   * disk store from continously growing. Ran for 6 hours through 10000 outer loops. No memory use
   * increase. Using a key of "key" + i * outer) you get early slots that cannot be reused. The
   * DiskStore actual size therefore starts at 133890 and ends at 616830. There is quite a lot of
   * space that cannot be used because of fragmentation. Question? Should an effort be made to
   * coalesce fragmented space? Unlikely in production to get contiguous fragments as in the first
   * form of this test.
   *
   * <p>Using a key of Integer.valueOf(i * outer) the size stays constant at 140800.
   *
   * @throws InterruptedException
   */
  @Test
  public void testExpiryWithSize() throws InterruptedException {
    Store diskStore = createDiskStore();
    diskStore.removeAll();

    byte[] data = new byte[1024];
    for (int outer = 1; outer <= 10; outer++) {
      for (int i = 0; i < 101; i++) {
        Element element = new Element(Integer.valueOf(i * outer), data);
        element.setTimeToLive(1);
        diskStore.put(element);
      }
      waitLonger();
      int predictedSize = (ELEMENT_ON_DISK_SIZE + 68) * 100;
      long actualSize = diskStore.getOnDiskSizeInBytes();
      LOG.info("Predicted Size: " + predictedSize + " Actual Size: " + actualSize);
      assertEquals(predictedSize, actualSize);
      LOG.info("Memory Use: " + measureMemoryUse());
    }
  }
Beispiel #8
0
 @Override
 public void putLBServerOffset(PageLetConfig pageLetConfig, int pos) {
   Element element = new Element(pageLetConfig.getURI(), pos);
   element.setTimeToLive(60 * 60);
   this.cchm.getCache("lb").put(element);
 }
Beispiel #9
0
 @Override
 public void put(PageLetConfig pageLetConfig, String html) {
   Element element = new Element(pageLetConfig.getURI(), html);
   element.setTimeToLive((int) pageLetConfig.getTtl());
   this.cchm.getCache("esi").put(element);
 }
 public void set(String key, Object value, int expiration) {
   Element element = new Element(key, value);
   element.setTimeToLive(expiration);
   cache.put(element);
 }