-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generalized cache handling (#36)
* Add generalized cache handling * Create CHANGELOG.md * Update ConfigCache.php * Update ConfigEntry.php * fetchTime int -> float * Fix remaining float conversion issues
- Loading branch information
Showing
17 changed files
with
306 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Please check the [Github Releases](https://github.com/configcat/php-sdk/releases) page for the changelog of the ConfigCat SDK for PHP. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace ConfigCat\Cache; | ||
|
||
use UnexpectedValueException; | ||
|
||
/** | ||
* Represents the cached configuration. | ||
* @package ConfigCat | ||
*/ | ||
class ConfigEntry | ||
{ | ||
private static ?ConfigEntry $empty = null; | ||
|
||
private function __construct( | ||
private readonly string $configJson, | ||
private readonly array $config, | ||
private readonly string $etag, | ||
private readonly float $fetchTime, | ||
) { | ||
} | ||
|
||
public function getConfigJson(): string | ||
{ | ||
return $this->configJson; | ||
} | ||
|
||
public function getConfig(): array | ||
{ | ||
return $this->config; | ||
} | ||
|
||
public function getEtag(): string | ||
{ | ||
return $this->etag; | ||
} | ||
|
||
public function getFetchTime(): float | ||
{ | ||
return $this->fetchTime; | ||
} | ||
|
||
public function serialize(): string | ||
{ | ||
return $this->fetchTime . "\n" . $this->etag . "\n" . $this->configJson; | ||
} | ||
|
||
public function withTime(float $time): ConfigEntry | ||
{ | ||
return new ConfigEntry($this->configJson, $this->config, $this->etag, $time); | ||
} | ||
|
||
public static function empty(): ConfigEntry | ||
{ | ||
if (self::$empty == null) { | ||
self::$empty = new ConfigEntry("", [], "", 0); | ||
} | ||
|
||
return self::$empty; | ||
} | ||
|
||
public static function fromConfigJson(string $configJson, string $etag, float $fetchTime): ConfigEntry | ||
{ | ||
$deserialized = json_decode($configJson, true); | ||
if ($deserialized == null) { | ||
return self::empty(); | ||
} | ||
|
||
return new ConfigEntry($configJson, $deserialized, $etag, $fetchTime); | ||
} | ||
|
||
public static function fromCached(string $cached): ConfigEntry | ||
{ | ||
$timePos = strpos($cached, "\n"); | ||
$etagPos = strpos($cached, "\n", $timePos + 1); | ||
|
||
if ($timePos === false || $etagPos === false) { | ||
throw new UnexpectedValueException("Number of values is fewer than expected."); | ||
} | ||
|
||
$fetchTimeString = substr($cached, 0, $timePos); | ||
$fetchTime = floatval($fetchTimeString); | ||
|
||
if ($fetchTime == 0) { | ||
throw new UnexpectedValueException("Invalid fetch time: " . $fetchTimeString); | ||
} | ||
|
||
$etag = substr($cached, $timePos + 1, $etagPos - $timePos - 1); | ||
$configJson = substr($cached, $etagPos + 1); | ||
|
||
return self::fromConfigJson($configJson, $etag, $fetchTime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.