Skip to content
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"require": {
"php": "^7.0",
"api-clients/middleware": "^1.0",
"guzzlehttp/psr7": "^1.3",
"react/cache": "^0.4.1"
"react/cache": "^0.4.1",
"ringcentral/psr7": "^1.2"
},
"require-dev": {
"api-clients/test-utilities": "^2.0"
Expand Down
120 changes: 60 additions & 60 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions src/CacheKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

namespace ApiClients\Middleware\Cache;

use Psr\Http\Message\UriInterface;

final class CacheKey
{
/**
* Create a cache key based on the URI and glue
*
* @param UriInterface $uri
* @param string $glue
* @return string
*/
public static function create(UriInterface $uri, string $glue): string
{
return self::stripExtraSlashes(
implode(
$glue,
[
(string)$uri->getScheme(),
(string)$uri->getHost(),
(string)$uri->getPort(),
self::chunkUp(md5((string)$uri->getPath()), $glue),
self::chunkUp(md5((string)$uri->getQuery()), $glue),
]
),
$glue
);
}

/**
* @param string $string
* @param string $glue
* @return string
*/
private static function chunkUp(string $string, string $glue): string
{
return implode($glue, str_split($string, 2));
}

/**
* @param string $string
* @return string
*/
private static function stripExtraSlashes(string $string, string $glue): string
{
return preg_replace('#' . $glue . '+#', $glue, $string);
}
}
Loading