Skip to main content

Snapshot Providers

Snapshot providers store feature flag definitions locally, enabling faster startup and continued operation during temporary API outages.

File Provider​

Store snapshots as JSON files on disk (default for core library).

use Toggly\FeatureManagement\Storage\SnapshotProviders\FileSnapshotProvider;
use Toggly\FeatureManagement\Storage\SnapshotSettings;

$settings = new SnapshotSettings(['document_name' => 'toggly_features']);
$provider = new FileSnapshotProvider('/var/lib/toggly', $settings);

Cache Provider​

Store snapshots using PSR-16 SimpleCache.

use Toggly\FeatureManagement\Storage\SnapshotProviders\CacheSnapshotProvider;
use Toggly\FeatureManagement\Storage\SnapshotSettings;

$settings = new SnapshotSettings(['document_name' => 'toggly_features']);
$provider = new CacheSnapshotProvider($cache, $settings, 86400);

Database Provider (PDO)​

Store snapshots in any database supported by PDO (MySQL, PostgreSQL, SQLite, etc.).

use Toggly\FeatureManagement\Storage\SnapshotProviders\DatabaseSnapshotProvider;
use Toggly\FeatureManagement\Storage\SnapshotSettings;

$pdo = new PDO('mysql:host=localhost;dbname=myapp', 'user', 'password');
$settings = new SnapshotSettings(['document_name' => 'toggly_features']);

$provider = new DatabaseSnapshotProvider(
$pdo,
$settings,
'toggly_snapshots', // Table name
'toggly_jwk_snapshots' // JWK table name
);

The provider creates the required tables automatically in the constructor.

MongoDB Provider​

Store snapshots in MongoDB for high availability and scalability.

use Toggly\FeatureManagement\Storage\SnapshotProviders\MongoDBSnapshotProvider;
use MongoDB\Client;

$client = new Client('mongodb://localhost:27017');

$provider = new MongoDBSnapshotProvider(
$client,
'toggly', // Database name
'snapshots', // Collection name
'toggly_features', // Definitions document ID
'toggly_jwks' // JWKs document ID
);

With Existing Collection​

$client = new Client('mongodb://localhost:27017');
$collection = $client->selectDatabase('myapp')->selectCollection('feature_flags');

$provider = new MongoDBSnapshotProvider($collection);
info

The MongoDB provider requires the mongodb/mongodb package:

composer require mongodb/mongodb

Laravel Cache Provider​

For Laravel applications, use the native Laravel cache integration.

use Toggly\Laravel\Storage\LaravelCacheSnapshotProvider;

$provider = new LaravelCacheSnapshotProvider(
app('cache'),
'redis', // Cache store (null for default)
'toggly', // Key prefix
3600 // TTL in seconds (null for forever)
);

Via Configuration​

Configure in config/toggly.php:

return [
// ...
'snapshot' => [
'provider' => 'cache', // 'file', 'database', 'cache', or 'mongodb'
'cache' => [
'store' => 'redis',
'prefix' => 'toggly',
'ttl' => 3600,
],
],
];

Provider Comparison​

ProviderBest ForRequires
FileSimple setups, single serverFilesystem access
CachePSR-16 compatible appsPSR-16 cache implementation
DatabaseExisting database appsPDO
MongoDBNoSQL, high availabilitymongodb/mongodb package
Laravel CacheLaravel applicationsLaravel framework

Clearing snapshots​

All providers implement clear() to delete persisted feature and JWKS snapshots:

$provider->clear();
// Or via the feature provider:
$featureProvider->clearPersistedSnapshots();

Signed snapshots store exact signedDefsJson for cold-start verification. Configure $onError / on_error on settings for signature and fetch failures. See Server-side reliability.

Custom Provider​

Implement the FeatureSnapshotProviderInterface:

use Toggly\FeatureManagement\Contracts\FeatureSnapshotProviderInterface;

class MyProvider implements FeatureSnapshotProviderInterface
{
public function saveSnapshot(
array $features,
?string $signature = null,
?string $keyId = null,
?int $timestamp = null,
?string $signedDefsJson = null,
?string $etag = null
): void {
// Save features + signedDefsJson to your storage
}

public function getFeaturesSnapshot(): array
{
// Return features, signature, keyId, timestamp, signedDefsJson, etag
}

public function saveJwkSnapshot(JsonWebKeySet $jwks, int $timestamp): void
{
// Save JWKs to your storage
}

public function getJwkSnapshot(): array
{
// Return ['jwks' => JsonWebKeySet, 'timestamp' => ...]
}

public function clear(): void
{
// Delete feature + JWKS snapshots
}
}