feat(kernel): Implement cache invalidation helper (A4)#19
Conversation
Add comprehensive cache invalidation system with pattern matching support. ## Features - **invalidate()** - Primary cache invalidation API with pattern matching - Supports prefix matching: ['thing', 'list'] matches all list queries - Can target specific stores or invalidate across all stores - Emits wpk.cache.invalidated event - **Cache key utilities**: - normalizeCacheKey() - Convert patterns to deterministic strings - matchesCacheKey() - Test if key matches pattern - findMatchingKeys() - Find all matching keys in collection - findMatchingKeysMultiple() - Find matches for multiple patterns - **Store registration** - Resources auto-register for invalidation tracking ## Implementation - packages/kernel/src/resource/cacheKeys.ts - Cache key utilities - packages/kernel/src/resource/invalidate.ts - Invalidation logic - Integrated with @wordpress/data registry - Handles graceful degradation when window.wp not available ## Tests - 22 tests for cache key utilities (100% coverage) - 16 tests for invalidation function (all paths tested) - All 233 tests passing ## Documentation - Updated docs/api/resources.md with invalidate() API reference - Added usage examples in Actions context - Documented pattern matching behavior Closes #4
There was a problem hiding this comment.
Pull Request Overview
This PR implements cache invalidation functionality (A4) for the WP Kernel framework. It provides the invalidate() function for Actions to manage cache state after write operations, ensuring UI consistency.
Key changes:
- Core cache invalidation API with pattern matching and store targeting
- Cache key utilities for normalization and matching operations
- Integration with WordPress data registry for store invalidation
- Comprehensive test coverage with 38 new tests
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
packages/kernel/src/resource/invalidate.ts |
Main invalidation function with store targeting and event emission |
packages/kernel/src/resource/cacheKeys.ts |
Cache key pattern matching utilities and normalization |
packages/kernel/src/resource/defineResource.ts |
Added store key registration on resource access |
packages/kernel/src/resource/__tests__/invalidate.test.ts |
16 tests covering invalidation scenarios and error handling |
packages/kernel/src/resource/__tests__/cacheKeys.test.ts |
22 tests for cache key utilities with 100% coverage |
packages/kernel/src/resource/index.ts |
Export new invalidation APIs |
packages/kernel/src/index.ts |
Export from main package entry point |
docs/api/resources.md |
API documentation with usage examples |
| if (process.env.NODE_ENV === 'development') { | ||
| console.warn( | ||
| `Failed to invalidate cache for store ${key}:`, | ||
| error | ||
| ); | ||
| } |
There was a problem hiding this comment.
[nitpick] Console.warn usage in production code should be replaced with a proper logging mechanism. Consider using the framework's error reporter or logging system instead of direct console calls.
| if (process.env.NODE_ENV === 'development') { | ||
| console.warn( | ||
| `Failed to invalidate all caches for store ${storeKey}:`, | ||
| error | ||
| ); | ||
| } |
There was a problem hiding this comment.
[nitpick] Console.warn usage in production code should be replaced with a proper logging mechanism. Consider using the framework's error reporter or logging system instead of direct console calls.
Major improvements: - Consolidated duplicate sections (2 Best Practices → 1 unified) - Removed outdated 'Current Limitations' section (A3 complete) - Removed redundant 'Using Resource Metadata' section - Better logical flow: Quick Start → Config → Client → Store → Cache → Best Practices → Advanced Content fixes based on feedback: - Consistent list() destructuring pattern throughout - Clear getItems vs getList distinction with usage guidance - Path parameter version requirements documented (A3+) - Nested resource examples use named params (not positional) - Added Resource Events callout (wpk.resource.*) - All imports extension-free - No Date.now() in cache key examples Technical fixes: - Escaped angle brackets in markdown headers for VitePress compatibility - Fixed callout block formatting - Removed duplicate section headers Results: - 137 lines shorter (420 deletions, 283 additions) - All concepts grouped logically - No duplication - Accurate implementation status - Clear API patterns Addresses documentation feedback for PR #19
Major improvements: - Consolidated duplicate sections (2 Best Practices → 1 unified) - Removed outdated 'Current Limitations' section (A3 complete) - Removed redundant 'Using Resource Metadata' section - Better logical flow: Quick Start → Config → Client → Store → Cache → Best Practices → Advanced Content fixes based on feedback: - Consistent list() destructuring pattern throughout - Clear getItems vs getList distinction with usage guidance - Path parameter version requirements documented (A3+) - Nested resource examples use named params (not positional) - Added Resource Events callout (wpk.resource.*) - All imports extension-free - No Date.now() in cache key examples Technical fixes: - Escaped angle brackets in markdown headers for VitePress compatibility - Fixed callout block formatting - Removed duplicate section headers Results: - 137 lines shorter (420 deletions, 283 additions) - All concepts grouped logically - No duplication - Accurate implementation status - Clear API patterns Addresses documentation feedback for PR #19
feat(kernel): Implement cache invalidation helper (A4)
Summary
Implements A4 (Cache Invalidation Helper) from Sprint 1, providing the
invalidate()function for cache management in Actions.Features
Primary API
invalidate(patterns, options?)- Invalidate caches by pattern matching['thing', 'list']matches all list querieswpk.cache.invalidatedevent for analytics/loggingwindow.wp(e.g., in tests)Cache Key Utilities
normalizeCacheKey()- Convert cache key arrays to deterministic stringsmatchesCacheKey()- Test if a key matches a pattern (prefix matching)findMatchingKeys()- Find all matching keys in a collectionfindMatchingKeysMultiple()- Find matches for multiple patterns with deduplicationStore Integration
@wordpress/dataregistry for invalidation dispatchgk/*storesImplementation Details
New Files
packages/kernel/src/resource/cacheKeys.ts- Cache key pattern matching utilitiespackages/kernel/src/resource/invalidate.ts- Main invalidation functionpackages/kernel/src/resource/__tests__/cacheKeys.test.ts- 22 tests (100% coverage)packages/kernel/src/resource/__tests__/invalidate.test.ts- 16 tests (all paths covered)Modified Files
packages/kernel/src/resource/defineResource.ts- Register store keys on accesspackages/kernel/src/resource/index.ts- Export new APIspackages/kernel/src/index.ts- Export from main packagedocs/api/resources.md- API documentation with examplesPattern Matching Examples
Usage in Actions
Tests
Documentation
docs/api/resources.mdwith complete API referenceCloses
#4