All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.ehcache.pool.impl.FromLargestCachePoolEvictor Maven / Gradle / Ivy

Go to download

Ehcache is an open source, standards-based cache used to boost performance, offload the database and simplify scalability. Ehcache is robust, proven and full-featured and this has made it the most widely-used Java-based cache.

There is a newer version: 2.10.9.2
Show newest version
/**
 *  Copyright Terracotta, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package net.sf.ehcache.pool.impl;

import net.sf.ehcache.pool.PoolAccessor;
import net.sf.ehcache.pool.PoolEvictor;
import net.sf.ehcache.pool.PoolParticipant;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Abstract pool evictor which always evicts from the store consuming the most resources.
 *
 * @author Ludovic Orban
 * @author Alex Snaps
 */
public class FromLargestCachePoolEvictor implements PoolEvictor {

    /**
     * {@inheritDoc}
     */
    public boolean freeSpace(Collection> from, long bytes) {
        if (from == null || from.isEmpty()) {
            return false;
        }

        long remainingSizeInBytes = bytes;
        Collection> tried = new ArrayList>();

        while (tried.size() != from.size()) {
            PoolAccessor largestPoolAccessor = findUntriedLargestPoolableStore(from, tried);

            long beforeEvictionSize = largestPoolAccessor.getSize();
            if (!largestPoolAccessor.getParticipant().evict(1, bytes)) {
                tried.add(largestPoolAccessor);
                continue;
            }
            long afterEvictionSize = largestPoolAccessor.getSize();

            remainingSizeInBytes -= (beforeEvictionSize - afterEvictionSize);
            if (remainingSizeInBytes <= 0L) {
                return true;
            }
        }

        return false;
    }

    private PoolAccessor findUntriedLargestPoolableStore(Collection> from,
                                                                          Collection> tried) {
        PoolAccessor largestPoolAccessor = null;
        for (PoolAccessor accessor : from) {
            if (alreadyTried(tried, accessor)) {
                continue;
            }

            if (largestPoolAccessor == null || accessor.getSize() > largestPoolAccessor.getSize()) {
                largestPoolAccessor = accessor;
            }
        }
        return largestPoolAccessor;
    }

    private boolean alreadyTried(Collection> tried, PoolAccessor from) {
        for (PoolAccessor accessor : tried) {
            if (accessor == from) {
                return true;
            }
        }
        return false;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy