Skip to content

Commit

Permalink
Separate MetricsCache from common Cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
v.bartusevicius committed Nov 1, 2023
1 parent d60019b commit 809b7e0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,7 @@ $unleash = UnleashBuilder::create()
->withMetricsEnabled(false) // turn off metric sending
->withMetricsEnabled(true) // turn on metric sending
->withMetricsInterval(10_000) // interval in milliseconds (10 seconds)
->withMetricsCacheHandler(new Psr16Cache(new RedisAdapter())) // use custom cache handler for metrics, defaults to standard cache handler
->build();

// the metric will be collected but not sent immediately
Expand Down
13 changes: 13 additions & 0 deletions src/Configuration/UnleashConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function __construct(
private int $staleTtl = 30 * 60,
private ?CacheInterface $staleCache = null,
private ?string $proxyKey = null,
private ?CacheInterface $metricsCache = null,
) {
$this->contextProvider ??= new DefaultUnleashContextProvider();
if ($defaultContext !== null) {
Expand All @@ -66,6 +67,11 @@ public function getStaleCache(): CacheInterface
return $this->staleCache ?? $this->getCache();
}

public function getMetricsCache(): CacheInterface
{
return $this->metricsCache ?? $this->getCache();
}

public function getUrl(): string
{
$url = $this->url;
Expand Down Expand Up @@ -117,6 +123,13 @@ public function setStaleCache(?CacheInterface $cache): self
return $this;
}

public function setMetricsCache(?CacheInterface $cache): self
{
$this->metricsCache = $cache;

return $this;
}

public function setTtl(int $ttl): self
{
$this->ttl = $ttl;
Expand Down
6 changes: 3 additions & 3 deletions src/Metrics/DefaultMetricsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function handleMetrics(Feature $feature, bool $successful, Variant $varia

private function getOrCreateBucket(): MetricsBucket
{
$cache = $this->configuration->getCache();
$cache = $this->configuration->getMetricsCache();

$bucket = null;
if ($cache->has(CacheKey::METRICS_BUCKET)) {
Expand Down Expand Up @@ -62,15 +62,15 @@ private function send(MetricsBucket $bucket): void
{
$bucket->setEndDate(new DateTimeImmutable());
$this->metricsSender->sendMetrics($bucket);
$cache = $this->configuration->getCache();
$cache = $this->configuration->getMetricsCache();
if ($cache->has(CacheKey::METRICS_BUCKET)) {
$cache->delete(CacheKey::METRICS_BUCKET);
}
}

private function store(MetricsBucket $bucket): void
{
$cache = $this->configuration->getCache();
$cache = $this->configuration->getMetricsCache();
$cache->set(CacheKey::METRICS_BUCKET, $bucket);
}
}
10 changes: 10 additions & 0 deletions src/UnleashBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ final class UnleashBuilder

private ?CacheInterface $staleCache = null;

private ?CacheInterface $metricsCache = null;

private ?int $cacheTtl = null;

private ?int $staleTtl = null;
Expand Down Expand Up @@ -223,6 +225,12 @@ public function withStaleCacheHandler(?CacheInterface $cache): self
return $this->with('staleCache', $cache);
}

#[Pure]
public function withMetricsCacheHandler(?CacheInterface $cache): self
{
return $this->with('metricsCache', $cache);
}

#[Pure]
public function withCacheTimeToLive(int $timeToLive): self
{
Expand Down Expand Up @@ -412,6 +420,7 @@ public function build(): Unleash
}
assert($cache instanceof CacheInterface);
$staleCache = $this->staleCache ?? $cache;
$metricsCache = $this->metricsCache ?? $cache;

$httpClient = $this->httpClient;
if ($httpClient === null) {
Expand Down Expand Up @@ -488,6 +497,7 @@ public function build(): Unleash
$configuration
->setCache($cache)
->setStaleCache($staleCache)
->setMetricsCache($metricsCache)
->setTtl($this->cacheTtl ?? $configuration->getTtl())
->setStaleTtl($this->staleTtl ?? $configuration->getStaleTtl())
->setMetricsEnabled($this->metricsEnabled ?? $configuration->isMetricsEnabled())
Expand Down
23 changes: 23 additions & 0 deletions tests/UnleashBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,29 @@ public function testWithStaleCacheHandler()
self::assertSame($cache1, $this->getConfiguration($instance->build())->getCache());
}

public function testWithMetricsCacheHandler()
{
$cache1 = $this->getCache();
$cache2 = $this->getCache();

$instance = $this->instance->withFetchingEnabled(false);
self::assertNull($this->getProperty($instance, 'metricsCache'));
self::assertNotNull($this->getConfiguration($instance->build())->getMetricsCache());

$instance = $this->instance->withFetchingEnabled(false)->withCacheHandler($cache1);
self::assertNull($this->getProperty($instance, 'metricsCache'));
self::assertSame($cache1, $this->getConfiguration($instance->build())->getMetricsCache());

$instance = $this->instance
->withFetchingEnabled(false)
->withCacheHandler($cache1)
->withMetricsCacheHandler($cache2)
;
self::assertSame($cache2, $this->getProperty($instance, 'metricsCache'));
self::assertSame($cache2, $this->getConfiguration($instance->build())->getMetricsCache());
self::assertSame($cache1, $this->getConfiguration($instance->build())->getCache());
}

public function testWithMetricsHandler()
{
$metricsHandler = new class implements MetricsHandler {
Expand Down

0 comments on commit 809b7e0

Please sign in to comment.