<p>As well as <a href="/daily/2025/04/05/strategies">working with different versions of an API</a>, I was able to use the same technique I wrote about yesterday to easily add a cacheable version of the API client.</p>
<p>As they all implement the same <code>ApiClientInterface</code>, I can inject and decorate a client with another client, making one solely responsible for caching the result from the API whilst keeping the API interaction logic separate (aka <a href="/daily/2022/12/08/the-decorator-design-pattern">the Decorator design pattern</a>).</p>
<p>Here's an example based on the code I wrote:</p>
<pre><code class="php">final class CacheableApiClient implements ApiClientInterface {
<p>Nothing in this instance is specific to either version of the API.</p>
<p>This client is only concerned with retrieving and saving cache data, and delegating any other logic to the original version.</p>
<p>With this approach, I can switch between <code>V1ApiClient</code>, <code>V2ApiClient</code> or any other version with the same methods without having to reimplement caching as that's handled within the <code>CacheableApiClient</code>.</p>
<p>But what if I don't want to interact with the API at all?</p>
<p>For local development, I have a <code>FakeApiClient</code> that returns a static response that I can work with.</p>
<p>As well as <a href="/daily/2025/04/05/strategies">working with different versions of an API</a>, I was able to use the same technique I wrote about yesterday to easily add a cacheable version of the API client.</p>
<p>As they all implement the same <code>ApiClientInterface</code>, I can inject and decorate a client with another client, making one solely responsible for caching the result from the API whilst keeping the API interaction logic separate (aka <a href="/daily/2022/12/08/the-decorator-design-pattern">the Decorator design pattern</a>).</p>
<p>Nothing in this instance is specific to either version of the API.</p>
<p>This client is only concerned with retrieving and saving cache data, and delegating any other logic to the original version.</p>
<p>With this approach, I can switch between <code>V1ApiClient</code>, <code>V2ApiClient</code> or any other version with the same methods without having to reimplement caching as that's handled within the <code>CacheableApiClient</code>.</p>
<p>But what if I don't want to interact with the API at all?</p>
<p>For local development, I have a <code>FakeApiClient</code> that returns a static response that I can work with.</p>