title: What happens if I cache the same RDD twice in Spark
body: <p>I'm building a generic function which receives a RDD and does some calculations on it. Since I run more than one calculation on the input RDD I would like to cache it. For example:</p>
  
  <pre><code>public JavaRDD&lt;String&gt; foo(JavaRDD&lt;String&gt; r) {
      r.cache();
      JavaRDD t1 = r... //Some calculations
      JavaRDD t2 = r... //Other calculations
      return t1.union(t2);
  }
  </code></pre>
  
  <p>My question is, since <code>r</code> is given to me it may or may not already be cached. If it is cached and I call cache on it again, will spark create a new layer of cache meaning that while <code>t1</code> and <code>t2</code> are calculated I will have two instances of <code>r</code> in the cache? or will spark is aware of the fact that <code>r</code> is cached and will ignore it?</p>