@@ -14,9 +14,10 @@ This implementation adds efficient caching to the `find` method in the Database
1414
1515### 2. Version Tracking for O(1) Invalidation
1616- ** Purpose** : Enable aggressive cache invalidation without expensive cache scanning
17- - ** Implementation** : Each collection has a version number that increments on any change
18- - ** Storage** : Version numbers are cached persistently with 1-year TTL
19- - ** Benefits** : O(1) invalidation time complexity
17+ - ** Implementation** : Each collection has a version string that changes on any modification
18+ - ** Format** : ` {microtime}-{random_hex} ` for sub-second precision and uniqueness
19+ - ** Storage** : Version strings are cached persistently with 1-year TTL
20+ - ** Benefits** : O(1) invalidation time complexity with sub-second granularity
2021
2122### 3. Find Method Caching
2223- ** Cache Key Generation** : Uses xxh3 hash of all query parameters plus collection version
@@ -26,7 +27,8 @@ This implementation adds efficient caching to the `find` method in the Database
2627
2728### 4. Aggressive Invalidation
2829- ** Trigger Points** : Any document create, update, or delete operation
29- - ** Method** : Increments collection version, making all cached queries invalid
30+ - ** Method** : Changes collection version, making all cached queries invalid instantly
31+ - ** Granularity** : Sub-second precision prevents cache inconsistencies during rapid operations
3032- ** Priority** : Correctness over performance (as requested)
3133- ** Implementation** : Updated ` purgeCachedDocument() ` and ` purgeCachedCollection() ` methods
3234
@@ -49,8 +51,8 @@ protected array $collectionVersions = [];
4951### New Methods
50521 . ` generateCacheHash(string $data): string ` - xxh3 hash implementation using PHP's built-in hash function
51532 . ` getFindCacheKey(...) ` - Generate cache keys for find queries
52- 3 . ` getCollectionVersion(string $collectionId): int ` - Get/initialize collection version
53- 4 . ` incrementCollectionVersion(string $collectionId): void ` - Increment version for invalidation
54+ 3 . ` getCollectionVersion(string $collectionId): string ` - Get/initialize collection version
55+ 4 . ` incrementCollectionVersion(string $collectionId): void ` - Change version for invalidation
54565 . ` getCollectionVersionKey(string $collectionId): string ` - Generate version cache key
5557
5658### Modified Methods
@@ -65,7 +67,7 @@ protected array $collectionVersions = [];
6567
6668Example:
6769```
68- default-cache-:::find:users:7a8b9c2d1e3f4567:v1691234567
70+ default-cache-:::find:users:7a8b9c2d1e3f4567:v1691234567.123456-a1b2c3d4
6971```
7072
7173## Performance Characteristics
@@ -81,8 +83,9 @@ default-cache-:::find:users:7a8b9c2d1e3f4567:v1691234567
8183- ** No degradation** : Database query performance unchanged
8284
8385### Invalidation Performance
84- - ** Time Complexity** : O(1) for version increment
86+ - ** Time Complexity** : O(1) for version change
8587- ** Space Complexity** : O(1) additional storage per collection
88+ - ** Granularity** : Sub-second precision with microsecond accuracy
8689- ** Immediate** : All cached queries become invalid instantly
8790
8891## Usage Example
@@ -104,6 +107,7 @@ $results2 = $database->find('users', [
104107$database->updateDocument('users', 'user_id', $updatedDoc);
105108
106109// Next call - cache miss (version changed), queries database
110+ // Works correctly even for rapid successive operations within the same second
107111$results3 = $database->find('users', [
108112 Query::equal('status', 'active'),
109113 Query::limit(25)
0 commit comments