oliverdavies.uk/content/node.1550e619-abd2-4efe-b941-fd4e42507d51.yml

154 lines
5.4 KiB
YAML

uuid:
- value: 1550e619-abd2-4efe-b941-fd4e42507d51
langcode:
- value: en
type:
- target_id: daily_email
target_type: node_type
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
revision_timestamp:
- value: '2025-05-11T08:59:58+00:00'
revision_uid:
- target_type: user
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
revision_log: { }
status:
- value: true
uid:
- target_type: user
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
title:
- value: 'Caching with decorators'
created:
- value: '2025-04-06T00:00:00+00:00'
changed:
- value: '2025-05-11T08:59:58+00:00'
promote:
- value: false
sticky:
- value: false
default_langcode:
- value: true
revision_translation_affected:
- value: true
path:
- alias: /daily/2025/04/06/caching
langcode: en
body:
- value: |
<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 {
/**
* The cache duration in seconds.
*/
private const CACHE_DURATION = 3600;
public function __construct(
private readonly ApiClientInterface $client,
private readonly TimeInterface $time,
private readonly CacheBackendInterface $cache,
) {
}
public function getResults(): Collection {
$key = $this-&gt;getCacheKey();
$cache = $this-&gt;cache-&gt;get($key);
if ($cache !== FALSE) {
return $cache-&gt;data;
}
$result = $this-&gt;client-&gt;getResults();
$this-&gt;cache-&gt;set(
cid: $key,
data: $result,
expire: $this-&gt;time-&gt;getRequestTime() + self::CACHE_DURATION,
);
return $result;
}
}
</code></pre>
<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>The possibilities are endless.</p>
format: full_html
processed: |
<p>As well as <a href="http://default/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="http://default/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 {
/**
* The cache duration in seconds.
*/
private const CACHE_DURATION = 3600;
public function __construct(
private readonly ApiClientInterface $client,
private readonly TimeInterface $time,
private readonly CacheBackendInterface $cache,
) {
}
public function getResults(): Collection {
$key = $this-&gt;getCacheKey();
$cache = $this-&gt;cache-&gt;get($key);
if ($cache !== FALSE) {
return $cache-&gt;data;
}
$result = $this-&gt;client-&gt;getResults();
$this-&gt;cache-&gt;set(
cid: $key,
data: $result,
expire: $this-&gt;time-&gt;getRequestTime() + self::CACHE_DURATION,
);
return $result;
}
}
</code></pre>
<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>The possibilities are endless.</p>
summary: null
field_daily_email_cta: { }