@@ -54,15 +54,6 @@ private function findWithCache(
5454 key: $ cacheKey ,
5555 callback: fn (): array => $ database ->find ($ collection , $ queries , $ forPermission ),
5656 hash: $ cacheHash ,
57- encode: static fn (array $ documents ): array => \array_map (
58- static fn (Document $ document ): array => $ document ->getArrayCopy (),
59- $ documents ,
60- ),
61- decode: fn (mixed $ payload ): array |false => $ database ->restoreQueryCacheDocuments (
62- collection: $ collectionDocument ,
63- payload: $ payload ,
64- forPermission: $ forPermission ,
65- ),
6657 );
6758 }
6859
@@ -227,77 +218,79 @@ function () use (&$callbackCalls): string {
227218 $ this ->assertSame (2 , $ callbackCalls );
228219 }
229220
230- public function testWithCacheEncodesSavedValueAndDecodesCachedValue (): void
221+ public function testWithCacheCachesSingleDocument (): void
231222 {
232223 $ cache = new HashMemoryCache ();
233224 $ database = $ this ->createDatabase ($ cache );
225+ $ database ->createCollection ('wafRules ' , permissions: [
226+ Permission::read (Role::any ()),
227+ Permission::create (Role::any ()),
228+ ]);
229+
230+ $ database ->createDocument ('wafRules ' , new Document ([
231+ '$id ' => 'rule-a ' ,
232+ ]));
234233
235234 $ callbackCalls = 0 ;
235+ $ collection = $ database ->getCollection ('wafRules ' );
236+ $ key = $ database ->getQueryCacheKey ($ collection ->getId (), '_39 ' );
237+ $ hash = $ database ->getQueryCacheField ($ collection , field: 'document ' );
236238
237239 $ first = $ database ->withCache (
238- key: ' key ' ,
239- callback: function () use (&$ callbackCalls ): array {
240+ key: $ key ,
241+ callback: function () use ($ database , &$ callbackCalls ): Document {
240242 $ callbackCalls ++;
241- return [ ' fresh ' ] ;
243+ return $ database -> getDocument ( ' wafRules ' , ' rule-a ' ) ;
242244 },
243- encode: static fn (array $ value ): array => ['encoded ' => $ value ],
244- decode: static fn (array $ value ): array => $ value ['encoded ' ],
245+ hash: $ hash ,
245246 );
246-
247247 $ second = $ database ->withCache (
248- key: ' key ' ,
249- callback: function () use (&$ callbackCalls ): array {
248+ key: $ key ,
249+ callback: function () use ($ database , &$ callbackCalls ): Document {
250250 $ callbackCalls ++;
251- return [ ' miss ' ] ;
251+ return $ database -> getDocument ( ' wafRules ' , ' missing ' ) ;
252252 },
253- encode: static fn (array $ value ): array => ['encoded ' => $ value ],
254- decode: static fn (array $ value ): array => $ value ['encoded ' ],
253+ hash: $ hash ,
255254 );
256255
257- $ this ->assertSame ([ ' fresh ' ] , $ first );
258- $ this ->assertSame ([ ' fresh ' ] , $ second );
256+ $ this ->assertSame (' rule-a ' , $ first-> getId () );
257+ $ this ->assertSame (' rule-a ' , $ second-> getId () );
259258 $ this ->assertSame (1 , $ callbackCalls );
260259 }
261260
262- public function testWithCacheRefreshesWhenDecodeRejectsCachedValue (): void
261+ public function testWithCacheCachesStaticQueryValues (): void
263262 {
264263 $ cache = new HashMemoryCache ();
265264 $ database = $ this ->createDatabase ($ cache );
265+ $ database ->createCollection ('wafRules ' , permissions: [
266+ Permission::read (Role::any ()),
267+ ]);
266268
267269 $ callbackCalls = 0 ;
270+ $ collection = $ database ->getCollection ('wafRules ' );
271+ $ key = $ database ->getQueryCacheKey ($ collection ->getId (), '_39 ' );
272+ $ hash = $ database ->getQueryCacheField ($ collection , field: 'count ' );
268273
269- $ database ->withCache (
270- key: 'key ' ,
271- callback: function () use (&$ callbackCalls ): array {
272- $ callbackCalls ++;
273- return ['stale ' ];
274- },
275- encode: static fn (array $ value ): array => $ value ,
276- );
277-
278- $ fresh = $ database ->withCache (
279- key: 'key ' ,
280- callback: function () use (&$ callbackCalls ): array {
274+ $ first = $ database ->withCache (
275+ key: $ key ,
276+ callback: function () use (&$ callbackCalls ): int {
281277 $ callbackCalls ++;
282- return [ ' fresh ' ] ;
278+ return 10 ;
283279 },
284- encode: static fn (array $ value ): array => $ value ,
285- decode: static fn (array $ value ): array |false => $ value === ['stale ' ] ? false : $ value ,
280+ hash: $ hash ,
286281 );
287-
288- $ cachedFresh = $ database ->withCache (
289- key: 'key ' ,
290- callback: function () use (&$ callbackCalls ): array {
282+ $ second = $ database ->withCache (
283+ key: $ key ,
284+ callback: function () use (&$ callbackCalls ): int {
291285 $ callbackCalls ++;
292- return [ ' miss ' ] ;
286+ return 20 ;
293287 },
294- encode: static fn (array $ value ): array => $ value ,
295- decode: static fn (array $ value ): array |false => $ value === ['stale ' ] ? false : $ value ,
288+ hash: $ hash ,
296289 );
297290
298- $ this ->assertSame ([ ' fresh ' ] , $ fresh );
299- $ this ->assertSame ([ ' fresh ' ] , $ cachedFresh );
300- $ this ->assertSame (2 , $ callbackCalls );
291+ $ this ->assertSame (10 , $ first );
292+ $ this ->assertSame (10 , $ second );
293+ $ this ->assertSame (1 , $ callbackCalls );
301294 }
302295
303296 public function testQueryCacheUsesCacheUntilPurged (): void
@@ -594,6 +587,8 @@ public function testQueryCacheRehydratesNestedDocumentPayloads(): void
594587 $ cache ->save (
595588 $ database ->getQueryCacheKey ($ collection ->getId (), '_39 ' ),
596589 [
590+ 'collection ' => $ collection ->getId (),
591+ 'type ' => 'documents ' ,
597592 'value ' => [
598593 [
599594 '$id ' => 'parent-a ' ,
@@ -648,7 +643,11 @@ public function testQueryCacheRefreshesInvalidPayload(): void
648643 $ collection = $ database ->getCollection ('wafRules ' );
649644 $ cache ->save (
650645 $ database ->getQueryCacheKey ($ collection ->getId (), '_39 ' ),
651- ['value ' => 'invalid ' ],
646+ [
647+ 'collection ' => $ collection ->getId (),
648+ 'type ' => 'documents ' ,
649+ 'value ' => 'invalid ' ,
650+ ],
652651 $ database ->getQueryCacheField ($ collection , $ queries ),
653652 );
654653
@@ -696,6 +695,8 @@ public function testQueryCacheRefreshesInvalidPayloadEntry(): void
696695 $ cache ->save (
697696 $ database ->getQueryCacheKey ($ collection ->getId (), '_39 ' ),
698697 [
698+ 'collection ' => $ collection ->getId (),
699+ 'type ' => 'documents ' ,
699700 'value ' => [
700701 [
701702 '$id ' => 'rule-a ' ,
0 commit comments