2
2
3
3
namespace Illuminate \Tests \Integration \Cache ;
4
4
5
+ use Illuminate \Foundation \Testing \LazilyRefreshDatabase ;
5
6
use Illuminate \Support \Carbon ;
6
7
use Illuminate \Support \Facades \Cache ;
8
+ use Orchestra \Testbench \Attributes \WithMigration ;
7
9
use Orchestra \Testbench \TestCase ;
8
10
11
+ #[WithMigration('cache ' )]
9
12
class RepositoryTest extends TestCase
10
13
{
14
+ use LazilyRefreshDatabase;
15
+
11
16
public function testStaleWhileRevalidate (): void
12
17
{
13
18
Carbon::setTestNow ('2000-01-01 00:00:00 ' );
@@ -22,7 +27,7 @@ public function testStaleWhileRevalidate(): void
22
27
$ this ->assertSame (1 , $ value );
23
28
$ this ->assertCount (0 , defer ());
24
29
$ this ->assertSame (1 , $ cache ->get ('foo ' ));
25
- $ this ->assertSame (946684800 , $ cache ->get ('foo: created ' ));
30
+ $ this ->assertSame (946684800 , $ cache ->get ('illuminate:cache:flexible: created:foo ' ));
26
31
27
32
// Cache is fresh. The value should be retrieved from the cache and used...
28
33
$ value = $ cache ->flexible ('foo ' , [10 , 20 ], function () use (&$ count ) {
@@ -31,7 +36,7 @@ public function testStaleWhileRevalidate(): void
31
36
$ this ->assertSame (1 , $ value );
32
37
$ this ->assertCount (0 , defer ());
33
38
$ this ->assertSame (1 , $ cache ->get ('foo ' ));
34
- $ this ->assertSame (946684800 , $ cache ->get ('foo: created ' ));
39
+ $ this ->assertSame (946684800 , $ cache ->get ('illuminate:cache:flexible: created:foo ' ));
35
40
36
41
Carbon::setTestNow (now ()->addSeconds (11 ));
37
42
@@ -43,7 +48,7 @@ public function testStaleWhileRevalidate(): void
43
48
$ this ->assertSame (1 , $ value );
44
49
$ this ->assertCount (1 , defer ());
45
50
$ this ->assertSame (1 , $ cache ->get ('foo ' ));
46
- $ this ->assertSame (946684800 , $ cache ->get ('foo: created ' ));
51
+ $ this ->assertSame (946684800 , $ cache ->get ('illuminate:cache:flexible: created:foo ' ));
47
52
48
53
// We will hit it again within the same request. This should not queue
49
54
// up an additional deferred callback as only one can be registered at
@@ -54,14 +59,14 @@ public function testStaleWhileRevalidate(): void
54
59
$ this ->assertSame (1 , $ value );
55
60
$ this ->assertCount (1 , defer ());
56
61
$ this ->assertSame (1 , $ cache ->get ('foo ' ));
57
- $ this ->assertSame (946684800 , $ cache ->get ('foo: created ' ));
62
+ $ this ->assertSame (946684800 , $ cache ->get ('illuminate:cache:flexible: created:foo ' ));
58
63
59
64
// We will now simulate the end of the request lifecycle by executing the
60
65
// deferred callback. This should refresh the cache.
61
66
defer ()->invoke ();
62
67
$ this ->assertCount (0 , defer ());
63
68
$ this ->assertSame (2 , $ cache ->get ('foo ' )); // this has been updated!
64
- $ this ->assertSame (946684811 , $ cache ->get ('foo: created ' )); // this has been updated!
69
+ $ this ->assertSame (946684811 , $ cache ->get ('illuminate:cache:flexible: created:foo ' )); // this has been updated!
65
70
66
71
// Now the cache is fresh again...
67
72
$ value = $ cache ->flexible ('foo ' , [10 , 20 ], function () use (&$ count ) {
@@ -70,7 +75,7 @@ public function testStaleWhileRevalidate(): void
70
75
$ this ->assertSame (2 , $ value );
71
76
$ this ->assertCount (0 , defer ());
72
77
$ this ->assertSame (2 , $ cache ->get ('foo ' ));
73
- $ this ->assertSame (946684811 , $ cache ->get ('foo: created ' ));
78
+ $ this ->assertSame (946684811 , $ cache ->get ('illuminate:cache:flexible: created:foo ' ));
74
79
75
80
// Let's now progress time beyond the stale TTL...
76
81
Carbon::setTestNow (now ()->addSeconds (21 ));
@@ -82,7 +87,7 @@ public function testStaleWhileRevalidate(): void
82
87
$ this ->assertSame (3 , $ value );
83
88
$ this ->assertCount (0 , defer ());
84
89
$ this ->assertSame (3 , $ cache ->get ('foo ' ));
85
- $ this ->assertSame (946684832 , $ cache ->get ('foo: created ' ));
90
+ $ this ->assertSame (946684832 , $ cache ->get ('illuminate:cache:flexible: created:foo ' ));
86
91
87
92
// Now lets see what happens when another request, job, or command is
88
93
// also trying to refresh the same key at the same time. Will push past
@@ -94,28 +99,28 @@ public function testStaleWhileRevalidate(): void
94
99
$ this ->assertSame (3 , $ value );
95
100
$ this ->assertCount (1 , defer ());
96
101
$ this ->assertSame (3 , $ cache ->get ('foo ' ));
97
- $ this ->assertSame (946684832 , $ cache ->get ('foo: created ' ));
102
+ $ this ->assertSame (946684832 , $ cache ->get ('illuminate:cache:flexible: created:foo ' ));
98
103
99
104
// Now we will execute the deferred callback but we will first aquire
100
105
// our own lock. This means that the value should not be refreshed by
101
106
// deferred callback.
102
107
/** @var Lock */
103
- $ lock = $ cache ->lock ('illuminate:cache:refresh :lock:foo ' );
108
+ $ lock = $ cache ->lock ('illuminate:cache:flexible :lock:foo ' );
104
109
105
110
$ this ->assertTrue ($ lock ->acquire ());
106
111
defer ()->first ()();
107
112
$ this ->assertSame (3 , $ value );
108
113
$ this ->assertCount (1 , defer ());
109
114
$ this ->assertSame (3 , $ cache ->get ('foo ' ));
110
- $ this ->assertSame (946684832 , $ cache ->get ('foo: created ' ));
115
+ $ this ->assertSame (946684832 , $ cache ->get ('illuminate:cache:flexible: created:foo ' ));
111
116
$ this ->assertTrue ($ lock ->release ());
112
117
113
118
// Now we have cleared the lock we will, one last time, confirm that
114
119
// the deferred callack does refresh the value when the lock is not active.
115
120
defer ()->invoke ();
116
121
$ this ->assertCount (0 , defer ());
117
122
$ this ->assertSame (4 , $ cache ->get ('foo ' ));
118
- $ this ->assertSame (946684843 , $ cache ->get ('foo: created ' ));
123
+ $ this ->assertSame (946684843 , $ cache ->get ('illuminate:cache:flexible: created:foo ' ));
119
124
120
125
// The last thing is to check that we don't refresh the cache in the
121
126
// deferred callback if another thread has already done the work for us.
@@ -127,13 +132,13 @@ public function testStaleWhileRevalidate(): void
127
132
$ this ->assertSame (4 , $ value );
128
133
$ this ->assertCount (1 , defer ());
129
134
$ this ->assertSame (4 , $ cache ->get ('foo ' ));
130
- $ this ->assertSame (946684843 , $ cache ->get ('foo: created ' ));
135
+ $ this ->assertSame (946684843 , $ cache ->get ('illuminate:cache:flexible: created:foo ' ));
131
136
132
137
// There is now a deferred callback ready to refresh the cache. We will
133
138
// simulate another thread updating the value.
134
139
$ cache ->putMany ([
135
140
'foo ' => 99 ,
136
- 'foo: created ' => 946684863 ,
141
+ 'illuminate:cache:flexible: created:foo ' => 946684863 ,
137
142
]);
138
143
139
144
// then we will run the refresh callback
@@ -144,6 +149,90 @@ public function testStaleWhileRevalidate(): void
144
149
$ this ->assertSame (99 , $ value );
145
150
$ this ->assertCount (0 , defer ());
146
151
$ this ->assertSame (99 , $ cache ->get ('foo ' ));
147
- $ this ->assertSame (946684863 , $ cache ->get ('foo:created ' ));
152
+ $ this ->assertSame (946684863 , $ cache ->get ('illuminate:cache:flexible:created:foo ' ));
153
+ }
154
+
155
+ public function testItHandlesStrayTtlKeyAfterMainKeyIsForgotten ()
156
+ {
157
+ $ cache = Cache::driver ('array ' );
158
+ $ count = 0 ;
159
+
160
+ $ value = $ cache ->flexible ('count ' , [5 , 10 ], function () use (&$ count ) {
161
+ $ count = 1 ;
162
+
163
+ return $ count ;
164
+ });
165
+
166
+ $ this ->assertSame (1 , $ value );
167
+ $ this ->assertSame (1 , $ count );
168
+
169
+ $ cache ->forget ('count ' );
170
+
171
+ $ value = $ cache ->flexible ('count ' , [5 , 10 ], function () use (&$ count ) {
172
+ $ count = 2 ;
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
+ $ cache = Cache::driver ('database ' );
184
+
185
+ $ cache ->flexible ('count ' , [5 , 10 ], fn () => 1 );
186
+
187
+ $ this ->assertTrue ($ cache ->has ('count ' ));
188
+ $ this ->assertTrue ($ cache ->has ('illuminate:cache:flexible:created:count ' ));
189
+
190
+ $ cache ->forget ('count ' );
191
+
192
+ $ this ->assertEmpty ($ cache ->getConnection ()->table ('cache ' )->get ());
193
+ $ this ->assertTrue ($ cache ->missing ('count ' ));
194
+ $ this ->assertTrue ($ cache ->missing ('illuminate:cache:flexible:created:count ' ));
195
+
196
+ $ cache ->flexible ('count ' , [5 , 10 ], fn () => 1 );
197
+
198
+ $ this ->assertTrue ($ cache ->has ('count ' ));
199
+ $ this ->assertTrue ($ cache ->has ('illuminate:cache:flexible:created:count ' ));
200
+
201
+ $ this ->travel (20 )->seconds ();
202
+ $ cache ->forgetIfExpired ('count ' );
203
+
204
+ $ this ->assertEmpty ($ cache ->getConnection ()->table ('cache ' )->get ());
205
+ $ this ->assertTrue ($ cache ->missing ('count ' ));
206
+ $ this ->assertTrue ($ cache ->missing ('illuminate:cache:flexible:created:count ' ));
207
+ }
208
+
209
+ public function testItImplicitlyClearsTtlKeysFromFileDriver ()
210
+ {
211
+ $ this ->freezeTime ();
212
+ $ cache = Cache::driver ('file ' );
213
+
214
+ $ cache ->flexible ('count ' , [5 , 10 ], fn () => 1 );
215
+
216
+ $ this ->assertTrue ($ cache ->has ('count ' ));
217
+ $ this ->assertTrue ($ cache ->has ('illuminate:cache:flexible:created:count ' ));
218
+
219
+ $ cache ->forget ('count ' );
220
+
221
+ $ this ->assertFalse ($ cache ->getFilesystem ()->exists ($ cache ->path ('count ' )));
222
+ $ this ->assertFalse ($ cache ->getFilesystem ()->exists ($ cache ->path ('illuminate:cache:flexible:created:count ' )));
223
+ $ this ->assertTrue ($ cache ->missing ('count ' ));
224
+ $ this ->assertTrue ($ cache ->missing ('illuminate:cache:flexible:created:count ' ));
225
+
226
+ $ cache ->flexible ('count ' , [5 , 10 ], fn () => 1 );
227
+
228
+ $ this ->assertTrue ($ cache ->has ('count ' ));
229
+ $ this ->assertTrue ($ cache ->has ('illuminate:cache:flexible:created:count ' ));
230
+
231
+ $ this ->travel (20 )->seconds ();
232
+
233
+ $ this ->assertTrue ($ cache ->missing ('count ' ));
234
+ $ this ->assertFalse ($ cache ->getFilesystem ()->exists ($ cache ->path ('count ' )));
235
+ $ this ->assertFalse ($ cache ->getFilesystem ()->exists ($ cache ->path ('illuminate:cache:flexible:created:count ' )));
236
+ $ this ->assertTrue ($ cache ->missing ('illuminate:cache:flexible:created:count ' ));
148
237
}
149
238
}
0 commit comments