creative-web-solution/cws-cognix-cache-bundle
Install the Bundle
Add the bundle to your composer.json:
composer require creative-web-solution/cws-cognix-cache-bundle
Then enable it in config/bundles.php:
Cws\Bundle\CognixCacheBundle\CwsCognixCacheCacheBundle::class => ['all' => true],
Configure the Bundle
Create config/packages/cws_cognix_cache.yaml:
cws_cognix_cache:
uri: 'https://your-cognix-server.com/api/cache' # Replace with your Cognix server URI
First Use Case: Clear Cognix Cache
Inject the CognixCacheService into a controller or service:
use Cws\Bundle\CognixCacheBundle\Service\CognixCacheService;
class MyController extends Controller
{
public function __construct(private CognixCacheService $cognixCache)
{
}
public function clearCache()
{
$this->cognixCache->clear();
return response()->json(['status' => 'Cache cleared on Cognix']);
}
}
Cache Clearing in Deployment Hooks
Trigger cache clearing post-deploy in post-deploy scripts or Laravel Forge/Envoyer hooks:
php artisan cognix:cache:clear
Or programmatically:
Artisan::call('cognix:cache:clear');
Conditional Cache Clearing Clear cache only for specific environments or routes:
if (app()->environment('production')) {
$this->cognixCache->clear();
}
Integration with Laravel Events
Listen to CacheCleared events or custom events to sync with Cognix:
Event::listen(CacheCleared::class, function () {
app(CognixCacheService::class)->clear();
});
Custom Cache Invalidation Logic Extend the service to invalidate specific cache keys:
$this->cognixCache->invalidate(['key1', 'key2']);
cws_cognix_cache:
uri: '...'
log_enabled: true # Enable in config
try {
$this->cognixCache->clear();
} catch (CognixCacheException $e) {
Log::error('Cognix cache clear failed: ' . $e->getMessage());
}
Authentication Issues
uri includes authentication (e.g., API keys). If Cognix requires headers (e.g., Authorization), extend the service:
$client = new \GuzzleHttp\Client([
'headers' => ['Authorization' => 'Bearer YOUR_TOKEN']
]);
$this->cognixCache->setClient($client);
URI Misconfiguration
uri ends with /api/cache or the correct endpoint. Test with:
php artisan cognix:cache:test-connection
CORS/Network Restrictions
Idempotency
clear() may not be idempotent. Add a check in your logic:
if (!$this->cognixCache->isCacheEmpty()) {
$this->cognixCache->clear();
}
Enable Verbose Logging
Add to config/packages/cws_cognix_cache.yaml:
cws_cognix_cache:
uri: '...'
debug: true
Logs will appear in storage/logs/laravel.log.
Test Locally Use a mock Cognix endpoint (e.g., Mockoon) to simulate responses before deploying.
Custom HTTP Client Override the default Guzzle client:
$this->cognixCache->setClient(new \GuzzleHttp\Client(['timeout' => 10]));
Add Headers Extend the service to include custom headers:
$this->cognixCache->setHeaders(['X-Custom-Header' => 'value']);
Event Dispatching Dispatch events after cache operations:
Event::dispatch(new CognixCacheCleared());
Cache Key Filtering Implement a filter for partial cache invalidation:
$this->cognixCache->invalidateMatching('/^user_\d+$/');
How can I help you explore Laravel packages today?