title: How Spring Caching works?
body: <p>How is the @Cacheable annotation notified about the changes in the database. For eg : If I Cache the result of CRUDRepository.findAll() function call by annotating the function call by @Cacheable the result is stored in a named cache. The Cache however continues to be updated with DB changes. Consider the below example :</p>
  
  <pre><code>@Cacheable(value = "employee", key = "#id")
  public Iterable&lt;Employee&gt; getAllEmployees(){                 
      long startTime = System.currentTimeMillis();
      Iterable&lt;Employee&gt;  itr = employeeEntityRepository.findAll();
      long stopTime = System.currentTimeMillis();
      System.out.println("Time--&gt;" + (stopTime - startTime));
      return itr;
  }
  </code></pre>
  
  <p>When this method is called the first time it takes 300 ms and the 2nd time it takes hardly 5 ms which means it Caches the result set. Fair enough. But now if I update a record in the employee table directly in the DB and call this method again two things happen :</p>
  
  <ol>
  <li>The new record is returned by this method call.</li>
  <li>The method returns very fast.</li>
  </ol>
  
  <p>This means that the cache is updated from the DB every time. How does this Sync work if the resultset is returned from Cache ?</p>
  
  <p>thanks.</p>