title: Guava cache as time-based cleanup storage in Spring ap
body: <p>I have a Spring Boot/MVC app that should store some Simple POJOs sent from users for 15 minutes of time. When this time expires, this object should be removed from <code>ConcurrentHashMap</code>. At first <code>ConcurrentHashMap</code> was something I wanted to implement this feature with, but then I thought maybe leveraging Guava's cache would be a better option, since it has a time-based eviction out of the box.</p>
  
  <p>My service implementation </p>
  
  <pre><code>@CachePut(cacheNames = "teamConfigs", key = "#authAccessDto.accessToken")
  @Override
  public OAuthAccessDto saveConfig(OAuthAccessDto authAccessDto) {
      return authAccessDto;
  }
  
  @Override
  @Cacheable(cacheNames = "teamConfigs")
  public OAuthAccessDto getConfig(String accessToken) {
      // we assume that the cache is already populated
      return null;
  }
  </code></pre>
  
  <p>As you can see, we save data with <code>saveConfig</code> and then when we need to retrieve it, we call <code>getConfig</code>. </p>
  
  <p>Cache configuration in Spring boot is the following (yml file):</p>
  
  <pre><code>spring:
    cache:
      cache-names: teamConfigs
      guava:
        spec: expireAfterWrites=900s
  </code></pre>
  
  <p><strong>However</strong>, after reading Guava's cache doc <a href="https://github.com/google/guava/wiki/CachesExplained" rel="nofollow">https://github.com/google/guava/wiki/CachesExplained</a> I found that Guava can clean up caches even <em>before</em> the time defined in <code>expireAfterWrites</code> elapses (<strong>and even before it runs out of memory</strong>). </p>
  
  <p>How can I configure Guava Cache to keep objects until the time expires (<em>considering it did not run out of memory</em>). Maybe I should opt for some other solution?</p>