@umituz/react-native-storage 2.6.22 → 2.6.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,122 @@
1
+ # Pattern Matcher
2
+
3
+ Pattern-based cache key matching for bulk operations.
4
+
5
+ ## Overview
6
+
7
+ PatternMatcher provides wildcard-based cache key matching for efficient bulk invalidation and querying operations. Located at `src/cache/domain/PatternMatcher.ts`.
8
+
9
+ ## Strategies
10
+
11
+ ### Pattern Syntax
12
+ - Use `*` wildcard to match any characters (including empty)
13
+ - Use `:` as default separator for structured keys
14
+ - Support multiple wildcards in single pattern
15
+ - Enable regex-compatible patterns for advanced matching
16
+
17
+ ### Performance Optimization
18
+ - Pre-compile frequently used patterns
19
+ - Cache regex objects for repeated use
20
+ - Use pattern indexing for large key sets
21
+ - Implement batch pattern matching for efficiency
22
+
23
+ ### Bulk Operations
24
+ - Use patterns for invalidating multiple keys
25
+ - Filter keys by pattern for querying
26
+ - Extract parameters from matched keys
27
+ - Support hierarchical invalidation
28
+
29
+ ### Pattern Organization
30
+ - Define pattern constants for reuse
31
+ - Document pattern purpose and structure
32
+ - Use consistent naming conventions
33
+ - Group related patterns together
34
+
35
+ ## Restrictions
36
+
37
+ ### Pattern Usage
38
+ - DO NOT use overly broad patterns (e.g., `*`)
39
+ - DO NOT mix different separators in same pattern
40
+ - DO NOT create ambiguous patterns
41
+ - DO NOT use patterns without documenting purpose
42
+
43
+ ### Performance
44
+ - DO NOT compile same pattern repeatedly
45
+ - DO NOT iterate through all keys for pattern matching
46
+ - DO NOT use patterns for single-key operations
47
+ - DO NOT create complex regex patterns unnecessarily
48
+
49
+ ### Pattern Design
50
+ - DO NOT use special regex characters without escaping
51
+ - DO NOT assume pattern separator is always `:`
52
+ - DO NOT create patterns that match unintended keys
53
+ - DO NOT use patterns for exact key matching
54
+
55
+ ### Testing
56
+ - DO NOT skip testing edge cases (empty keys, special characters)
57
+ - DO NOT forget to test non-matching keys
58
+ - DO NOT assume pattern behavior without tests
59
+ - DO NOT use production data in pattern tests
60
+
61
+ ## Rules
62
+
63
+ ### Pattern Syntax
64
+ - MUST use `*` for wildcard matching
65
+ - MUST use `:` as default separator
66
+ - MUST support multiple wildcards per pattern
67
+ - MUST escape special regex characters in patterns
68
+
69
+ ### Method Implementation
70
+ - MUST provide `convertPatternToRegex(pattern: string): RegExp`
71
+ - MUST provide `matchPattern(key: string, pattern: string): boolean`
72
+ - MUST provide `filterKeys(keys: string[], pattern: string): string[]`
73
+ - MUST handle empty pattern strings gracefully
74
+
75
+ ### Pattern Conversion
76
+ - MUST escape special regex characters except `*`
77
+ - MUST replace `*` with `.*` regex equivalent
78
+ - MUST anchor pattern with `^` and `$`
79
+ - MUST return compiled RegExp object
80
+
81
+ ### Matching Logic
82
+ - MUST use RegExp.test() for matching
83
+ - MUST handle empty key strings
84
+ - MUST return boolean for match results
85
+ - MUST be case-sensitive by default
86
+
87
+ ### Filtering Operations
88
+ - MUST return array of matching keys
89
+ - MUST preserve input array order
90
+ - MUST handle empty input arrays
91
+ - MUST not modify input array
92
+
93
+ ### Performance Optimization
94
+ - MUST cache compiled regex objects
95
+ - MUST provide method to clear pattern cache
96
+ - MUST use efficient iteration for large key sets
97
+ - MUST avoid redundant pattern compilation
98
+
99
+ ### Error Handling
100
+ - MUST throw error for invalid patterns
101
+ - MUST handle null/undefined inputs gracefully
102
+ - MUST log pattern compilation errors
103
+ - MUST provide descriptive error messages
104
+
105
+ ### Pattern Documentation
106
+ - MUST document all pattern constants
107
+ - MUST specify pattern structure and purpose
108
+ - MUST provide examples of matching keys
109
+ - MUST warn about performance implications
110
+
111
+ ### Testing Requirements
112
+ - MUST test wildcard matching
113
+ - MUST test multiple wildcards
114
+ - MUST test non-matching keys
115
+ - MUST test edge cases (empty strings, special characters)
116
+ - MUST test pattern extraction
117
+
118
+ ### Integration with Cache
119
+ - MUST work with Cache.invalidatePattern()
120
+ - MUST support bulk operations
121
+ - MUST handle non-existent keys gracefully
122
+ - MUST return count of invalidated keys
@@ -0,0 +1,118 @@
1
+ # Cache Domain
2
+
3
+ Core business logic and entities for the cache system.
4
+
5
+ ## Overview
6
+
7
+ Domain layer contains cache entities, managers, error handling, and pattern matching. Located at `src/cache/domain/`.
8
+
9
+ ## Strategies
10
+
11
+ ### Domain Design
12
+ - Implement core cache business logic
13
+ - Define cache entities and value objects
14
+ - Provide error handling and recovery
15
+ - Support pattern-based operations
16
+
17
+ ### Cache Management
18
+ - Use singleton pattern for CacheManager
19
+ - Support multiple named cache instances
20
+ - Provide lifecycle management (create, delete, clear)
21
+ - Track statistics across all caches
22
+
23
+ ### Error Handling
24
+ - Centralize error handling logic
25
+ - Categorize errors by type
26
+ - Provide recovery strategies
27
+ - Log errors for debugging
28
+
29
+ ### Pattern Matching
30
+ - Support wildcard patterns for bulk operations
31
+ - Use consistent separator (`:`)
32
+ - Enable efficient key filtering
33
+ - Support invalidation by pattern
34
+
35
+ ## Restrictions
36
+
37
+ ### Domain Logic
38
+ - DO NOT mix infrastructure concerns in domain
39
+ - DO NOT depend on React or UI libraries
40
+ - DO NOT include I/O operations
41
+ - DO NOT use AsyncStorage directly
42
+
43
+ ### Error Handling
44
+ - DO NOT throw exceptions from domain logic
45
+ - DO NOT ignore error conditions
46
+ - DO NOT lose error context
47
+ - DO NOT create generic error types
48
+
49
+ ### Pattern Matching
50
+ - DO NOT use regex patterns directly (use PatternMatcher)
51
+ - DO NOT mix different separators
52
+ - DO NOT create ambiguous patterns
53
+ - DO NOT use overly broad patterns
54
+
55
+ ### Cache Management
56
+ - DO NOT create unlimited cache instances
57
+ - DO NOT allow duplicate cache names
58
+ - DO NOT leak memory through unreferenced caches
59
+ - DO NOT ignore cache statistics
60
+
61
+ ## Rules
62
+
63
+ ### Cache Entity
64
+ - MUST implement generic Cache<T> class
65
+ - MUST provide CRUD operations (get, set, has, delete)
66
+ - MUST provide clear() operation
67
+ - MUST provide invalidatePattern() operation
68
+ - MUST provide getStats() operation
69
+
70
+ ### CacheManager Singleton
71
+ - MUST provide getCache(name, config?) method
72
+ - MUST return same instance for same name
73
+ - MUST provide deleteCache(name) method
74
+ - MUST provide clearAll() method
75
+ - MUST provide getCacheNames() method
76
+
77
+ ### Error Handling
78
+ - MUST use CacheError base class
79
+ - MUST define specific error types
80
+ - MUST provide error codes
81
+ - MUST preserve error context (key, operation)
82
+
83
+ ### Pattern Matcher
84
+ - MUST provide convertPatternToRegex() method
85
+ - MUST provide matchPattern() method
86
+ - MUST provide filterKeys() method
87
+ - MUST use `*` for wildcard
88
+ - MUST use `:` as default separator
89
+
90
+ ### Statistics Tracking
91
+ - MUST track hits, misses, evictions, expirations
92
+ - MUST calculate hit rate
93
+ - MUST provide getStats() method
94
+ - MUST reset statistics in tests
95
+
96
+ ### Type Safety
97
+ - MUST use generic type parameter <T>
98
+ - MUST enforce type consistency
99
+ - MUST provide type inference
100
+ - MUST avoid `any` types
101
+
102
+ ### Testing Support
103
+ - MUST provide clearAll() for test cleanup
104
+ - MUST reset statistics between tests
105
+ - MUST support mock implementations
106
+ - MUST not have hidden dependencies
107
+
108
+ ### Documentation Rules
109
+ - MUST document all public methods
110
+ - MUST specify parameter types
111
+ - MUST specify return types
112
+ - MUST provide usage examples in comments
113
+
114
+ ### Performance Rules
115
+ - MUST use efficient data structures (Map)
116
+ - MUST provide O(1) access for get/set
117
+ - MUST provide O(n) worst case for pattern operations
118
+ - MUST minimize memory overhead
@@ -0,0 +1,117 @@
1
+ # Cache Eviction Strategies
2
+
3
+ Algorithms for determining which cache entries to evict when cache is full.
4
+
5
+ ## Overview
6
+
7
+ Eviction strategies determine which cache entries to remove when the cache reaches its maximum size. Located at `src/cache/domain/strategies/`.
8
+
9
+ ## Strategies
10
+
11
+ ### Strategy Selection
12
+ - Use LRU (Least Recently Used) for most use cases - best general performance
13
+ - Use LFU (Least Frequently Used) for popularity-based caching - keeps frequently accessed items
14
+ - Use FIFO (First In First Out) for simple queue-based caching - lowest overhead
15
+ - Use TTL (Time To Live) for time-sensitive data - required for expiration
16
+
17
+ ### Performance Considerations
18
+ - LRU provides O(1) eviction time with proper data structures
19
+ - LFU provides O(n) eviction time but better for read-heavy workloads
20
+ - FIFO provides O(1) eviction time with minimal overhead
21
+ - TTL requires periodic cleanup but essential for freshness
22
+
23
+ ### Access Pattern Analysis
24
+ - Consider data access patterns when choosing strategy
25
+ - Use LRU for temporal locality (recently accessed likely to be accessed again)
26
+ - Use LFU for frequency-based patterns (popular items should stay)
27
+ - Use FIFO for sequential access patterns
28
+ - Use TTL when data freshness is critical
29
+
30
+ ### Implementation Strategy
31
+ - Implement EvictionStrategy interface for custom strategies
32
+ - Track necessary metadata (access count, last access, timestamp)
33
+ - Provide findKeyToEvict method for selection
34
+ - Update metadata on cache operations
35
+
36
+ ## Restrictions
37
+
38
+ ### Strategy Usage
39
+ - DO NOT use LFU for write-heavy workloads (expensive)
40
+ - DO NOT use FIFO when access patterns matter
41
+ - DO NOT use TTL when data staleness is acceptable
42
+ - DO NOT change strategies after cache creation
43
+
44
+ ### Performance
45
+ - DO NOT implement O(n²) eviction algorithms
46
+ - DO NOT scan entire cache for eviction on every operation
47
+ - DO NOT track unnecessary metadata for chosen strategy
48
+ - DO NOT use complex algorithms for simple use cases
49
+
50
+ ### Custom Strategies
51
+ - DO NOT implement custom strategies without thorough testing
52
+ - DO NOT ignore edge cases (empty cache, single entry)
53
+ - DO NOT create strategies without clear performance benefits
54
+ - DO NOT mix multiple eviction strategies in same cache
55
+
56
+ ## Rules
57
+
58
+ ### EvictionStrategy Interface
59
+ - MUST implement findKeyToEvict(store: Map): string | undefined
60
+ - MUST return undefined when store is empty
61
+ - MUST return single key for eviction
62
+ - MUST handle all cache states correctly
63
+
64
+ ### LRU Strategy
65
+ - MUST track lastAccess timestamp for each entry
66
+ - MUST update lastAccess on every get operation
67
+ - MUST select entry with oldest lastAccess time
68
+ - MUST provide O(1) or O(log n) time complexity
69
+
70
+ ### LFU Strategy
71
+ - MUST track accessCount for each entry
72
+ - MUST increment accessCount on every get operation
73
+ - MUST select entry with lowest accessCount
74
+ - MUST handle ties consistently (use LRU as tiebreaker)
75
+
76
+ ### FIFO Strategy
77
+ - MUST track insertion order for each entry
78
+ - MUST select oldest entry (first inserted)
79
+ - MUST NOT consider access patterns
80
+ - MUST provide O(1) time complexity
81
+
82
+ ### TTL Strategy
83
+ - MUST check timestamp + ttl against current time
84
+ - MUST select expired entries first
85
+ - MUST fall back to secondary strategy if no expired entries
86
+ - MUST handle zero TTL entries (immediate eviction)
87
+
88
+ ### Metadata Updates
89
+ - MUST update eviction metadata on every cache operation
90
+ - MUST update metadata on cache set operations
91
+ - MUST update metadata on cache get operations
92
+ - MUST reset metadata appropriately on cache updates
93
+
94
+ ### Edge Cases
95
+ - MUST handle empty cache (return undefined)
96
+ - MUST handle single entry cache
97
+ - MUST handle all entries with same metadata
98
+ - MUST handle concurrent access safely
99
+
100
+ ### Strategy Selection
101
+ - MUST use LRU as default strategy
102
+ - MUST allow strategy override in configuration
103
+ - MUST validate strategy choice
104
+ - MUST document strategy behavior
105
+
106
+ ### Testing Requirements
107
+ - MUST test eviction with full cache
108
+ - MUST test eviction with empty cache
109
+ - MUST test metadata tracking accuracy
110
+ - MUST test edge cases
111
+ - MUST measure time complexity
112
+
113
+ ### Performance Rules
114
+ - MUST not exceed O(n) for eviction selection
115
+ - MUST not allocate memory during eviction
116
+ - MUST not use blocking operations
117
+ - MUST minimize CPU overhead
@@ -0,0 +1,107 @@
1
+ # Cache Domain Types
2
+
3
+ TypeScript interfaces and types for cache system.
4
+
5
+ ## Overview
6
+
7
+ TypeScript type definitions for cache entries, configuration, statistics, and eviction strategies. Located at `src/cache/domain/types/`.
8
+
9
+ ## Strategies
10
+
11
+ ### Cache Entry Definition
12
+ - Define CacheEntry with value, timestamp, TTL, and access tracking
13
+ - Include metadata for eviction strategies
14
+ - Support generic type parameter for cached values
15
+ - Track access patterns for statistics
16
+
17
+ ### Cache Configuration
18
+ - Provide optional configuration parameters
19
+ - Set sensible defaults (maxSize: 100, TTL: 5 minutes)
20
+ - Support lifecycle callbacks (onEvict, onExpire)
21
+ - Allow strategy selection
22
+
23
+ ### Statistics Tracking
24
+ - Track hits, misses, evictions, expirations
25
+ - Calculate hit rate automatically
26
+ - Provide snapshot of cache state
27
+ - Support performance monitoring
28
+
29
+ ### Type Safety
30
+ - Use generic type parameter for cached values
31
+ - Enforce type consistency across operations
32
+ - Provide type inference for cache creation
33
+ - Support nested type structures
34
+
35
+ ## Restrictions
36
+
37
+ ### Type Definitions
38
+ - DO NOT use optional properties for required fields
39
+ - DO NOT mix different data types in same cache without generic
40
+ - DO NOT create circular type dependencies
41
+ - DO NOT omit timestamp or TTL from entries
42
+
43
+ ### Configuration
44
+ - DO NOT allow unlimited cache sizes (must have maxSize)
45
+ - DO NOT set zero or negative TTL as default
46
+ - DO NOT make callbacks required (use optional)
47
+ - DO NOT ignore validation in configuration
48
+
49
+ ### Statistics
50
+ - DO NOT calculate statistics on every access (expensive)
51
+ - DO NOT track unnecessary metrics
52
+ - DO NOT use floating-point for hit rate (use decimal)
53
+ - DO NOT expose internal tracking fields
54
+
55
+ ## Rules
56
+
57
+ ### CacheEntry Interface
58
+ - MUST include value field of generic type T
59
+ - MUST include timestamp (creation time in milliseconds)
60
+ - MUST include ttl (time to live in milliseconds)
61
+ - MUST include accessCount (for LFU strategy)
62
+ - MUST include lastAccess (for LRU strategy)
63
+
64
+ ### CacheConfig Interface
65
+ - MUST provide maxSize with default value of 100
66
+ - MUST provide defaultTTL with default value of 300000 (5 minutes)
67
+ - MUST provide optional onEvict callback
68
+ - MUST provide optional onExpire callback
69
+ - MUST validate configuration values
70
+
71
+ ### CacheStats Interface
72
+ - MUST include size (current entry count)
73
+ - MUST include hits (cache hit count)
74
+ - MUST include misses (cache miss count)
75
+ - MUST include evictions (evicted entry count)
76
+ - MUST include expirations (expired entry count)
77
+ - MUST include hitRate (calculated as hits / (hits + misses))
78
+
79
+ ### Eviction Strategy Type
80
+ - MUST be union of literal types ('lru' | 'lfu' | 'fifo' | 'ttl')
81
+ - MUST support custom strategies
82
+ - MUST be case-sensitive
83
+ - MUST be documented
84
+
85
+ ### Generic Type Parameters
86
+ - MUST use T for cached value type
87
+ - MUST provide type inference
88
+ - MUST support nested types
89
+ - MUST enforce type safety
90
+
91
+ ### Type Guards
92
+ - MUST provide isCacheEntry guard
93
+ - MUST validate all required fields
94
+ - MUST check types of all fields
95
+ - MUST handle edge cases
96
+
97
+ ### Statistics Calculation
98
+ - MUST calculate hitRate as decimal (0-1)
99
+ - MUST update statistics on every operation
100
+ - MUST provide accurate counts
101
+ - MUST not expose internal calculation methods
102
+
103
+ ### Export Rules
104
+ - MUST export all public types
105
+ - MUST use `type` keyword for type-only exports
106
+ - MUST maintain backward compatibility
107
+ - MUST document type changes
@@ -0,0 +1,126 @@
1
+ # Cache Infrastructure
2
+
3
+ Implementation details and concrete implementations of cache domain logic.
4
+
5
+ ## Overview
6
+
7
+ Infrastructure layer for cache system, providing TTLCache implementation with actual storage and eviction logic. Located at `src/cache/infrastructure/`.
8
+
9
+ ## Strategies
10
+
11
+ ### Cache Implementation
12
+ - Use Map data structure for O(1) operations
13
+ - Implement TTL-based expiration checking
14
+ - Support eviction strategies from domain layer
15
+ - Provide thread-safe operations
16
+
17
+ ### Storage Management
18
+ - Track cache size limits
19
+ - Implement automatic eviction when full
20
+ - Clean up expired entries periodically
21
+ - Support manual cache clearing
22
+
23
+ ### Performance Optimization
24
+ - Minimize memory overhead per entry
25
+ - Use efficient data structures
26
+ - Implement lazy expiration checking
27
+ - Cache frequently accessed metadata
28
+
29
+ ### Integration with Domain
30
+ - Implement interfaces defined in domain layer
31
+ - Use domain types for type safety
32
+ - Follow domain-level strategies and rules
33
+ - Export clean API to presentation layer
34
+
35
+ ## Restrictions
36
+
37
+ ### Implementation
38
+ - DO NOT expose internal storage directly
39
+ - DO NOT bypass eviction strategies
40
+ - DO NOT allow cache size to exceed limit
41
+ - DO NOT store non-serializable data
42
+
43
+ ### Performance
44
+ - DO NOT perform expensive operations on every access
45
+ - DO NOT use blocking operations
46
+ - DO NOT allocate memory unnecessarily
47
+ - DO NOT iterate entire cache for single operations
48
+
49
+ ### Expiration
50
+ - DO NOT ignore expired entries on access
51
+ - DO NOT auto-clean without explicit trigger
52
+ - DO NOT use inconsistent time sources
53
+ - DO NOT rely solely on TTL for memory management
54
+
55
+ ## Rules
56
+
57
+ ### TTLCache Implementation
58
+ - MUST implement Cache interface from domain
59
+ - MUST enforce maxSize limit
60
+ - MUST check expiration on get operations
61
+ - MUST trigger eviction when full
62
+ - MUST provide accurate statistics
63
+
64
+ ### Entry Storage
65
+ - MUST use Map<string, CacheEntry<T>> for storage
66
+ - MUST include all required metadata
67
+ - MUST update timestamps on access
68
+ - MUST validate entry structure
69
+
70
+ ### Expiration Handling
71
+ - MUST check (timestamp + ttl) < Date.now()
72
+ - MUST return undefined for expired entries
73
+ - MUST remove expired entries from storage
74
+ - MUST track expiration statistics
75
+
76
+ ### Eviction Execution
77
+ - MUST call strategy.findKeyToEvict() when full
78
+ - MUST remove selected entry from storage
79
+ - MUST trigger onEvict callback if provided
80
+ - MUST update eviction statistics
81
+
82
+ ### Statistics Tracking
83
+ - MUST increment hits on successful get
84
+ - MUST increment misses on failed get
85
+ - MUST increment evictions on eviction
86
+ - MUST increment expirations on expiration
87
+ - MUST calculate hitRate correctly
88
+
89
+ ### Public Methods
90
+ - MUST implement set(key, value, ttl?)
91
+ - MUST implement get(key) -> T | undefined
92
+ - MUST implement has(key) -> boolean
93
+ - MUST implement delete(key) -> boolean
94
+ - MUST implement clear()
95
+ - MUST implement getStats()
96
+
97
+ ### Configuration Validation
98
+ - MUST validate maxSize > 0
99
+ - MUST validate defaultTTL > 0
100
+ - MUST provide default values
101
+ - MUST reject invalid configuration
102
+
103
+ ### Lifecycle Callbacks
104
+ - MUST call onEvict(entry) when entry evicted
105
+ - MUST call onExpire(entry) when entry expired
106
+ - MUST pass key and entry to callbacks
107
+ - MUST handle callback errors gracefully
108
+
109
+ ### Type Safety
110
+ - MUST use generic type parameter T
111
+ - MUST enforce type consistency
112
+ - MUST provide type inference
113
+ - MUST validate input types where possible
114
+
115
+ ### Thread Safety
116
+ - MUST handle concurrent access appropriately
117
+ - MUST not corrupt cache state
118
+ - MUST provide atomic operations
119
+ - MUST handle race conditions
120
+
121
+ ### Testing Requirements
122
+ - MUST test all eviction strategies
123
+ - MUST test expiration logic
124
+ - MUST test statistics accuracy
125
+ - MUST test edge cases (empty, full, expired)
126
+ - MUST measure performance
@@ -0,0 +1,123 @@
1
+ # Cache Presentation
2
+
3
+ React hooks and UI integration for cache system.
4
+
5
+ ## Overview
6
+
7
+ Presentation layer provides React hooks and components for integrating cache functionality with React applications. Located at `src/cache/presentation/`.
8
+
9
+ ## Strategies
10
+
11
+ ### Hook Design
12
+ - Create hooks for common caching patterns
13
+ - Provide simple, composable APIs
14
+ - Support React Suspense where applicable
15
+ - Handle loading and error states
16
+
17
+ ### React Integration
18
+ - Follow React rules of hooks
19
+ - Support dependency arrays correctly
20
+ - Clean up on component unmount
21
+ - Avoid unnecessary re-renders
22
+
23
+ ### State Management
24
+ - Expose cache state reactively
25
+ - Provide update mechanisms
26
+ - Support invalidation and refresh
27
+ - Handle stale data appropriately
28
+
29
+ ### Performance
30
+ - Use memoization to prevent redundant operations
31
+ - Implement efficient re-render strategies
32
+ - Debounce rapid cache operations
33
+ - Minimize hook overhead
34
+
35
+ ## Restrictions
36
+
37
+ ### Hook Usage
38
+ - DO NOT call hooks outside React components
39
+ - DO NOT call hooks conditionally
40
+ - DO NOT call hooks in loops
41
+ - DO NOT create new cache instances on every render
42
+
43
+ ### State Management
44
+ - DO NOT cause infinite re-render loops
45
+ - DO NOT mutate state directly
46
+ - DO NOT ignore loading states
47
+ - DO NOT swallow errors silently
48
+
49
+ ### Performance
50
+ - DO NOT recreate functions on every render
51
+ - DO NOT subscribe to entire cache when slice needed
52
+ - DO NOT perform expensive operations in render
53
+ - DO NOT cache large computed values in hook state
54
+
55
+ ### Cleanup
56
+ - DO NOT leak subscriptions
57
+ - DO NOT forget cleanup on unmount
58
+ - DO NOT create memory leaks
59
+ - DO NOT leave timers running
60
+
61
+ ## Rules
62
+
63
+ ### Hook Implementation
64
+ - MUST follow React rules of hooks
65
+ - MUST use `use` prefix for hook names
66
+ - MUST provide TypeScript types
67
+ - MUST handle errors gracefully
68
+
69
+ ### useCache Hook
70
+ - MUST accept cache name parameter
71
+ - MUST return cache interface methods
72
+ - MUST provide consistent API across renders
73
+ - MUST handle non-existent cache gracefully
74
+
75
+ ### useCachedValue Hook
76
+ - MUST accept key and fetcher function
77
+ - MUST accept optional TTL parameter
78
+ - MUST return value, setter, and invalidate function
79
+ - MUST handle loading state
80
+ - MUST handle error state
81
+
82
+ ### State Updates
83
+ - MUST trigger re-renders on cache changes
84
+ - MUST use React state for reactive values
85
+ - MUST memoize callbacks to prevent re-renders
86
+ - MUST update state atomically
87
+
88
+ ### Cleanup Behavior
89
+ - MUST clean up subscriptions on unmount
90
+ - MUST cancel pending operations
91
+ - MUST clear timers
92
+ - MUST not leak memory
93
+
94
+ ### Error Handling
95
+ - MUST expose error state to caller
96
+ - MUST allow error recovery
97
+ - MUST log errors in development
98
+ - MUST provide error boundaries for usage
99
+
100
+ ### TypeScript Types
101
+ - MUST provide generic type parameters
102
+ - MUST infer types from cache
103
+ - MUST enforce type safety
104
+ - MUST document complex types
105
+
106
+ ### Performance Rules
107
+ - MUST use useCallback for stable function references
108
+ - MUST use useMemo for expensive computations
109
+ - MUST provide stable references across renders
110
+ - MUST minimize re-render frequency
111
+
112
+ ### Testing Requirements
113
+ - MUST test with @testing-library/react-hooks
114
+ - MUST test cleanup behavior
115
+ - MUST test error scenarios
116
+ - MUST test type safety
117
+ - MUST test re-render behavior
118
+
119
+ ### Export Rules
120
+ - MUST export hooks from index file
121
+ - MUST provide consistent naming
122
+ - MUST document hook contracts
123
+ - MUST maintain backward compatibility