Java Code Examples for org.cache2k.Cache#expireAt()

The following examples show how to use org.cache2k.Cache#expireAt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExpiryTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * Leads to a special case using {@link org.cache2k.core.Entry#EXPIRED_REFRESH_PENDING}
 */
@Test
public void manualExpire_refreshAhead_sharp() {
  Cache<Integer, Integer> c = builder(Integer.class, Integer.class)
    .expireAfterWrite(LONG_DELTA, TimeUnit.MILLISECONDS)
    .refreshAhead(true)
    .keepDataAfterExpired(false)
    .loader(new CacheLoader<Integer, Integer>() {
      @Override
      public Integer load(final Integer key) throws Exception {
        return 4711;
      }
    })
    .build();
  c.put(1,2);
  c.expireAt(1, -millis());
  assertFalse(c.containsKey(1));
  await(new Condition() {
    @Override
    public boolean check() throws Exception {
      return getInfo().getRefreshCount() > 0;
    }
  });
}
 
Example 2
Source File: SlowExpiryTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void manualExpire_nowIsh_doesRefresh() {
  final Cache<Integer, Integer> c = builder(Integer.class, Integer.class)
    .expireAfterWrite(5, TimeUnit.MINUTES)
    .refreshAhead(true)
    .loader(new CacheLoader<Integer, Integer>() {
      @Override
      public Integer load(final Integer key) throws Exception {
        return 4711;
      }
    })
    .build();
  c.put(1,2);
  c.expireAt(1, millis() + TestingParameters.MINIMAL_TICK_MILLIS);
  await(new Condition() {
    @Override
    public boolean check() throws Exception {
      return c.get(1) == 4711;
    }
  });
}
 
Example 3
Source File: SlowExpiryTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * Refresh an entry immediately.
 */
@Test
public void manualExpire_nowIsh_getLater_doesRefresh() throws Exception {
  final Cache<Integer, Integer> c = builder(Integer.class, Integer.class)
    .expireAfterWrite(5, TimeUnit.MINUTES)
    .refreshAhead(true)
    .loader(new CacheLoader<Integer, Integer>() {
      @Override
      public Integer load(final Integer key) throws Exception {
        return 4711;
      }
    })
    .build();
  c.put(1,2);
  c.expireAt(1, millis() + TestingParameters.MINIMAL_TICK_MILLIS);
  sleep(TestingParameters.MINIMAL_TICK_MILLIS * 21);
  await(new Condition() {
    @Override
    public boolean check() throws Exception {
      return c.get(1) == 4711;
    }
  });
}
 
Example 4
Source File: SlowExpiryTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void manualExpire_nowIsh_doesTriggerRefresh() throws Exception {
  final Cache<Integer, Integer> c = builder(Integer.class, Integer.class)
    .expireAfterWrite(5, TimeUnit.MINUTES)
    .refreshAhead(true)
    .loader(new CacheLoader<Integer, Integer>() {
      @Override
      public Integer load(final Integer key) throws Exception {
        return 4711;
      }
    })
    .build();
  c.put(1,2);
  c.expireAt(1, millis() + TestingParameters.MINIMAL_TICK_MILLIS);
  await(new Condition() {
    @Override
    public boolean check() throws Exception {
      return getInfo().getRefreshCount() > 0;
    }
  });
}
 
Example 5
Source File: SlowExpiryTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void manualExpire_now_doesRefresh() {
  final Cache<Integer, Integer> c = builder(Integer.class, Integer.class)
    .expireAfterWrite(5, TimeUnit.MINUTES)
    .refreshAhead(true)
    .loader(new CacheLoader<Integer, Integer>() {
      @Override
      public Integer load(final Integer key) throws Exception {
        return 4711;
      }
    })
    .build();
  c.put(1,2);
  c.expireAt(1, 0);
  await(new Condition() {
    @Override
    public boolean check() throws Exception {
      return c.get(1) == 4711;
    }
  });
}
 
Example 6
Source File: ExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void manualExpire_exception() {
  Cache<Integer, Integer> c = builder(Integer.class, Integer.class)
    .eternal(true)
    .build();
  c.put(1,2);
  c.expireAt(1, FUTURE_TIME);
}
 
Example 7
Source File: ExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void manualExpire_now() {
  Cache<Integer, Integer> c = builder(Integer.class, Integer.class)
    .eternal(true)
    .build();
  c.put(1,2);
  c.expireAt(1, 0);
  assertFalse(c.containsKey(1));
}
 
Example 8
Source File: ExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void manualExpire_aboutNow() {
  Cache<Integer, Integer> c = cache = builder(Integer.class, Integer.class)
    .expireAfterWrite(LONG_DELTA, TimeUnit.MILLISECONDS)
    .build();
  c.put(1,2);
  assertTrue(c.containsKey(1));
  c.expireAt(1, millis());
  assertFalse(c.containsKey(1));
  statistics()
    .putCount.expect(1)
    .expiredCount.expect(1)
    .expectAllZero();
}
 
Example 9
Source File: ExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void manualExpire_sharp() {
  Cache<Integer, Integer> c = builder(Integer.class, Integer.class)
    .expireAfterWrite(LONG_DELTA, TimeUnit.MILLISECONDS)
    .loader(new CacheLoader<Integer, Integer>() {
      @Override
      public Integer load(final Integer key) throws Exception {
        return 4711;
      }
    })
    .build();
  c.put(1,2);
  c.expireAt(1, -millis());
  assertFalse(c.containsKey(1));
}
 
Example 10
Source File: ExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void manualExpire_refreshAhead_sharp_expireAt0_gone() {
  final Cache<Integer, Integer> c = builder(Integer.class, Integer.class)
    .expireAfterWrite(LONG_DELTA, TimeUnit.MILLISECONDS)
    .refreshAhead(true)
    .keepDataAfterExpired(false)
    .loader(new CacheLoader<Integer, Integer>() {
      @Override
      public Integer load(final Integer key) throws Exception {
        return 4711;
      }
    })
    .build();
  within(LONG_DELTA).work(new Runnable() {
    @Override
    public void run() {
      c.put(1,2);
      c.expireAt(1, -millis());
    }
  }).check(new Runnable() {
    @Override
    public void run() {
      assertFalse(c.containsKey(1));
      await(new Condition() {
        @Override
        public boolean check() throws Exception {
          return getInfo().getRefreshCount() > 0;
        }
      });
      assertEquals(1, getInfo().getSize());
    }
  });
  c.expireAt(1, ExpiryTimeValues.NO_CACHE);
  assertEquals(0, getInfo().getSize());
}
 
Example 11
Source File: SlowExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void manualExpire_nowIsh() {
  final Cache<Integer, Integer> c = builder(Integer.class, Integer.class)
    .expireAfterWrite(5, TimeUnit.MINUTES)
    .build();
  c.put(1,2);
  c.expireAt(1, millis() + TestingParameters.MINIMAL_TICK_MILLIS);
  await(new Condition() {
    @Override
    public boolean check() throws Exception {
      return !c.containsKey(1);
    }
  });
}