@@ -18,6 +18,8 @@ class PermissionCacheService implements PermissionCacheInterface
1818
1919 private const LEGACY_USER_PERMISSIONS_KEY = 'user ' ;
2020
21+ private const USER_PERMISSION_GROUPS_KEY = 'user_permission_groups ' ;
22+
2123 private const TRACKED_PERMISSION_KEYS = 'permission_cache_keys ' ;
2224
2325 private const TRACKED_PERMISSION_KEYS_LOCK = 'permission_cache_keys_lock ' ;
@@ -91,11 +93,42 @@ public function cacheGroupPermissions(int $groupId, array $permissions): void
9193 /**
9294 * Invalidate user permissions cache
9395 */
96+ public function rememberUserPermissionGroups (int $ userId , int $ ttl , callable $ callback ): array
97+ {
98+ $ key = $ this ->getUserPermissionGroupsKey ($ userId );
99+
100+ try {
101+ $ groups = Cache::remember ($ key , $ ttl , $ callback );
102+ $ this ->trackPermissionKey ($ key );
103+
104+ return is_array ($ groups ) ? $ groups : [];
105+ } catch (\Exception $ e ) {
106+ Log::warning ("Failed to remember user permission groups for user {$ userId }: " . $ e ->getMessage ());
107+
108+ $ groups = $ callback ();
109+
110+ return is_array ($ groups ) ? $ groups : [];
111+ }
112+ }
113+
114+ public function forgetUserPermissionGroups (int $ userId ): void
115+ {
116+ $ key = $ this ->getUserPermissionGroupsKey ($ userId );
117+
118+ try {
119+ Cache::forget ($ key );
120+ $ this ->untrackPermissionKey ($ key );
121+ } catch (\Exception $ e ) {
122+ Log::warning ("Failed to forget user permission groups for user {$ userId }: " . $ e ->getMessage ());
123+ }
124+ }
125+
94126 public function invalidateUserPermissions (int $ userId ): void
95127 {
96128 $ keys = [
97129 $ this ->getUserPermissionsKey ($ userId ),
98130 $ this ->getLegacyUserPermissionsKey ($ userId ),
131+ $ this ->getUserPermissionGroupsKey ($ userId ),
99132 ];
100133
101134 try {
@@ -216,6 +249,11 @@ private function getLegacyUserPermissionsKey(int $userId): string
216249 return self ::LEGACY_USER_PERMISSIONS_KEY . "_ {$ userId }_permissions " ;
217250 }
218251
252+ private function getUserPermissionGroupsKey (int $ userId ): string
253+ {
254+ return self ::USER_PERMISSION_GROUPS_KEY . ": {$ userId }" ;
255+ }
256+
219257 /**
220258 * Warm up cache for a user
221259 */
0 commit comments