@Test public void testCacheTime() { Cache<String, String> cache = new LFUCache<>(3); cache.put("1", "1", 50); assertNotNull(cache.get("1")); assertNotNull(cache.get("1")); // boost usage cache.put("2", "2"); cache.get("2"); assertFalse(cache.isFull()); cache.put("3", "3"); assertTrue(cache.isFull()); ThreadUtil.sleep(100); assertNull(cache.get("1")); // expired assertFalse(cache.isFull()); cache.put("1", "1", 50); assertNotNull(cache.get("1")); assertNotNull(cache.get("1")); ThreadUtil.sleep(100); assertTrue(cache.isFull()); cache.put("4", "4"); assertNotNull(cache.get("3")); assertNotNull(cache.get("2")); assertNotNull(cache.get("4")); assertNull(cache.get("1")); }
@Test public void testCache2() { Cache<String, String> cache = new LRUCache<String, String>(3); cache.put("1", "1"); cache.put("2", "2"); assertFalse(cache.isFull()); cache.put("3", "3"); assertTrue(cache.isFull()); assertNotNull(cache.get("3")); assertNotNull(cache.get("3")); assertNotNull( cache.get( "3")); // boost usage of a 3, but this doesn't change a thing, since this is a LRU cache // and not a LFU cache assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); cache.put("4", "4"); assertNull(cache.get("3")); assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); assertNotNull(cache.get("4")); cache.put("3", "3"); assertNull(cache.get("1")); }
@Test public void testEndless() { Cache<String, String> cache = new LRUCache<String, String>(0); assertFalse(cache.isFull()); cache.put("1", "1"); assertEquals(1, cache.size()); assertFalse(cache.isFull()); cache.put("2", "2"); assertEquals(2, cache.size()); assertFalse(cache.isFull()); }
@Test public void testCache() { Cache<String, String> cache = new LRUCache<String, String>(3); cache.put("1", "1"); cache.put("2", "2"); assertFalse(cache.isFull()); cache.put("3", "3"); assertTrue(cache.isFull()); assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); cache.put("4", "4"); assertNull(cache.get("3")); assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); cache.put("3", "3"); assertNull(cache.get("4")); }
@Test public void testCache2() { Cache<String, String> cache = new LFUCache<>(3); cache.put("1", "1"); cache.put("2", "2"); assertFalse(cache.isFull()); cache.put("3", "3"); assertTrue(cache.isFull()); assertNotNull(cache.get("3")); assertNotNull(cache.get("3")); assertNotNull(cache.get("3")); // boost usage of a 3 assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); cache.put("4", "4"); // since this is LFU cache, 1 AND 2 will be removed, but not 3 assertNotNull(cache.get("3")); assertNotNull(cache.get("4")); assertEquals(2, cache.size()); }
@Test public void testCache() { Cache<String, String> cache = new LFUCache<>(3); cache.put("1", "1"); cache.put("2", "2"); assertFalse(cache.isFull()); cache.put("3", "3"); assertTrue(cache.isFull()); assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); cache.put("4", "4"); // new element, cache is full, prune is invoked assertNull(cache.get("3")); assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); cache.put("3", "3"); assertNull(cache.get("4")); assertNotNull(cache.get("3")); }