Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: add optional cache for feature flags #43

Open
drjayvee opened this issue Jan 27, 2023 · 0 comments
Open

Proposal: add optional cache for feature flags #43

drjayvee opened this issue Jan 27, 2023 · 0 comments

Comments

@drjayvee
Copy link
Contributor

I'm trialing PostHog for our SaaS app. For us, the lack of caching for feature flags are a total no-go. (We're currently canary-releasing a feature that requires a decide on every single page view.)

So I've added caching by extending the PostHog\Client and overriding fetchFeatureVariants.

<?php

use PostHog\Client;
use PostHog\HttpClient;
use Psr\Cache\CacheItemPoolInterface;

class CachingClient extends Client {
	
	private const DEFAULT_EXPIRY_DURATION = 'PT5M';    // five minutes
	
	private ?CacheItemPoolInterface $cache;
	private \DateInterval $cacheExpiryInterval;
	
	public function __construct(
		string $apiKey,
		array $options = [],
		?HttpClient $httpClient = null,
		string $personalAPIKey = null,
		CacheItemPoolInterface $cache = null
	) {
		parent::__construct($apiKey, $options, $httpClient, $personalAPIKey);
		
		$this->cache = $cache;
		$this->cacheExpiryInterval = new \DateInterval($options['cacheDuration'] ?? self::DEFAULT_EXPIRY_DURATION);
	}
	
	public function fetchFeatureVariants(string $distinctId, array $groups = [], array $personProperties = [], array $groupProperties = []): array {
		$callParent = fn() => parent::fetchFeatureVariants($distinctId, $groups, $personProperties, $groupProperties);
		
		if (!$this->cache) {
			return $callParent();
		}
		
		$query = substr(md5(json_encode([$groups, $personProperties, $groupProperties])), 0, 8);	// yes, md5 is totally suitable here: it's fast and provides a good distribution
		$cacheItem = $this->cache->getItem("FeatureFlags.dId=$distinctId.q=$query");
		
		if ($cacheItem->isHit()) {
			return $cacheItem->get();
		}
		
		$result = $callParent();
		
		$this->cache->save(
			$cacheItem
				->expiresAfter($this->cacheExpiryInterval)
				->set($result)
		);
		
		return $result;
	}
	
}

I'd be happy to create a PR to add this to the base class.

@drjayvee drjayvee changed the title Proposal add optional cache for feature flags Proposal: add optional cache for feature flags Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant