protected ICacheElement get()

in commons-jcs3-core/src/main/java/org/apache/commons/jcs3/engine/control/CompositeCache.java [497:606]


    protected ICacheElement<K, V> get(final K key, final boolean localOnly)
    {
        ICacheElement<K, V> element = null;

        boolean found = false;

        log.debug("get: key = {0}, localOnly = {1}", key, localOnly);

        try
        {
            // First look in memory cache
            element = memCache.get(key);

            if (element != null)
            {
                // Found in memory cache
                if (isExpired(element))
                {
                    log.debug("{0} - Memory cache hit, but element expired",
                            () -> cacheAttr.getCacheName());

                    doExpires(element);
                    element = null;
                }
                else
                {
                    log.debug("{0} - Memory cache hit", () -> cacheAttr.getCacheName());

                    // Update counters
                    hitCountRam.incrementAndGet();
                }

                found = true;
            }
            else
            {
                // Item not found in memory. If local invocation look in aux
                // caches, even if not local look in disk auxiliaries
                for (final AuxiliaryCache<K, V> aux : auxCaches)
                {
                    final CacheType cacheType = aux.getCacheType();

                    if (!localOnly || cacheType == CacheType.DISK_CACHE)
                    {
                        log.debug("Attempting to get from aux [{0}] which is of type: {1}",
                                aux::getCacheName, () -> cacheType);

                        try
                        {
                            element = aux.get(key);
                        }
                        catch (final IOException e)
                        {
                            log.error("Error getting from aux", e);
                        }
                    }

                    log.debug("Got CacheElement: {0}", element);

                    // Item found in one of the auxiliary caches.
                    if (element != null)
                    {
                        if (isExpired(element))
                        {
                            log.debug("{0} - Aux cache[{1}] hit, but element expired.",
                                    () -> cacheAttr.getCacheName(), aux::getCacheName);

                            // This will tell the remotes to remove the item
                            // based on the element's expiration policy. The elements attributes
                            // associated with the item when it created govern its behavior
                            // everywhere.
                            doExpires(element);
                            element = null;
                        }
                        else
                        {
                            log.debug("{0} - Aux cache[{1}] hit.",
                                    () -> cacheAttr.getCacheName(), aux::getCacheName);

                            // Update counters
                            hitCountAux.incrementAndGet();
                            copyAuxiliaryRetrievedItemToMemory(element);
                        }

                        found = true;

                        break;
                    }
                }
            }
        }
        catch (final IOException e)
        {
            log.error("Problem encountered getting element.", e);
        }

        if (!found)
        {
            missCountNotFound.incrementAndGet();

            log.debug("{0} - Miss", () -> cacheAttr.getCacheName());
        }

        if (element != null)
        {
            element.getElementAttributes().setLastAccessTimeNow();
        }

        return element;
    }