title: Spring Cache - Generate custom cached result
body: <p>I'm using Spring Boot and EhCache to develop a calendar application.</p>
  
  <p>Some users can request concerts for a specific week, others - for entire month. All responses are being cached with time to live 300 seconds to decrease the response time.</p>
  
  <p>Input parameters for cached methods are <code>startDate</code>, <code>endDate</code> and a <code>username</code>.</p>
  
  <p>The problem is when user requests concerts for the entire month, and then for a singe week within the same month, the method will be executed two times, even though the "week" response can be calculated based on the "month" response, since the week might be a subset of a cached month.</p>
  
  <p>Examples:</p>
  
  <pre><code>1. 
  INPUT
  startDate:08.06.2016, endDate:07.07.2016, username:exampleUser
  //the method is executed, response cached
  OUTPUT
  concert "Example Concert" on 13.06.2016
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>INPUT
  startDate:08.06.2016, endDate:15.06.2016, username:exampleUser
  //the method is executed, response cached
  OUTPUT
  concert "Example Concert" on 13.06.2016
  </code></pre>
  
  <p>As you can see in the example, the method will be executed two times, but in fact it is not necessary to execute the method in second example, the response can be extracted from cached data returned in the first request.</p>
  
  <p>I was thinking to create a custom "generator" for <code>concerts</code> cache which will be able to loop through all cached data and find date ranges that "contain" requested <code>startDate</code> and <code>endDate</code></p>
  
  <p>Here is what I have so far:</p>
  
  <p><strong>Cached methods:</strong></p>
  
  <pre><code>@Override
      @CachePut("concerts")
      public List&lt;Event&gt; refreshEventsCache(String eventsForUser, Date startDate, Date endDate) throws Exception {
  
          return fetchEventsFromTheServer(eventsForUser, startDate, endDate);
      }
  
  
      @Override
      @Cacheable(value = "concerts")
      public List&lt;Event&gt; getEvents(String eventsForUser, Date startDate, Date endDate) throws Exception {
  
          return fetchEventsFromTheServer(eventsForUser, startDate, endDate);
      }
  </code></pre>
  
  <p><strong>ehcache.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ehcache&gt;
      &lt;defaultCache maxElementsInMemory="500" eternal="false"
                    overflowToDisk="false" memoryStoreEvictionPolicy="LFU" /&gt;
  
      &lt;diskStore path="java.io.tempdir"/&gt;
  
      &lt;cache name="concerts"
             maxElementsInMemory="5000"
             eternal="false"
             timeToIdleSeconds="0"
             timeToLiveSeconds="300"
             overflowToDisk="false"
             memoryStoreEvictionPolicy="LFU" /&gt;
  &lt;/ehcache&gt;
  </code></pre>
  
  <p><strong>Application.java:</strong></p>
  
  <pre><code>...
  @SpringBootApplication
  @ComponentScan
  @EnableCaching
  @EnableScheduling
  public class SpringBootWebApplication extends SpringBootServletInitializer {
  
      public static void main(String[] args) {
          SpringApplication.run(SpringBootWebApplication.class, args);
      }
  
      @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
          return application.sources(applicationClass);
      }
  
      private static Class&lt;SpringBootWebApplication&gt; applicationClass = SpringBootWebApplication.class;
  
  
  
  }
  </code></pre>
  
  <p>How can I build "smart" cache generator which will return subsets of cached data?</p>