Skip to content

Commit 0a669f4

Browse files
hayase_yasuhiroclaude
andcommitted
Update tests to use hash tag key format
Tests updated to account for the new key format introduced by the Cache::flexible() fix for Redis Cluster compatibility. Changes: - Calculate hash keys using md5() in test setup - Use {hashKey} prefixed keys when checking cache values - Update MemoizedStoreTest::test_it_supports_with_flexible - Update RepositoryTest::testStaleWhileRevalidate - Update RepositoryTest::testItHandlesStrayTtlKeyAfterMainKeyIsForgotten - Update RepositoryTest::testItImplicitlyClearsTtlKeysFromDatabaseCache - Update RepositoryTest::testItImplicitlyClearsTtlKeysFromFileDriver 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6a82038 commit 0a669f4

File tree

2 files changed

+77
-52
lines changed

2 files changed

+77
-52
lines changed

tests/Integration/Cache/MemoizedStoreTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,17 @@ public function getPrefix()
486486
public function test_it_supports_with_flexible()
487487
{
488488
$this->freezeTime();
489+
490+
// Calculate the hash key for 'key'
491+
$hashKey = substr(md5('key'), 0, 4);
492+
$valueKey = "{{$hashKey}}:key";
493+
489494
Cache::flexible('key', [10, 20], 'value-1');
490495

491496
$this->travel(11)->seconds();
492497
Cache::memo()->flexible('key', [10, 20], 'value-2');
493498
defer()->invoke();
494-
$value = Cache::get('key');
499+
$value = Cache::get($valueKey);
495500

496501
$this->assertSame('value-2', $value);
497502
}

tests/Integration/Cache/RepositoryTest.php

Lines changed: 71 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,29 @@ public function testStaleWhileRevalidate(): void
2121
$cache = Cache::driver('array');
2222
$count = 0;
2323

24+
// Calculate the hash key for 'foo'
25+
$hashKey = substr(md5('foo'), 0, 4);
26+
$valueKey = "{{$hashKey}}:foo";
27+
$createdKey = "{{$hashKey}}:illuminate:cache:flexible:created:foo";
28+
2429
// Cache is empty. The value should be populated...
2530
$value = $cache->flexible('foo', [10, 20], function () use (&$count) {
2631
return ++$count;
2732
});
2833

2934
$this->assertSame(1, $value);
3035
$this->assertCount(0, defer());
31-
$this->assertSame(1, $cache->get('foo'));
32-
$this->assertSame(946684800, $cache->get('illuminate:cache:flexible:created:foo'));
36+
$this->assertSame(1, $cache->get($valueKey));
37+
$this->assertSame(946684800, $cache->get($createdKey));
3338

3439
// Cache is fresh. The value should be retrieved from the cache and used...
3540
$value = $cache->flexible('foo', [10, 20], function () use (&$count) {
3641
return ++$count;
3742
});
3843
$this->assertSame(1, $value);
3944
$this->assertCount(0, defer());
40-
$this->assertSame(1, $cache->get('foo'));
41-
$this->assertSame(946684800, $cache->get('illuminate:cache:flexible:created:foo'));
45+
$this->assertSame(1, $cache->get($valueKey));
46+
$this->assertSame(946684800, $cache->get($createdKey));
4247

4348
Carbon::setTestNow(now()->addSeconds(11));
4449

@@ -49,8 +54,8 @@ public function testStaleWhileRevalidate(): void
4954
});
5055
$this->assertSame(1, $value);
5156
$this->assertCount(1, defer());
52-
$this->assertSame(1, $cache->get('foo'));
53-
$this->assertSame(946684800, $cache->get('illuminate:cache:flexible:created:foo'));
57+
$this->assertSame(1, $cache->get($valueKey));
58+
$this->assertSame(946684800, $cache->get($createdKey));
5459

5560
// We will hit it again within the same request. This should not queue
5661
// up an additional deferred callback as only one can be registered at
@@ -60,24 +65,24 @@ public function testStaleWhileRevalidate(): void
6065
});
6166
$this->assertSame(1, $value);
6267
$this->assertCount(1, defer());
63-
$this->assertSame(1, $cache->get('foo'));
64-
$this->assertSame(946684800, $cache->get('illuminate:cache:flexible:created:foo'));
68+
$this->assertSame(1, $cache->get($valueKey));
69+
$this->assertSame(946684800, $cache->get($createdKey));
6570

6671
// We will now simulate the end of the request lifecycle by executing the
6772
// deferred callback. This should refresh the cache.
6873
defer()->invoke();
6974
$this->assertCount(0, defer());
70-
$this->assertSame(2, $cache->get('foo')); // this has been updated!
71-
$this->assertSame(946684811, $cache->get('illuminate:cache:flexible:created:foo')); // this has been updated!
75+
$this->assertSame(2, $cache->get($valueKey)); // this has been updated!
76+
$this->assertSame(946684811, $cache->get($createdKey)); // this has been updated!
7277

