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

com.dexcoder.commons.cache.FIFOCache Maven / Gradle / Ivy

package com.dexcoder.commons.cache;

import java.util.Iterator;
import java.util.LinkedHashMap;

/**
 * FIFO缓存实现
 *
 * Created by liyd on 9/25/14.
 */
public class FIFOCache extends AbstractCacheMap {

    /**
     * 构造访求
     * 
     * @param cacheSize
     * @param defaultExpire
     */
    public FIFOCache(int cacheSize, long defaultExpire) {
        super(cacheSize, defaultExpire);
        cacheMap = new LinkedHashMap>(cacheSize + 1, 1F, false);
    }

    @Override
    protected int eliminateCache() {

        int count = 0;
        K firstKey = null;

        Iterator> iterator = cacheMap.values().iterator();
        while (iterator.hasNext()) {
            CacheObject cacheObject = iterator.next();

            if (cacheObject.isExpired()) {
                iterator.remove();
                count++;
            } else {
                if (firstKey == null) {
                    firstKey = cacheObject.key;
                }
            }
        }

        //删除过期对象还是满,继续删除链表第一个
        if (firstKey != null && isFull()) {
            cacheMap.remove(firstKey);
        }

        return count;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy