Skip to content

Conversation

@roncodes
Copy link
Member

Description

This PR enhances the UserCacheService by incorporating the user's updated_at timestamp into the cache key generation. This change enables automatic cache busting when user data is modified, improving cache consistency and data freshness.

Changes Made

Core Changes

  • UserCacheService.php
    • Updated getCacheKey() to accept User object and include updated_at timestamp in the key
    • Modified get(), put(), and invalidate() methods to accept User objects instead of just user IDs
    • Updated all log statements to use $user->uuid consistently

Controller Updates

  • UserController.php
    • Updated current() method to pass the full $user object to cache service methods

Documentation Updates

  • UserObserver.php - Added clarifying comments about automatic cache busting
  • User.php - Added clarifying comments in assignSingleRole() method

Benefits

Automatic cache invalidation - When user data changes, the updated_at timestamp changes, resulting in a new cache key
Improved data consistency - Ensures stale data is never served from cache
Aligns with existing patterns - Matches the generateETag() implementation which already uses updated_at
Simplified cache management - Reduces reliance on explicit invalidation calls
Better cache efficiency - Old cache entries expire naturally via TTL

Cache Key Format

Before: user:current:{userId}:{companyId}
After: user:current:{userUuid}:{companyId}:{updatedAtTimestamp}

Example: user:current:abc-123-def:company-456:1707619200

Testing Recommendations

  • Verify cache key generation includes timestamp
  • Test that updating a user creates a new cache entry
  • Confirm old cache entries are not used after user updates
  • Validate cache behavior across user role changes

Backward Compatibility

This change is backward compatible. Old cache keys will naturally expire based on the configured TTL (default 15 minutes). No migration or manual cache clearing is required.

Related

Implements the technical plan for improving user cache management by leveraging timestamp-based cache keys.

This enhancement adds the user's updated_at timestamp to the cache key
generation in UserCacheService, enabling automatic cache busting when
user data is modified.

Changes:
- Updated getCacheKey() to accept User object and include updated_at timestamp
- Modified get(), put(), and invalidate() methods to work with User objects
- Updated UserController to pass User object to cache methods
- Added clarifying comments in UserObserver and User model

Benefits:
- Automatic cache invalidation when user data changes
- Improved cache consistency and data freshness
- Aligns with existing ETag implementation
- Simplifies cache management logic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant