Example #1
0
  @Override
  public Ticket getTicket(final String ticketIdToGet) {
    final String ticketId = encodeTicketId(ticketIdToGet);
    if (ticketId == null) {
      return null;
    }

    final Element element = this.ehcacheTicketsCache.get(ticketId);
    if (element == null) {
      logger.debug("No ticket by id [{}] is found in the registry", ticketId);
      return null;
    }
    final Ticket ticket = decodeTicket((Ticket) element.getObjectValue());

    final CacheConfiguration config = new CacheConfiguration();
    config.setTimeToIdleSeconds(ticket.getExpirationPolicy().getTimeToIdle());
    config.setTimeToLiveSeconds(ticket.getExpirationPolicy().getTimeToIdle());

    if (element.isExpired(config) || ticket.isExpired()) {
      logger.debug("Ticket {} has expired", ticket.getId());
      this.ehcacheTicketsCache.evictExpiredElements();
      this.ehcacheTicketsCache.flush();
      return null;
    }

    return ticket;
  }
Example #2
0
  /**
   * Removes expired elements.
   *
   * <p>Note that the DiskStore cannot efficiently expire based on TTI. It does it on TTL. However
   * any gets out of the DiskStore are check for both before return.
   *
   * @noinspection SynchronizeOnNonFinalField
   */
  public void expireElements() {
    final long now = System.currentTimeMillis();

    // Clean up the spool
    synchronized (spoolLock) {
      for (Iterator iterator = spool.values().iterator(); iterator.hasNext(); ) {
        final Element element = (Element) iterator.next();
        if (element.isExpired()) {
          // An expired element
          if (LOG.isDebugEnabled()) {
            LOG.debug(name + "Cache: Removing expired spool element " + element.getObjectKey());
          }
          iterator.remove();
          notifyExpiryListeners(element);
        }
      }
    }

    Element element = null;
    RegisteredEventListeners listeners = cache.getCacheEventNotificationService();
    synchronized (diskElements) {
      // Clean up disk elements
      for (Iterator iterator = diskElements.entrySet().iterator(); iterator.hasNext(); ) {
        final Map.Entry entry = (Map.Entry) iterator.next();
        final DiskElement diskElement = (DiskElement) entry.getValue();

        if (now >= diskElement.expiryTime) {
          // An expired element
          if (LOG.isDebugEnabled()) {
            LOG.debug(
                name
                    + "Cache: Removing expired spool element "
                    + entry.getKey()
                    + " from Disk Store");
          }

          iterator.remove();

          // only load the element from the file if there is a listener interested in hearing about
          // its expiration
          if (listeners.hasCacheEventListeners()) {
            try {
              element = loadElementFromDiskElement(diskElement);
              notifyExpiryListeners(element);
            } catch (Exception exception) {
              LOG.error(
                  name
                      + "Cache: Could not remove disk store entry for "
                      + entry.getKey()
                      + ". Error was "
                      + exception.getMessage(),
                  exception);
            }
          }
          freeBlock(diskElement);
        }
      }
    }
  }
Example #3
0
 private void notifyEvictionOrExpiry(final Element element) {
   if (element != null && cacheEventNotificationService != null) {
     if (element.isExpired()) {
       cacheEventNotificationService.notifyElementExpiry(element, false);
     } else {
       cacheEventNotificationService.notifyElementEvicted(element, false);
     }
   }
 }
  @Override
  public SellingContext getByGuid(final String sellingContextGuid) {
    Element element = getCache().get(sellingContextGuid);
    if (element != null && !element.isExpired()) {
      LOG.debug("get objecct from cache... guid: " + sellingContextGuid);
      return (SellingContext) element.getValue();
    }

    LOG.debug("get objecct from database... guid: " + sellingContextGuid);
    SellingContext sellingContext = sellingContextService.getByGuid(sellingContextGuid);
    cache.put(new Element(sellingContextGuid, sellingContext));

    return sellingContext;
  }
Example #5
0
  @SuppressWarnings("unchecked")
  public C getCached() {
    if (query.isCacheable()) {
      Cache cache = ccf.get(query);
      if (cache == null) {

        if (log.isDebugEnabled()) {
          log.debug(String.format("Cache for %s not found", this));
        }

      } else {

        if (log.isDebugEnabled()) {
          log.debug(String.format("Cache for %s found", this));
        }

        if (cache.isKeyInCache(getId())) {

          Element el = cache.get(getId());
          boolean expired = (el == null || el.isExpired());

          if (log.isDebugEnabled()) {

            log.debug(
                String.format(
                    "Element %d found in %s cache (expired = %s)",
                    getId(), query.getQid(), expired));
          }

          if (el != null && !expired) {
            return (C) el.getObjectValue();
          }

        } else {

          if (log.isDebugEnabled()) {
            log.debug(String.format("Element %d not found in %s cache", getId(), query.getQid()));
          }
        }
      }
    }

    return (C) null;
  }
Example #6
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;
  }
Example #7
0
  public Element[] getRandomValues(final int size, Object keyHint) {
    ArrayList<Element> sampled = new ArrayList<Element>(size * 2);

    // pick a random starting point in the map
    int randomHash = rndm.nextInt();

    final int segmentStart;
    if (keyHint == null) {
      segmentStart = (randomHash >>> segmentShift) & segmentMask;
    } else {
      segmentStart = (hash(keyHint.hashCode()) >>> segmentShift) & segmentMask;
    }

    int segmentIndex = segmentStart;
    do {
      final HashEntry[] table = segments[segmentIndex].table;
      final int tableStart = randomHash & (table.length - 1);
      int tableIndex = tableStart;
      do {
        for (HashEntry e = table[tableIndex]; e != null; e = e.next) {
          Element value = e.value;
          if (value != null && (!(e.pinned && elementPinningEnabled) || value.isExpired())) {
            sampled.add(value);
          }
        }

        if (sampled.size() >= size) {
          return sampled.toArray(new Element[sampled.size()]);
        }

        // move to next table slot
        tableIndex = (tableIndex + 1) & (table.length - 1);
      } while (tableIndex != tableStart);

      // move to next segment
      segmentIndex = (segmentIndex + 1) & segmentMask;
    } while (segmentIndex != segmentStart);

    return sampled.toArray(new Element[sampled.size()]);
  }
Example #8
0
  /**
   * Removes the element chosen by the eviction policy
   *
   * @param elementJustAdded it is possible for this to be null
   */
  protected void removeElementChosenByEvictionPolicy(Element elementJustAdded) {

    if (LOG.isLoggable(Level.FINEST)) {
      LOG.finest("Cache is full. Removing element ...");
    }

    Element element = findEvictionCandidate(elementJustAdded);
    // this CAN happen rarely. Let the store get one bigger
    if (element == null) {
      LOG.info("Eviction selection miss. Selected element is null");
      return;
    }

    // If the element is expired remove
    if (element.isExpired()) {
      remove(element.getObjectKey());
      notifyExpiry(element);
      return;
    }

    evict(element);
    remove(element.getObjectKey());
  }