Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-6197: Trait to generate cache keys #71

Merged
merged 3 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/Cache/CacheKeyTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types = 1);

namespace Drupal\helfi_api_base\Cache;

/**
* A helper trait to generate cache keys.
*/
trait CacheKeyTrait {

/**
* Converts an array of request options to cache key.
*
* @param string $baseKey
* The base key.
* @param array $options
* The options.
*
* @return string
* The flattened cache key.
*/
protected function requestOptionsToCacheKey(string $baseKey, array $options) : string {
foreach ($options as $key => $value) {
// We only care about string keys.
if (is_string($key)) {
$baseKey .= sprintf('%s=', $key);
}
if (is_array($value)) {
$baseKey .= $this->requestOptionsToCacheKey('', $value);
}
elseif (is_scalar($value)) {
$baseKey .= $value;
}
$baseKey .= ';';
}
return rtrim($baseKey, ';');
}

/**
* Gets the cache key for given base key and request options.
*
* @param string $baseKey
* The base key.
* @param array $options
* The request options.
*
* @return string
* The cache key.
*/
protected function getCacheKey(string $baseKey, array $options = []) : string {
return rtrim(
sprintf('%s:%s', $baseKey, $this->requestOptionsToCacheKey('', $options)),
':'
);
}

}
81 changes: 81 additions & 0 deletions tests/src/Unit/Cache/CacheKeyTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\helfi_api_base\Unit;

use Drupal\helfi_api_base\Cache\CacheKeyTrait;
use Drupal\Tests\UnitTestCase;

/**
* Tests cache key trait.
*
* @group helfi_api_base
*/
class CacheKeyTraitTest extends UnitTestCase {

use CacheKeyTrait;

/**
* @covers \Drupal\helfi_api_base\Cache\CacheKeyTrait::getCacheKey
* @covers \Drupal\helfi_api_base\Cache\CacheKeyTrait::requestOptionsToCacheKey
* @dataProvider cacheKeyData
*/
public function testGetCacheKey(string $expected, string $baseKey, array $options) : void {
$actual = $this->getCacheKey($baseKey, $options);
$this->assertEquals($expected, $actual);
}

/**
* Data provider for testRequestOptionsToCacheKey().
*
* @return array[]
* The data.
*/
public function cacheKeyData() : array {
return [
[
'test:1',
'test:1',
[],
],
[
'test:2:key=value',
'test:2',
[
'key' => 'value',
],
],
[
'test:3:value;key=value',
'test:3',
[
'value',
[
'key' => 'value',
],
],
],
[
':key1=value1;key2=value2;key3=value3',
'',
[
'key1' => 'value1',
'key2' => ['value2', 'key3' => 'value3'],
],
],
// Make sure non-scalar values are ignored.
[
'test:4:value',
'test:4',
[
'value',
[
(object) ['key' => 'value'],
],
],
],
];
}

}