Skip to content

Commit

Permalink
Implement caching for better performance - requires cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
cannycookie committed Dec 16, 2024
1 parent 8f55593 commit 9ba8e15
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Livewire/ConsentOptionFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ public function submit(): void
'key' => $consentOption->key,
]
);
$pivotModel = ConsentOptionUser::query()
->where('consentable_type', $this->user->getMorphClass())
->where('consentable_id', $this->user->id)
->where('consent_option_id', $consentOption->id)
->first();

$pivotModel::clearCache($pivotModel);

if($consentOption->questions->count() > 0) {
$consentable = $this->user->consents()->where('consent_option_id', $consentOption->id)->first();
Expand Down Expand Up @@ -240,6 +247,7 @@ public function submit(): void
}



Notification::make()
->title('Success')
->body('Your consent preferences have been saved.')
Expand Down
19 changes: 19 additions & 0 deletions src/Models/ConsentOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Visualbuilder\FilamentUserConsent\Database\Factories\ConsentOptionFactory;
Expand Down Expand Up @@ -138,6 +139,23 @@ public static function getAllUserTypes(): Collection
return $models;
}

public static function activeKeysForUser($user): array
{
$className = class_basename($user);
$cacheKey = 'consent_option_active_keys_for_'.$className.'_'.$user->id;
return Cache::tags(['user-consents'])->rememberForever($cacheKey, function () use ($user, $className) {
return self::where('models', 'like', "%$className%")
->where('is_current', true)
->where('enabled', true)
->where('published_at', '<=', now())
->filterByOrganisations(self::getOrganisationIdsForUser($user))
->filterByProducts(self::getProductIdsForUser($user))
->filterByProductCategories(self::getProductCategoryIdsForUser($user))
->pluck('key')
->toArray();
});
}

public static function getAllActiveKeysbyUserClass($className, $survey = false): array
{
return self::where('models', 'like', "%$className%")
Expand All @@ -149,6 +167,7 @@ public static function getAllActiveKeysbyUserClass($className, $survey = false):
->toArray();
}


public static function getAllKeys(): array
{
return self::query()
Expand Down
35 changes: 35 additions & 0 deletions src/Models/ConsentOptionUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Visualbuilder\FilamentUserConsent\Models;

use App\Models\Scopes\AssociateConsentScope;
use App\Models\Scopes\EndUserConsentScope;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Cache;

/**
* @property int $id
Expand All @@ -32,6 +35,38 @@ class ConsentOptionUser extends MorphPivot

protected $table = 'consentables';


protected static function booted()
{

static::saved(function ($consentOptionUser) {
self::clearCache($consentOptionUser);
});

static::deleted(function ($consentOptionUser) {
self::clearCache($consentOptionUser);
});
}

public static function clearCache($consentOptionUser)
{
$userKey = class_basename($consentOptionUser->consentable_type).'_'.$consentOptionUser->consentable_id;
$cacheKey1 = 'consent_option_active_keys_for_'.$userKey;
$cacheKey2 = 'user_consent_'.$userKey;

// Similarly, flush the cache when a consent option user relationship is deleted
// Flush the cache for the specific user involved using a simplified class name
Cache::tags([
'user-consents',
$cacheKey1
])->flush();

Cache::tags([
'user-consents',
$cacheKey2
])->flush();
}

public static function getAllSavedUserTypes(): array
{
return self::query()->select('consentable_type')->distinct()->pluck('consentable_type')->toArray();
Expand Down
5 changes: 2 additions & 3 deletions src/Traits/HasConsent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ public function requiredConsents()
{
return ConsentOption::findbykeys($this->requiredConsentKeys())->get();
}

public function requiredConsentKeys(): array
{
return ConsentOption::getAllActiveKeysbyUserClass(class_basename($this));
return ConsentOption::activeKeysForUser($this);
}

public function outstandingConsentValidators()
{
$consents = $this->outstandingConsents();
Expand Down

0 comments on commit 9ba8e15

Please sign in to comment.