0

I have following spring cache config:

spring.cache.guava.spec: expireAfterWrite=1s

Then I have test for it:

@Test
public void test_not_work() {
  callCachedMethod(..);
  sleep(2s);
  callCachedMethod(..);

  expect("real method called TWO times"); 
  // because cache should be expired after 1s
  // It DOESN'T work, real method only called once
}

@Test
public void test_works() {
  callCachedMethod(..);
  sleep(2s);
  callCachedMethod(..);
  sleep(2s);
  callCachedMethod(..);

  expect("real method called THREE times"); 
  // because cache should be expired after 1s
  // IT WORKS!!
}

can someone explain it?

Jaiwo99
  • 9,687
  • 3
  • 36
  • 53
  • 1
    For one, are you sure Guava is actually being used? Instead of pasting a test we have no chance to run, please share a project that reproduces the problem. That'll be much easier to explain what is going on. – Stephane Nicoll Jul 01 '16 at 14:10
  • please explain the problem more, you don't write the problem – Safwan Hijazi Jul 04 '16 at 12:03

1 Answers1

1

It's because Guava does not ensure the eviction of the values automatically when the timeout value expires.

Per its documentation here:

Caches built with CacheBuilder do not perform cleanup and evict values "automatically," or instantly after a value expires, or anything of the sort. Instead, it performs small amounts of maintenance during write operations, or during occasional read operations if writes are rare.

Devs love ZenUML
  • 11,344
  • 8
  • 53
  • 67