示例#1
0
  @Test
  public void testCacheTime() {
    Cache<String, String> cache = new LRUCache<String, String>(3);
    cache.put("3", "3");
    cache.put("2", "2");
    assertNotNull(cache.get("2"));
    cache.put("1", "1", 50);
    assertNotNull(cache.get("1"));
    assertTrue(cache.isFull());

    ThreadUtil.sleep(100);
    assertNull(cache.get("1")); // expired
    assertFalse(cache.isFull());
  }
  @Test
  public void spr14853AdaptsToOptionalWithSync() {
    AnnotationConfigApplicationContext context =
        new AnnotationConfigApplicationContext(Spr14853Config.class);
    Spr14853Service bean = context.getBean(Spr14853Service.class);
    Cache cache = context.getBean(CacheManager.class).getCache("itemCache");

    TestBean tb = new TestBean("tb1");
    bean.insertItem(tb);
    assertSame(tb, bean.findById("tb1").get());
    assertSame(tb, cache.get("tb1").get());

    cache.clear();
    TestBean tb2 = bean.findById("tb1").get();
    assertNotSame(tb, tb2);
    assertSame(tb2, cache.get("tb1").get());
  }
示例#3
0
  @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"));
  }
示例#4
0
  @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"));
  }
示例#5
0
  @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());
  }
示例#6
0
  @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"));
  }
示例#7
0
  @Test
  public void testBoosting() {
    Cache<String, String> cache = new LFUCache<>(3);
    cache.put("1", "1");
    cache.put("2", "2");
    cache.put("3", "3");

    cache.get("3");
    cache.get("3");
    cache.get("3");
    cache.get("3");
    cache.get("2");
    cache.get("2");
    cache.get("2");
    cache.get("1");
    cache.get("1");

    assertEquals(3, cache.size());

    cache.put("4", "4");

    assertNull(cache.get("1")); // 1 is less frequent and it is out of cache
    assertNotNull(cache.get("4")); // 4 is new and it is inside

    cache.get("3");
    cache.get("2");

    // bad sequence
    cache.put("5", "5");
    cache.get("5"); // situation: 2(1), 3(2), 5(1)   value(accessCount)
    cache.put("4", "4");
    cache.get("4"); // situation: 3(1), 4(1)
    cache.put("5", "5");
    cache.get("5"); // situation: 3(1), 4(1), 5(1)
    cache.put("4", "4");
    cache.get("4"); // situation: 4(1)

    assertEquals(3, cache.size());

    assertNull(cache.get("1"));
    assertNull(cache.get("2"));
    assertNotNull(cache.get("3"));
    assertNotNull(cache.get("4"));
    assertNotNull(cache.get("5"));
  }