22
33namespace  Illuminate \Tests \Integration \Cache ;
44
5+ use  Illuminate \Foundation \Testing \LazilyRefreshDatabase ;
56use  Illuminate \Support \Carbon ;
67use  Illuminate \Support \Facades \Cache ;
8+ use  Orchestra \Testbench \Attributes \WithMigration ;
79use  Orchestra \Testbench \TestCase ;
810
11+ #[WithMigration('cache ' )]
912class  RepositoryTest extends  TestCase
1013{
14+     use  LazilyRefreshDatabase;
15+ 
1116    public  function  testStaleWhileRevalidate (): void 
1217    {
1318        Carbon::setTestNow ('2000-01-01 00:00:00 ' );
@@ -22,7 +27,7 @@ public function testStaleWhileRevalidate(): void
2227        $ this assertSame (1 , $ value
2328        $ this assertCount (0 , defer ());
2429        $ this assertSame (1 , $ cacheget ('foo ' ));
25-         $ this assertSame (946684800 , $ cacheget ('foo: created ));
30+         $ this assertSame (946684800 , $ cacheget ('illuminate:cache:flexible: created:foo  ));
2631
2732        // Cache is fresh. The value should be retrieved from the cache and used... 
2833        $ value$ cacheflexible ('foo ' , [10 , 20 ], function  () use  (&$ count
@@ -31,7 +36,7 @@ public function testStaleWhileRevalidate(): void
3136        $ this assertSame (1 , $ value
3237        $ this assertCount (0 , defer ());
3338        $ this assertSame (1 , $ cacheget ('foo ' ));
34-         $ this assertSame (946684800 , $ cacheget ('foo: created ));
39+         $ this assertSame (946684800 , $ cacheget ('illuminate:cache:flexible: created:foo  ));
3540
3641        Carbon::setTestNow (now ()->addSeconds (11 ));
3742
@@ -43,7 +48,7 @@ public function testStaleWhileRevalidate(): void
4348        $ this assertSame (1 , $ value
4449        $ this assertCount (1 , defer ());
4550        $ this assertSame (1 , $ cacheget ('foo ' ));
46-         $ this assertSame (946684800 , $ cacheget ('foo: created ));
51+         $ this assertSame (946684800 , $ cacheget ('illuminate:cache:flexible: created:foo  ));
4752
4853        // We will hit it again within the same request. This should not queue 
4954        // up an additional deferred callback as only one can be registered at 
@@ -54,14 +59,14 @@ public function testStaleWhileRevalidate(): void
5459        $ this assertSame (1 , $ value
5560        $ this assertCount (1 , defer ());
5661        $ this assertSame (1 , $ cacheget ('foo ' ));
57-         $ this assertSame (946684800 , $ cacheget ('foo: created ));
62+         $ this assertSame (946684800 , $ cacheget ('illuminate:cache:flexible: created:foo  ));
5863
5964        // We will now simulate the end of the request lifecycle by executing the 
6065        // deferred callback. This should refresh the cache. 
6166        defer ()->invoke ();
6267        $ this assertCount (0 , defer ());
6368        $ this assertSame (2 , $ cacheget ('foo ' )); // this has been updated! 
64-         $ this assertSame (946684811 , $ cacheget ('foo: created )); // this has been updated! 
69+         $ this assertSame (946684811 , $ cacheget ('illuminate:cache:flexible: created:foo  )); // this has been updated! 
6570
6671        // Now the cache is fresh again... 
6772        $ value$ cacheflexible ('foo ' , [10 , 20 ], function  () use  (&$ count
@@ -70,7 +75,7 @@ public function testStaleWhileRevalidate(): void
7075        $ this assertSame (2 , $ value
7176        $ this assertCount (0 , defer ());
7277        $ this assertSame (2 , $ cacheget ('foo ' ));
73-         $ this assertSame (946684811 , $ cacheget ('foo: created ));
78+         $ this assertSame (946684811 , $ cacheget ('illuminate:cache:flexible: created:foo  ));
7479
7580        // Let's now progress time beyond the stale TTL... 
7681        Carbon::setTestNow (now ()->addSeconds (21 ));
@@ -82,7 +87,7 @@ public function testStaleWhileRevalidate(): void
8287        $ this assertSame (3 , $ value
8388        $ this assertCount (0 , defer ());
8489        $ this assertSame (3 , $ cacheget ('foo ' ));
85-         $ this assertSame (946684832 , $ cacheget ('foo: created ));
90+         $ this assertSame (946684832 , $ cacheget ('illuminate:cache:flexible: created:foo  ));
8691
8792        // Now lets see what happens when another request, job, or command is 
8893        // also trying to refresh the same key at the same time. Will push past 
@@ -94,28 +99,28 @@ public function testStaleWhileRevalidate(): void
9499        $ this assertSame (3 , $ value
95100        $ this assertCount (1 , defer ());
96101        $ this assertSame (3 , $ cacheget ('foo ' ));
97-         $ this assertSame (946684832 , $ cacheget ('foo: created ));
102+         $ this assertSame (946684832 , $ cacheget ('illuminate:cache:flexible: created:foo  ));
98103
99104        // Now we will execute the deferred callback but we will first aquire 
100105        // our own lock. This means that the value should not be refreshed by 
101106        // deferred callback. 
102107        /** @var Lock */ 
103-         $ lock$ cachelock ('illuminate:cache:refresh :lock:foo ' );
108+         $ lock$ cachelock ('illuminate:cache:flexible :lock:foo ' );
104109
105110        $ this assertTrue ($ lockacquire ());
106111        defer ()->first ()();
107112        $ this assertSame (3 , $ value
108113        $ this assertCount (1 , defer ());
109114        $ this assertSame (3 , $ cacheget ('foo ' ));
110-         $ this assertSame (946684832 , $ cacheget ('foo: created ));
115+         $ this assertSame (946684832 , $ cacheget ('illuminate:cache:flexible: created:foo  ));
111116        $ this assertTrue ($ lockrelease ());
112117
113118        // Now we have cleared the lock we will, one last time, confirm that 
114119        // the deferred callack does refresh the value when the lock is not active. 
115120        defer ()->invoke ();
116121        $ this assertCount (0 , defer ());
117122        $ this assertSame (4 , $ cacheget ('foo ' ));
118-         $ this assertSame (946684843 , $ cacheget ('foo: created ));
123+         $ this assertSame (946684843 , $ cacheget ('illuminate:cache:flexible: created:foo  ));
119124
120125        // The last thing is to check that we don't refresh the cache in the 
121126        // deferred callback if another thread has already done the work for us. 
@@ -127,13 +132,13 @@ public function testStaleWhileRevalidate(): void
127132        $ this assertSame (4 , $ value
128133        $ this assertCount (1 , defer ());
129134        $ this assertSame (4 , $ cacheget ('foo ' ));
130-         $ this assertSame (946684843 , $ cacheget ('foo: created ));
135+         $ this assertSame (946684843 , $ cacheget ('illuminate:cache:flexible: created:foo  ));
131136
132137        // There is now a deferred callback ready to refresh the cache. We will 
133138        // simulate another thread updating the value. 
134139        $ cacheputMany ([
135140            'foo '  => 99 ,
136-             'foo: created  => 946684863 ,
141+             'illuminate:cache:flexible: created:foo   => 946684863 ,
137142        ]);
138143
139144        // then we will run the refresh callback 
@@ -144,6 +149,90 @@ public function testStaleWhileRevalidate(): void
144149        $ this assertSame (99 , $ value
145150        $ this assertCount (0 , defer ());
146151        $ this assertSame (99 , $ cacheget ('foo ' ));
147-         $ this assertSame (946684863 , $ cacheget ('foo:created ' ));
152+         $ this assertSame (946684863 , $ cacheget ('illuminate:cache:flexible:created:foo ' ));
153+     }
154+ 
155+     public  function  testItHandlesStrayTtlKeyAfterMainKeyIsForgotten ()
156+     {
157+         $ cachedriver ('array ' );
158+         $ count0 ;
159+ 
160+         $ value$ cacheflexible ('count ' , [5 , 10 ], function  () use  (&$ count
161+             $ count1 ;
162+ 
163+             return  $ count
164+         });
165+ 
166+         $ this assertSame (1 , $ value
167+         $ this assertSame (1 , $ count
168+ 
169+         $ cacheforget ('count ' );
170+ 
171+         $ value$ cacheflexible ('count ' , [5 , 10 ], function  () use  (&$ count
172+             $ count2 ;
173+ 
174+             return  $ count
175+         });
176+         $ this assertSame (2 , $ value
177+         $ this assertSame (2 , $ count
178+     }
179+ 
180+     public  function  testItImplicitlyClearsTtlKeysFromDatabaseCache ()
181+     {
182+         $ this freezeTime ();
183+         $ cachedriver ('database ' );
184+ 
185+         $ cacheflexible ('count ' , [5 , 10 ], fn  () => 1 );
186+ 
187+         $ this assertTrue ($ cachehas ('count ' ));
188+         $ this assertTrue ($ cachehas ('illuminate:cache:flexible:created:count ' ));
189+ 
190+         $ cacheforget ('count ' );
191+ 
192+         $ this assertEmpty ($ cachegetConnection ()->table ('cache ' )->get ());
193+         $ this assertTrue ($ cachemissing ('count ' ));
194+         $ this assertTrue ($ cachemissing ('illuminate:cache:flexible:created:count ' ));
195+ 
196+         $ cacheflexible ('count ' , [5 , 10 ], fn  () => 1 );
197+ 
198+         $ this assertTrue ($ cachehas ('count ' ));
199+         $ this assertTrue ($ cachehas ('illuminate:cache:flexible:created:count ' ));
200+ 
201+         $ this travel (20 )->seconds ();
202+         $ cacheforgetIfExpired ('count ' );
203+ 
204+         $ this assertEmpty ($ cachegetConnection ()->table ('cache ' )->get ());
205+         $ this assertTrue ($ cachemissing ('count ' ));
206+         $ this assertTrue ($ cachemissing ('illuminate:cache:flexible:created:count ' ));
207+     }
208+ 
209+     public  function  testItImplicitlyClearsTtlKeysFromFileDriver ()
210+     {
211+         $ this freezeTime ();
212+         $ cachedriver ('file ' );
213+ 
214+         $ cacheflexible ('count ' , [5 , 10 ], fn  () => 1 );
215+ 
216+         $ this assertTrue ($ cachehas ('count ' ));
217+         $ this assertTrue ($ cachehas ('illuminate:cache:flexible:created:count ' ));
218+ 
219+         $ cacheforget ('count ' );
220+ 
221+         $ this assertFalse ($ cachegetFilesystem ()->exists ($ cachepath ('count ' )));
222+         $ this assertFalse ($ cachegetFilesystem ()->exists ($ cachepath ('illuminate:cache:flexible:created:count ' )));
223+         $ this assertTrue ($ cachemissing ('count ' ));
224+         $ this assertTrue ($ cachemissing ('illuminate:cache:flexible:created:count ' ));
225+ 
226+         $ cacheflexible ('count ' , [5 , 10 ], fn  () => 1 );
227+ 
228+         $ this assertTrue ($ cachehas ('count ' ));
229+         $ this assertTrue ($ cachehas ('illuminate:cache:flexible:created:count ' ));
230+ 
231+         $ this travel (20 )->seconds ();
232+ 
233+         $ this assertTrue ($ cachemissing ('count ' ));
234+         $ this assertFalse ($ cachegetFilesystem ()->exists ($ cachepath ('count ' )));
235+         $ this assertFalse ($ cachegetFilesystem ()->exists ($ cachepath ('illuminate:cache:flexible:created:count ' )));
236+         $ this assertTrue ($ cachemissing ('illuminate:cache:flexible:created:count ' ));
148237    }
149238}
0 commit comments