1. Go to this page and download the library: Download qoliber/trident-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
qoliber / trident-php example snippets
use Qoliber\Trident\TridentFactory;
// Create from environment variables
$trident = TridentFactory::createFromEnv();
// Or create with explicit configuration
$trident = TridentFactory::create(
adminUrl: 'http://localhost:9100',
apiKey: 'your-admin-api-key'
);
// Check health
if ($trident->isHealthy()) {
echo "Trident is running!";
}
// Get cache stats
$stats = $trident->stats();
echo "Hit ratio: {$stats->getHitRatioPercent()}%";
// Single tag
$trident->purgeTag('product.123');
// Multiple tags (OR - purge if ANY tag matches)
$trident->purgeTags(['product.1', 'product.2', 'product.3']);
// Multiple tags (AND - purge if ALL tags match)
$trident->purgeTagsAll(['category.electronics', 'featured']);
// Pattern matching (wildcard)
$trident->purgeTagPattern('product.*');
// Pattern matching (regex)
$trident->purgeTagPattern('product\.\d+', 'regex');
// Hard purge (remove immediately)
$trident->purgeUrl('https://example.com/product/123');
// Soft purge (mark stale, serve while revalidating)
$trident->purgeUrl('https://example.com/product/123', soft: true);
use Qoliber\Trident\Cache\TagCollection;
// Build a collection of tags
$tags = TagCollection::create()
->addProduct(123)
->addCategory(45)
->add('store.default')
->add('featured');
// Use in response headers
header('X-Cache-Tags: ' . $tags->toHeader());
// Or purge the collection
$trident->purgeCollection($tags);
use Qoliber\Trident\Cache\TagResolver;
$resolver = new TagResolver();
// Register custom resolver for your entities
$resolver->register('product', function ($product) {
return [
"product.{$product->getId()}",
"category.{$product->getCategoryId()}",
"store.{$product->getStoreId()}",
];
});
// Resolve tags from entities
$tags = $resolver->resolve('product', $product);
use Qoliber\Trident\Delivery\{Instances, Purger, PurgeClient, Psr18Transport};
// 1. OutboxStore on the platform's own database layer, written through the SAME
// connection the platform saves entities with. `record()` takes a due time
// (the writer's grace) and returns the row id; `byIds()` returns rows by id.
// Binary collation on `instance`. See WpdbOutboxStore in
// integrations/ecommerce/woocommerce for a complete implementation.
$store = new MyDoctrineOutboxStore($connection);
// 2. Transport: Psr18Transport for Composer-native platforms, or your own
// (WordPress HTTP API, Magento Curl).
$transport = new Psr18Transport($psr18Client, $requestFactory, $streamFactory);
[$instances, $errors] = Instances::parse($deploymentConfigList, $adminUrl, $adminToken);
$purger = new Purger(
$store,
$instances,
fn ($i) => new PurgeClient($i, $transport),
'soft',
fn (callable $deliver) => $eventDispatcher->addListener('kernel.terminate', $deliver), // end of request
);
$purger->purgeTags(['product_42', 'category_7']); // on save
$purger->drain(500); // from cron / a CLI command
$purger->drain(500, ignoreBackoff: true); // an operator's "deliver now"
use Qoliber\Trident\Admin\Fleet;
use Qoliber\Trident\Client\TridentClient;
$fleet = new Fleet($instances, $transport); // the same instances and transport the purges use
foreach ($fleet->each(fn (TridentClient $c) => $c->stats()) as $result) {
echo $result->isOk()
? sprintf("%s: %.1f%% hits\n", $result->name(), $result->value->getHitRatioPercent())
: sprintf("%s: %s\n", $result->name(), $result->reason());
}
$fleet->on(['edge-1'], fn (TridentClient $c) => $c->purgeUrl('https://shop.example/p/beanie/'));
use Qoliber\Trident\Middleware\CacheTagMiddleware;
// Add to your middleware stack
$middleware = new CacheTagMiddleware(
headerName: 'X-Cache-Tags',
separator: ','
);
// In your request handlers, add tags:
CacheTagMiddleware::addTags($request, 'product.123', 'category.45');
use Qoliber\Trident\Middleware\CacheControlMiddleware;
$middleware = new CacheControlMiddleware(
defaultTtl: 3600, // 1 hour
defaultSwr: 60, // 60 seconds stale-while-revalidate
defaultPrivate: false
);
// Disable caching for specific requests
$request = CacheControlMiddleware::disableCache($request);
// Set custom TTL
$request = CacheControlMiddleware::setTtl($request, 300);
// Mark as private (user-specific)
$request = CacheControlMiddleware::setPrivate($request);
bash
composer
bash
# Run unit tests
composer test
# Run with Docker (integration tests)
cd docker
docker-compose up -d
docker-compose exec php-app php /var/www/html/test-api.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.