7378
// Now the cache is fresh again...
7479
$value = $cache->flexible('foo', [10, 20], function () use (&$count) {
7580
return ++$count;
7681
});
7782
$this->assertSame(2, $value);
7883
$this->assertCount(0, defer());
79-
$this->assertSame(2, $cache->get('foo'));
80-
$this->assertSame(946684811, $cache->get('illuminate:cache:flexible:created:foo'));
84+
$this->assertSame(2, $cache->get($valueKey));
85+
$this->assertSame(946684811, $cache->get($createdKey));
8186

8287
// Let's now progress time beyond the stale TTL...
8388
Carbon::setTestNow(now()->addSeconds(21));
@@ -88,8 +93,8 @@ public function testStaleWhileRevalidate(): void
8893
});
8994
$this->assertSame(3, $value);
9095
$this->assertCount(0, defer());
91-
$this->assertSame(3, $cache->get('foo'));
92-
$this->assertSame(946684832, $cache->get('illuminate:cache:flexible:created:foo'));
96+
$this->assertSame(3, $cache->get($valueKey));
97+
$this->assertSame(946684832, $cache->get($createdKey));
9398

9499
// Now lets see what happens when another request, job, or command is
95100
// also trying to refresh the same key at the same time. Will push past
@@ -100,29 +105,30 @@ public function testStaleWhileRevalidate(): void
100105
});
101106
$this->assertSame(3, $value);
102107
$this->assertCount(1, defer());
103-
$this->assertSame(3, $cache->get('foo'));
104-
$this->assertSame(946684832, $cache->get('illuminate:cache:flexible:created:foo'));
108+
$this->assertSame(3, $cache->get($valueKey));
109+
$this->assertSame(946684832, $cache->get($createdKey));
105110

106111
// Now we will execute the deferred callback but we will first acquire
107112
// our own lock. This means that the value should not be refreshed by
108113
// deferred callback.
109114
/** @var Lock */
110-
$lock = $cache->lock('illuminate:cache:flexible:lock:foo');
115+
$lockKey = "{{$hashKey}}:illuminate:cache:flexible:lock:foo";
116+
$lock = $cache->lock($lockKey);
111117

112118
$this->assertTrue($lock->acquire());
113119
defer()->first()();
114120
$this->assertSame(3, $value);
115121
$this->assertCount(1, defer());
116-
$this->assertSame(3, $cache->get('foo'));
117-
$this->assertSame(946684832, $cache->get('illuminate:cache:flexible:created:foo'));
122+
$this->assertSame(3, $cache->get($valueKey));
123+
$this->assertSame(946684832, $cache->get($createdKey));
118124
$this->assertTrue($lock->release());
119125

120126
// Now we have cleared the lock we will, one last time, confirm that
121127
// the deferred callback does refresh the value when the lock is not active.
122128
defer()->invoke();
123129
$this->assertCount(0, defer());
124-
$this->assertSame(4, $cache->get('foo'));
125-
$this->assertSame(946684843, $cache->get('illuminate:cache:flexible:created:foo'));
130+
$this->assertSame(4, $cache->get($valueKey));
131+
$this->assertSame(946684843, $cache->get($createdKey));
126132

127133
// The last thing is to check that we don't refresh the cache in the
128134
// deferred callback if another thread has already done the work for us.
@@ -133,14 +139,14 @@ public function testStaleWhileRevalidate(): void
133139
});
134140
$this->assertSame(4, $value);
135141
$this->assertCount(1, defer());
136-
$this->assertSame(4, $cache->get('foo'));
137-
$this->assertSame(946684843, $cache->get('illuminate:cache:flexible:created:foo'));
142+
$this->assertSame(4, $cache->get($valueKey));
143+
$this->assertSame(946684843, $cache->get($createdKey));
138144

139145
// There is now a deferred callback ready to refresh the cache. We will
140146
// simulate another thread updating the value.
141147
$cache->putMany([
142-
'foo' => 99,
143-
'illuminate:cache:flexible:created:foo' => 946684863,
148+
$valueKey => 99,
149+
$createdKey => 946684863,
144150
]);
145151

146152
// then we will run the refresh callback
@@ -150,15 +156,19 @@ public function testStaleWhileRevalidate(): void
150156
});
151157
$this->assertSame(99, $value);
152158
$this->assertCount(0, defer());
153-
$this->assertSame(99, $cache->get('foo'));
154-
$this->assertSame(946684863, $cache->get('illuminate:cache:flexible:created:foo'));
159+
$this->assertSame(99, $cache->get($valueKey));
160+
$this->assertSame(946684863, $cache->get($createdKey));
155161
}
156162

