title: Caching functions in Java?
body: <p>I have Spring/Java project where I am thinking about caching some of the functions that access databases and other rest services. I am not sure at which point should I cache a certain flow. For example, if I have the following functions:</p>
  
  <pre><code>public List&lt;String&gt; getSchools()
  {
       //call db to get names
  }
  
  public List&lt;String&gt; getCourses(String school)
  {
      //rest call to get courses for a school
  }
  
  public List&lt;String&gt; getTeachers(String course)
  {
      //call db to get teacher names for a course
  }
  
  /*Uses above three functions together*/
  public List&lt;String&gt; getAllTeachers()
  {
        List&lt;String&gt; schools = getSchools();
        List&lt;String&gt; courses = new ArrayList&lt;String&gt;();
        List&lt;String&gt; teachers = new ArrayList&lt;String&gt;();
  
        for(String s : schools)
            courses.addAll( getCourses( s ) );
        for(String c : courses)
           teachers.addAll( getTeachers( s ) );
  
       return teachers;
  }
  </code></pre>
  
  <p>Here, which methods should I cache? Should I cache the function that call the other 3 resource-heavy functions or should I cache the three functions individually ? What would be generally considered good-practice ? </p>