<p>In <code>DBIx::Class::ResultSet::related_resultset</code>, if the current resultset has a cache of results, the rows in that cache are checked for related objects which can be used to populate the cache belonging to the related resultset which is going to be returned.</p>
<p>However, the related resultset objects themselves are also stored on <code>$self</code> in a cache (<code>$self-&gt;{'related_resultsets'}</code>), so that this process is skipped if the related resultset has already been created in a previous call to <code>related_resultset</code>.</p>
<p>This means that if the cache is set after a related resultset has been created, subsequent calls to <code>related_resultset</code> retrieve the existing resultset; because it was created before the cache existed, it either has no cache of its own, or has a cache which potentially contradicts the cache in set_cache.</p>
<p>There is no user-facing way to 'undo' the creation of a cached related resultset, nor is it obvious from the docs that this might be useful or necessary (not to mention why it should be). In fact, the docs don't even mention that related resultsets are cached.</p>
<p>For example: in this case <code>$artist</code> is an arrayref of one item:</p>
<pre><code>$cd_rset-&gt;set_cache(\@cds);
my $artist_rset_after = $cd_rset-&gt;related_resultset('artist');
my $artist = $artist_rset_after-&gt;get_cache();
</code></pre>
<p>... whereas in this case <code>$artist</code> is <code>undef</code>:</p>
<pre><code>my $artist_rset_before = $cd_rset-&gt;related_resultset('artist');
$cd_rset-&gt;set_cache(\@cds);
my $artist_rset_after = $cd_rset-&gt;related_resultset('artist');
my $artist = $artist_rset_after-&gt;get_cache();
</code></pre>
<p>I see two potential solutions:</p>
<ol>
<li>
<p>When <code>$self-&gt;set_cache</code> is called, clear <code>$self-&gt;{'related_resultsets'}</code>. New calls to <code>related_resultset</code> would return new resultsets with the correct cache. Resultsets previously created would be unchanged.</p>
</li>
<li>
<p>When <code>$self-&gt;set_cache</code> is called, populate the cache for each resultset in <code>$self-&gt;{'related_resultsets'}</code>. This may be surprising if those resultsets have also previously populated a cache. Furthermore, what ought to happen if <code>\@cds</code> do not all have a populated cache in their <code>related_resultset</code> for a given relationship. Should the cache on the existing resultset be emptied, or should it be left alone?</p>
</li>
</ol>
<p>I feel that option 1 is the cleanest solution.</p>
<p>This PR introduces:</p>
<ul>
<li>A currently-failing test which demonstrates the issue</li>
<li>A one-line-plus-comments fix, following the first solution.</li>
</ul>

<hr>

<h4>You can view, comment on, or merge this pull request online at:</h4>
<p>&nbsp;&nbsp;<a href='https://github.com/dbsrgits/dbix-class/pull/110'>https://github.com/dbsrgits/dbix-class/pull/110</a></p>

<h4>Commit Summary</h4>
<ul>
  <li>Test how related_resultset and set_cache interact</li>
  <li>Clear related_resultsets after set_cache</li>
</ul>

<h4>File Changes</h4>
<ul>
  <li>
    <strong>M</strong>
    <a href="https://github.com/dbsrgits/dbix-class/pull/110/files#diff-0">lib/DBIx/Class/ResultSet.pm</a>
    (8)
  </li>
  <li>
    <strong>A</strong>
    <a href="https://github.com/dbsrgits/dbix-class/pull/110/files#diff-1">t/resultset/cache.t</a>
    (56)
  </li>
</ul>

<h4>Patch Links:</h4>
<ul>
  <li><a href='https://github.com/dbsrgits/dbix-class/pull/110.patch'>https://github.com/dbsrgits/dbix-class/pull/110.patch</a></li>
  <li><a href='https://github.com/dbsrgits/dbix-class/pull/110.diff'>https://github.com/dbsrgits/dbix-class/pull/110.diff</a></li>
</ul>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">&mdash;<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/dbsrgits/dbix-class/pull/110">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AASeAhvgPEmHV9L0U6Z-t6oDwkcbrVEEks5q8c3JgaJpZM4KtizR">mute the thread</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/AASeAsxfP6jN76LhZ8nBy4a3vzD4ZTOhks5q8c3JgaJpZM4KtizR.gif" width="1" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
  <link itemprop="url" href="https://github.com/dbsrgits/dbix-class/pull/110"></link>
  <meta itemprop="name" content="View Pull Request"></meta>
</div>
<meta itemprop="description" content="View this Pull Request on GitHub"></meta>
</div>

<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/dbsrgits/dbix-class","title":"dbsrgits/dbix-class","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/dbsrgits/dbix-class"}},"updates":{"snippets":[{"icon":"DESCRIPTION","message":"set_cache should invalidate existing related_resultset (#110)"}],"action":{"name":"View Pull Request","url":"https://github.com/dbsrgits/dbix-class/pull/110"}}}</script>