package com.softcorporation.util;

/**
 * test
 */
public class ExpiringCacheTest {
 
    public static void main(String[] args) throws InterruptedException {
        testAddRemoveObjects();
        testExpiredCacheObjects();
        testObjectsCleanupTime();
    }
 
    static void testAddRemoveObjects() {
 
        MyExpiringCache cache = new MyExpiringCache(5, 200, 500);
 
//        cache.put("1", "eBay");
//        cache.put("2", "Paypal");
//        cache.put("3", "Google");
//        cache.put("4", "Microsoft");
//        cache.put("5", "IBM");
//        cache.put("6", "Facebook");
 
        System.out.println("6 Cache Object Added.. cache.size(): " + cache.size());
        cache.remove("5");
        System.out.println("One object removed.. cache.size(): " + cache.size());
 
        cache.put("7", "Twitter");
        cache.put("8", "SAP");
        System.out.println("Two objects Added but reached maxItems.. cache.size(): " + cache.size());
        System.out.println("Map objects: " + cache.toString());
    }
 
    static void testExpiredCacheObjects() throws InterruptedException {
 
        MyExpiringCache cache = new MyExpiringCache(10, 1, 2);
 
        cache.put("eBay", "eBay");
        cache.put("Paypal", "Paypal");
        // Adding 10 seconds sleep.. Both above objects will be removed from
        // Cache because of timeToLiveInSeconds value
        Thread.sleep(2000);
 
        System.out.println("Two objects are added but reached timeToLive. cache.size(): " + cache.size());
 
    }
 
    static void testObjectsCleanupTime() throws InterruptedException {
        int size = 500000;
 
        MyExpiringCache cache = new MyExpiringCache(size, 0, 0);
 
        for (int i = 0; i < size * 2; i++) {
            String value = Integer.toString(i);
            cache.put(value, value);
        }
 
        Thread.sleep(200);
 
        long start = System.currentTimeMillis();
        cache.cleanup();
        long took = System.currentTimeMillis() - start;
 
        System.out.println("Cleanup time for " + size + " objects " + took + " ms");
 
    }
}
