You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+14-8Lines changed: 14 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -188,9 +188,7 @@ Durability only applies when connectors throw a recoverable exception type. If a
188
188
Caching
189
189
-------
190
190
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).
194
192
195
193
### Cache advice
196
194
@@ -214,15 +212,23 @@ $records = $porter->import(
214
212
);
215
213
```
216
214
217
-
### Cache key
215
+
### Cache keys
218
216
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`.
220
218
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
222
220
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
224
230
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.
226
232
227
233
```php
228
234
class MyCacheKeyGenerator implements CacheKeyGenerator
0 commit comments