PHP code example of daikazu / eloquent-salesforce-cache
1. Go to this page and download the library: Download daikazu/eloquent-salesforce-cache 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/ */
daikazu / eloquent-salesforce-cache example snippets
// This query result is cached on first execution and served from Redis on subsequent calls.
$opportunities = Opportunity::all();
// Write operations automatically invalidate the relevant object cache.
$opportunity = Opportunity::find('0061a00000AbCdEfG');
$opportunity->update(['StageName' => 'Closed Won']);
// Cache for the Opportunity object type is now flushed.
use Daikazu\EloquentSalesforceCache\Concerns\CachesSalesforceQueries;
class Opportunity extends SalesforceObject
{
use CachesSalesforceQueries;
protected int $cacheTtl = 3600;
protected array $trackedRelationships = [
'lineItems',
'payments',
];
}
class OpportunityLineItem extends SalesforceObject
{
use CachesSalesforceQueries;
protected array $invalidatesObjects = ['Opportunity'];
}
// Instance method — invalidates this specific record
$opportunity->invalidateCache();
// Instance method — invalidates this record and all tracked related records
$opportunity->invalidateCacheWithRelationships();
// Static method — invalidates a record by ID
Opportunity::invalidateCacheFor('0061a00000AbCdEfG');
// Static method — invalidates multiple records by ID
Opportunity::invalidateCacheForMany(['0061a00000AbCdEfG', '0061a00000HiJkLm']);
// Static method — flushes all cached queries for this object type
Opportunity::flushAllCache();
use Daikazu\EloquentSalesforceCache\Events\CacheInvalidated;
use App\Listeners\LogCacheInvalidation;
protected $listen = [
CacheInvalidated::class => [
LogCacheInvalidation::class,
],
];
namespace App\Listeners;
use Daikazu\EloquentSalesforceCache\Events\CacheInvalidated;
use Illuminate\Support\Facades\Log;
class LogCacheInvalidation
{
public function handle(CacheInvalidated $event): void
{
Log::info('Salesforce cache invalidated', [
'object' => $event->object,
'ids' => $event->ids,
'scope' => $event->scope,
]);
}
}
use Daikazu\EloquentSalesforceCache\Events\CacheInvalidated;
use Illuminate\Support\Facades\Event;
Event::listen(CacheInvalidated::class, function (CacheInvalidated $event): void {
// Notify a monitoring system, invalidate a secondary cache, etc.
});