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

com.alicp.jetcache.embedded.Cleaner Maven / Gradle / Ivy

The newest version!
package com.alicp.jetcache.embedded;

import com.alicp.jetcache.support.JetCacheExecutor;

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created on 2017/2/28.
 *
 * @author huangli
 */
class Cleaner {

    static ConcurrentLinkedQueue> linkedHashMapCaches = new ConcurrentLinkedQueue<>();

    static {
        ScheduledExecutorService executorService = JetCacheExecutor.defaultExecutor();
        executorService.scheduleWithFixedDelay(() -> run(), 60, 60, TimeUnit.SECONDS);
    }

    static void add(LinkedHashMapCache cache) {
        linkedHashMapCaches.add(new WeakReference<>(cache));
    }

    static void run() {
        Iterator> it = linkedHashMapCaches.iterator();
        while (it.hasNext()) {
            WeakReference ref = it.next();
            LinkedHashMapCache c = ref.get();
            if (c == null) {
                it.remove();
            } else {
                c.cleanExpiredEntry();
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy