Skip to content

Commit 4c80562

Browse files
committed
Add a cacheKeyPrefix option to BrandingClient and OrbitClient
This allows the consuming application to customise the cache key that is stored.
1 parent 34402ee commit 4c80562

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ Valid $options keys to pass to the `BrandingClient` are:
9191

9292
* `env`: To set an environment to request Branding from. Must be one of
9393
'live', 'test' or 'int'. If omitted, shall default to 'live'.
94+
* `cacheKeyPrefix`: A prefix to use for the cache key. If omitted, shall default
95+
to 'branding'.
9496
* `cacheTime`: By default the Client uses the cache control headers of the API
9597
response determine how long to cache for. To override this value set the
9698
`cacheTime` to a value in seconds.
@@ -99,6 +101,8 @@ Valid $options keys to pass to the `OrbitClient` are:
99101

100102
* `env`: To set an environment to request Orbit from. Must be one of
101103
'live', 'test' or 'int'. If omitted, shall default to 'live'.
104+
* `cacheKeyPrefix`: A prefix to use for the cache key. If omitted, shall default
105+
to 'orbit'.
102106
* `cacheTime`: By default the Client uses the cache control headers of the API
103107
response determine how long to cache for. To override this value set the
104108
`cacheTime` to a value in seconds.

src/BrandingClient.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class BrandingClient
4343
*/
4444
private $options = [
4545
'env' => 'live',
46+
'cacheKeyPrefix' => 'branding',
4647
'cacheTime' => null,
4748
];
4849

@@ -75,7 +76,7 @@ public function __construct(
7576
public function getContent($projectId, $themeVersionId = null)
7677
{
7778
$url = $this->getUrl($projectId, $themeVersionId);
78-
$cacheKey = 'BBC_BRANDING_' . md5($url);
79+
$cacheKey = $this->options['cacheKeyPrefix'] . '.' . md5($url);
7980

8081
/** @var CacheItemInterface $cacheItem */
8182
$cacheItem = $this->cache->getItem($cacheKey);

src/OrbitClient.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class OrbitClient
3434
*/
3535
private $options = [
3636
'env' => 'live',
37+
'cacheKeyPrefix' => 'orbit',
3738
'cacheTime' => null,
3839
'mustache' => [],
3940
];
@@ -77,7 +78,7 @@ public function getContent(array $requestParams = [], array $templateParams = []
7778
{
7879
$url = $this->getUrl();
7980
$headers = $this->getRequestHeaders($requestParams);
80-
$cacheKey = 'BBC_BRANDING_ORBIT_' . md5($url . json_encode($requestParams));
81+
$cacheKey = $this->options['cacheKeyPrefix'] . '.' . md5($url . json_encode($requestParams));
8182

8283
/** @var CacheItemInterface $cacheItem */
8384
$cacheItem = $this->cache->getItem($cacheKey);

tests/BrandingClientTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function testConstructor()
1919
{
2020
$expectedDefaultOptions = [
2121
'env' => 'live',
22+
'cacheKeyPrefix' => 'branding',
2223
'cacheTime' => null,
2324
];
2425

@@ -34,6 +35,7 @@ public function testConstructorCustomOptions()
3435
{
3536
$options = [
3637
'env' => 'test',
38+
'cacheKeyPrefix' => 'branding.123',
3739
'cacheTime' => 10,
3840
];
3941

@@ -155,6 +157,8 @@ public function testMalformedContentThrowsException()
155157
*/
156158
public function testCachingTimes($options, $headers, $expectedCacheDuration)
157159
{
160+
$expectedKey = 'branding.b22b2e21ce267c3879b21fd96939bfd3';
161+
158162
$client = $this->getClient([$this->mockSuccessfulJsonResponse($headers)]);
159163
$cache = $this->getMockBuilder('Symfony\Component\Cache\Adapter\NullAdapter')
160164
->disableOriginalClone()
@@ -164,8 +168,9 @@ public function testCachingTimes($options, $headers, $expectedCacheDuration)
164168
->getMock();
165169

166170
$cache->expects($this->once())->method('save')->with($this->callback(
167-
function ($cacheItemToSave) use ($expectedCacheDuration) {
171+
function ($cacheItemToSave) use ($expectedKey, $expectedCacheDuration) {
168172
$current = time() + $expectedCacheDuration;
173+
$this->assertEquals($expectedKey, $cacheItemToSave->getKey());
169174
$this->assertAttributeEquals($current, 'expiry', $cacheItemToSave);
170175
return true;
171176
}

tests/OrbitClientTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function testConstructor()
1919
{
2020
$expectedDefaultOptions = [
2121
'env' => 'live',
22+
'cacheKeyPrefix' => 'orbit',
2223
'cacheTime' => null,
2324
'mustache' => [],
2425
];
@@ -35,6 +36,7 @@ public function testConstructorCustomOptions()
3536
{
3637
$options = [
3738
'env' => 'test',
39+
'cacheKeyPrefix' => 'orbit.123',
3840
'cacheTime' => 10,
3941
'mustache' => ['someconfig'],
4042
];
@@ -174,6 +176,8 @@ public function testMalformedContentThrowsException()
174176
*/
175177
public function testCachingTimes($options, $headers, $expectedCacheDuration)
176178
{
179+
$expectedKey = 'orbit.5617e91c21636eb642dbeabcfb06342c';
180+
177181
$client = $this->getClient([$this->mockSuccessfulJsonResponse($headers)]);
178182

179183
$cache = $this->getMockBuilder('Symfony\Component\Cache\Adapter\NullAdapter')
@@ -184,8 +188,9 @@ public function testCachingTimes($options, $headers, $expectedCacheDuration)
184188
->getMock();
185189

186190
$cache->expects($this->once())->method('save')->with($this->callback(
187-
function ($cacheItemToSave) use ($expectedCacheDuration) {
191+
function ($cacheItemToSave) use ($expectedKey, $expectedCacheDuration) {
188192
$current = time() + $expectedCacheDuration;
193+
$this->assertEquals($expectedKey, $cacheItemToSave->getKey());
189194
$this->assertAttributeEquals($current, 'expiry', $cacheItemToSave);
190195
return true;
191196
}

0 commit comments

Comments
 (0)