title: How to store objects in memory and flush them to a destination after Max(amount, time)
body: <p>My <code>Spring Boot</code> application gets input <code>Student</code> and needs to write it to the DB.</p>
  
  <p>I can write to the DB right after getting the <code>Student</code> object, but I like to make it more efficient, by accumulating several students in memory and then to save to the DB a bulk of <code>Students</code>.</p>
  
  <p>So I'm looking for a solution that will enable to cache the Students for <code>Max(some duration, number of students)</code> and will enable to save the Students in one bulk once the limit was reached.</p>
  
  <p>There is an option to write the code with <code>Thread</code> and such, but it seems like this problem was probably solved before.</p>
  
  <p>There are the <a href="https://www.baeldung.com/java-caching-caffeine" rel="nofollow noreferrer">caffeine</a> and <a href="https://github.com/google/guava" rel="nofollow noreferrer">guava</a> libraries, but I still didn't find what I'm looking for.</p>