157163
public function testItHandlesStrayTtlKeyAfterMainKeyIsForgotten()
158164
{
159165
$cache = Cache::driver('array');
160166
$count = 0;
161167

168+
// Calculate the hash key for 'count'
169+
$hashKey = substr(md5('count'), 0, 4);
170+
$valueKey = "{{$hashKey}}:count";
171+
162172
$value = $cache->flexible('count', [5, 10], function () use (&$count) {
163173
$count = 1;
164174

@@ -168,7 +178,7 @@ public function testItHandlesStrayTtlKeyAfterMainKeyIsForgotten()
168178
$this->assertSame(1, $value);
169179
$this->assertSame(1, $count);
170180

171-
$cache->forget('count');
181+
$cache->forget($valueKey);
172182

173183
$value = $cache->flexible('count', [5, 10], function () use (&$count) {
174184
$count = 2;
@@ -184,58 +194,68 @@ public function testItImplicitlyClearsTtlKeysFromDatabaseCache()
184194
$this->freezeTime();
185195
$cache = Cache::driver('database');
186196

197+
// Calculate the hash key for 'count'
198+
$hashKey = substr(md5('count'), 0, 4);
199+
$valueKey = "{{$hashKey}}:count";
200+
$createdKey = "{{$hashKey}}:illuminate:cache:flexible:created:count";
201+
187202
$cache->flexible('count', [5, 10], fn () => 1);
188203

189-
$this->assertTrue($cache->has('count'));
190-
$this->assertTrue($cache->has('illuminate:cache:flexible:created:count'));
204+
$this->assertTrue($cache->has($valueKey));
205+
$this->assertTrue($cache->has($createdKey));
191206

192-
$cache->forget('count');
207+
$cache->forget($valueKey);
193208

194209
$this->assertEmpty($cache->getConnection()->table('cache')->get());
195-
$this->assertTrue($cache->missing('count'));
196-
$this->assertTrue($cache->missing('illuminate:cache:flexible:created:count'));
210+
$this->assertTrue($cache->missing($valueKey));
211+
$this->assertTrue($cache->missing($createdKey));
197212

198213
$cache->flexible('count', [5, 10], fn () => 1);
199214

200-
$this->assertTrue($cache->has('count'));
201-
$this->assertTrue($cache->has('illuminate:cache:flexible:created:count'));
215+
$this->assertTrue($cache->has($valueKey));
216+
$this->assertTrue($cache->has($createdKey));
202217

203218
$this->travel(20)->seconds();
204-
$cache->forgetIfExpired('count');
219+
$cache->forgetIfExpired($valueKey);
205220

206221
$this->assertEmpty($cache->getConnection()->table('cache')->get());
207-
$this->assertTrue($cache->missing('count'));
208-
$this->assertTrue($cache->missing('illuminate:cache:flexible:created:count'));
222+
$this->assertTrue($cache->missing($valueKey));
223+
$this->assertTrue($cache->missing($createdKey));
209224
}
210225

211226
public function testItImplicitlyClearsTtlKeysFromFileDriver()
212227
{
213228
$this->freezeTime();
214229
$cache = Cache::driver('file');
215230

231+
// Calculate the hash key for 'count'
232+
$hashKey = substr(md5('count'), 0, 4);
233+
$valueKey = "{{$hashKey}}:count";
234+
$createdKey = "{{$hashKey}}:illuminate:cache:flexible:created:count";
235+
216236
$cache->flexible('count', [5, 10], fn () => 1);
217237

218-
$this->assertTrue($cache->has('count'));
219-
$this->assertTrue($cache->has('illuminate:cache:flexible:created:count'));
238+
$this->assertTrue($cache->has($valueKey));
239+
$this->assertTrue($cache->has($createdKey));
220240

221-
$cache->forget('count');
241+
$cache->forget($valueKey);
222242

223-
$this->assertFalse($cache->getFilesystem()->exists($cache->path('count')));
224-
$this->assertFalse($cache->getFilesystem()->exists($cache->path('illuminate:cache:flexible:created:count')));
225-
$this->assertTrue($cache->missing('count'));
226-
$this->assertTrue($cache->missing('illuminate:cache:flexible:created:count'));
243+
$this->assertFalse($cache->getFilesystem()->exists($cache->path($valueKey)));
244+
$this->assertFalse($cache->getFilesystem()->exists($cache->path($createdKey)));
245+
$this->assertTrue($cache->missing($valueKey));
246+
$this->assertTrue($cache->missing($createdKey));
227247

228248
$cache->flexible('count', [5, 10], fn () => 1);
229249

230-
$this->assertTrue($cache->has('count'));
231-
$this->assertTrue($cache->has('illuminate:cache:flexible:created:count'));
250+
$this->assertTrue($cache->has($valueKey));
251+
$this->assertTrue($cache->has($createdKey));
232252

233253
$this->travel(20)->seconds();
234254

235-
$this->assertTrue($cache->missing('count'));
236-
$this->assertFalse($cache->getFilesystem()->exists($cache->path('count')));
237-
$this->assertFalse($cache->getFilesystem()->exists($cache->path('illuminate:cache:flexible:created:count')));
238-
$this->assertTrue($cache->missing('illuminate:cache:flexible:created:count'));
255+
$this->assertTrue($cache->missing($valueKey));
256+
$this->assertFalse($cache->getFilesystem()->exists($cache->path($valueKey)));
257+
$this->assertFalse($cache->getFilesystem()->exists($cache->path($createdKey)));
258+
$this->assertTrue($cache->missing($createdKey));
239259
}
240260

241261
public function testItCanAlwaysDefer()

0 commit comments

Comments
 (0)