Skip to content

Commit c13eb95

Browse files
committed
Copy edit "cache key" section of readme.
Minor style changes to tests.
1 parent e484fae commit c13eb95

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,7 @@ Durability only applies when connectors throw a recoverable exception type. If a
188188
Caching
189189
-------
190190

191-
Caching is available at the connector level if the connector implements `CacheToggle`. Connectors typically extend `CachingConnector` which implements [PSR-6][PSR-6]-compatible caching. Porter ships with just one cache implementation, `MemoryCache`, which stores data in memory but this can be substituted for any PSR-6 cache if the connector permits it.
192-
193-
When available, the connector caches raw responses for each unique [cache key](#cache-key). Cache keys are generated by an implementation-defined strategy or the default `JsonCacheKeyGenerator` strategy.
191+
Caching is available at the connector level if the connector implements `CacheToggle`. Connectors typically extend `CachingConnector` which implements [PSR-6][PSR-6]-compatible caching. Porter ships with just one cache implementation, `MemoryCache`, which stores data in memory but this can be substituted for any PSR-6 cache if the connector permits it. When available, the connector caches raw responses for each unique [cache key](#cache-keys).
194192

195193
### Cache advice
196194

@@ -214,15 +212,23 @@ $records = $porter->import(
214212
);
215213
```
216214

217-
### Cache key
215+
### Cache keys
218216

219-
The cache key can optionally be generated by an implementation of `CacheKeyGeneratorInterface` if the connector permits it. This implementation should provide one method `generateCacheKey` which returns a [PSR-6][PSR-6]-compatible cache key.
217+
The cache key is generated by a `CacheKeyGenerator` that encodes parameters to produce a unique cache key for each distinct `Connector::fetch` request. The default `JsonCacheKeyGenerator` simply JSON-encodes the parameters to create a cache key. The cache key generation strategy may be changed by calling `CachingConnector::setCacheKeyGenerator`.
220218

221-
The default implementation `JsonCacheKeyGenerator` generates keys comprised of the source and options parameters passed to `Connector::fetch`. Options are sorted before the cache key is created so the order of options are insignificant.
219+
#### Writing a cache key generator
222220

223-
#### Implementation example
221+
The `CacheKeyGenerator` interface defines one method with the following interface.
222+
223+
```php
224+
public function generateCacheKey(string $source, array $sortedOptions) : string;
225+
```
226+
227+
Implementations receive arguments similar to [connectors](#connectors), with the notable exception that the options parameter is converted to an array and sorted so that the same options specified in a different order result in the same cache key. The method must return a [PSR-6][PSR-6] compatible cache key.
228+
229+
##### Implementation example
224230

225-
The following example demonstrates a simple cache key generation implementation using an md5 hash of the json encoded parameters.
231+
The following example demonstrates cache key generation using a hash of JSON-encoded parameters.
226232

227233
```php
228234
class MyCacheKeyGenerator implements CacheKeyGenerator

src/Connector/CachingConnector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,14 @@ public function setCacheKeyGenerator(CacheKeyGenerator $cacheKeyGenerator)
108108
*
109109
* @return string
110110
*
111-
* @throws InvalidCacheKeyException
111+
* @throws InvalidCacheKeyException Cache key contains invalid data.
112112
*/
113113
private function validateCacheKey($key)
114114
{
115115
if (!is_string($key)) {
116-
throw new InvalidCacheKeyException('Cache key must be of type string.');
116+
throw new InvalidCacheKeyException('Cache key must be a string.');
117117
}
118+
118119
if (strpbrk($key, self::RESERVED_CHARACTERS) !== false) {
119120
throw new InvalidCacheKeyException(
120121
sprintf('Cache key "%s" contains one or more reserved characters: "%s"', $key, self::RESERVED_CHARACTERS)

test/Integration/Porter/Connector/CachingConnectorTest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ public function testCacheUsedForCacheKeyGenerator()
9797

9898
public function testFetchThrowsInvalidCacheKeyExceptionOnNonStringCackeKey()
9999
{
100-
$this->setExpectedException(InvalidCacheKeyException::class, 'Cache key must be of type string.');
101-
102100
$this->connector->setCacheKeyGenerator(
103101
\Mockery::mock(CacheKeyGenerator::class)
104102
->shouldReceive('generateCacheKey')
@@ -107,21 +105,14 @@ public function testFetchThrowsInvalidCacheKeyExceptionOnNonStringCackeKey()
107105
->getMock()
108106
);
109107

108+
$this->setExpectedException(InvalidCacheKeyException::class, 'Cache key must be a string.');
110109
$this->connector->fetch('quux', $this->options);
111110
}
112111

113112
public function testFetchThrowsInvalidCacheKeyExceptionOnNonPSR6CompliantCacheKey()
114113
{
115114
$cacheKey = CachingConnector::RESERVED_CHARACTERS;
116115

117-
$this->setExpectedException(
118-
InvalidCacheKeyException::class,
119-
sprintf('Cache key "%s" contains one or more reserved characters: "%s"',
120-
$cacheKey,
121-
CachingConnector::RESERVED_CHARACTERS
122-
)
123-
);
124-
125116
$this->connector->setCacheKeyGenerator(
126117
\Mockery::mock(CacheKeyGenerator::class)
127118
->shouldReceive('generateCacheKey')
@@ -130,6 +121,7 @@ public function testFetchThrowsInvalidCacheKeyExceptionOnNonPSR6CompliantCacheKe
130121
->getMock()
131122
);
132123

124+
$this->setExpectedException(InvalidCacheKeyException::class, 'contains one or more reserved characters');
133125
$this->connector->fetch('quux', $this->options);
134126
}
135127

0 commit comments

Comments
 (0)