@@ -38,11 +38,13 @@ static bool hasExpired(const kj::Maybe<double>& expiration, bool allowOutsideIoC
38
38
39
39
SharedMemoryCache::SharedMemoryCache (kj::Maybe<const MemoryCacheProvider&> provider,
40
40
kj::StringPtr id,
41
- kj::Maybe<AdditionalResizeMemoryLimitHandler&> additionalResizeMemoryLimitHandler)
41
+ kj::Maybe<AdditionalResizeMemoryLimitHandler&> additionalResizeMemoryLimitHandler,
42
+ const kj::MonotonicClock& timer)
42
43
: data(),
43
44
provider (provider),
44
45
id(kj::str(id)),
45
- additionalResizeMemoryLimitHandler(additionalResizeMemoryLimitHandler) {}
46
+ additionalResizeMemoryLimitHandler(additionalResizeMemoryLimitHandler),
47
+ timer(timer) {}
46
48
47
49
SharedMemoryCache::~SharedMemoryCache () noexcept (false ) {
48
50
KJ_IF_SOME (p, provider) {
@@ -219,8 +221,9 @@ void SharedMemoryCache::removeIfExistsWhileLocked(
219
221
kj::Own<const SharedMemoryCache> SharedMemoryCache::create (
220
222
kj::Maybe<const MemoryCacheProvider&> provider,
221
223
kj::StringPtr id,
222
- kj::Maybe<AdditionalResizeMemoryLimitHandler&> handler) {
223
- return kj::atomicRefcounted<const SharedMemoryCache>(provider, id, handler);
224
+ kj::Maybe<AdditionalResizeMemoryLimitHandler&> handler,
225
+ const kj::MonotonicClock& timer) {
226
+ return kj::atomicRefcounted<const SharedMemoryCache>(provider, id, handler, timer);
224
227
}
225
228
226
229
SharedMemoryCache::Use::Use (kj::Own<const SharedMemoryCache> cache, const Limits& limits)
@@ -240,14 +243,22 @@ SharedMemoryCache::Use::~Use() noexcept(false) {
240
243
}
241
244
242
245
kj::Maybe<kj::Own<CacheValue>> SharedMemoryCache::Use::getWithoutFallback (
243
- const kj::String& key) const {
244
- auto data = cache->data .lockExclusive ();
246
+ const kj::String& key, SpanBuilder& span) const {
247
+ kj::Locked<ThreadUnsafeData> data = [&] {
248
+ auto memoryCacheLockRecord =
249
+ ScopedDurationTagger (span, memoryCachekLockWaitTimeTag, cache->timer );
250
+ return cache->data .lockExclusive ();
251
+ }();
245
252
return cache->getWhileLocked (*data, key);
246
253
}
247
254
248
255
kj::OneOf<kj::Own<CacheValue>, kj::Promise<SharedMemoryCache::Use::GetWithFallbackOutcome>>
249
- SharedMemoryCache::Use::getWithFallback (const kj::String& key) const {
250
- auto data = cache->data .lockExclusive ();
256
+ SharedMemoryCache::Use::getWithFallback (const kj::String& key, SpanBuilder& span) const {
257
+ kj::Locked<ThreadUnsafeData> data = [&] {
258
+ auto memoryCacheLockRecord =
259
+ ScopedDurationTagger (span, memoryCachekLockWaitTimeTag, cache->timer );
260
+ return cache->data .lockExclusive ();
261
+ }();
251
262
KJ_IF_SOME (existingValue, cache->getWhileLocked (*data, key)) {
252
263
return kj::mv (existingValue);
253
264
} else KJ_IF_SOME (existingInProgress, data->inProgress .find (key)) {
@@ -374,7 +385,7 @@ jsg::Promise<jsg::JsRef<jsg::JsValue>> MemoryCache::read(jsg::Lock& js,
374
385
auto readSpan = IoContext::current ().makeTraceSpan (" memory_cache_read" _kjc);
375
386
376
387
KJ_IF_SOME (fallback, optionalFallback) {
377
- KJ_SWITCH_ONEOF (cacheUse.getWithFallback (key.value )) {
388
+ KJ_SWITCH_ONEOF (cacheUse.getWithFallback (key.value , readSpan )) {
378
389
KJ_CASE_ONEOF (result, kj::Own<CacheValue>) {
379
390
// Optimization: Don't even release the isolate lock if the value is aleady in cache.
380
391
jsg::Deserializer deserializer (js, result->bytes .asPtr ());
@@ -423,7 +434,7 @@ jsg::Promise<jsg::JsRef<jsg::JsValue>> MemoryCache::read(jsg::Lock& js,
423
434
}
424
435
KJ_UNREACHABLE;
425
436
} else {
426
- KJ_IF_SOME (cacheValue, cacheUse.getWithoutFallback (key.value )) {
437
+ KJ_IF_SOME (cacheValue, cacheUse.getWithoutFallback (key.value , readSpan )) {
427
438
jsg::Deserializer deserializer (js, cacheValue->bytes .asPtr ());
428
439
return js.resolvedPromise (jsg::JsRef (js, deserializer.readValue (js)));
429
440
}
@@ -433,10 +444,11 @@ jsg::Promise<jsg::JsRef<jsg::JsValue>> MemoryCache::read(jsg::Lock& js,
433
444
434
445
// ======================================================================================
435
446
436
- MemoryCacheProvider::MemoryCacheProvider (
447
+ MemoryCacheProvider::MemoryCacheProvider (const kj::MonotonicClock& timer,
437
448
kj::Maybe<SharedMemoryCache::AdditionalResizeMemoryLimitHandler>
438
449
additionalResizeMemoryLimitHandler)
439
- : additionalResizeMemoryLimitHandler(kj::mv(additionalResizeMemoryLimitHandler)) {}
450
+ : additionalResizeMemoryLimitHandler(kj::mv(additionalResizeMemoryLimitHandler)),
451
+ timer (timer) {}
440
452
441
453
MemoryCacheProvider::~MemoryCacheProvider () noexcept (false ) {
442
454
// TODO(cleanup): Later, assuming progress is made on kj::Ptr<T>, we ought to be able
@@ -456,7 +468,7 @@ kj::Own<const SharedMemoryCache> MemoryCacheProvider::getInstance(
456
468
-> SharedMemoryCache::AdditionalResizeMemoryLimitHandler& {
457
469
return const_cast <SharedMemoryCache::AdditionalResizeMemoryLimitHandler&>(handler);
458
470
});
459
- return SharedMemoryCache::create (provider, id, handler);
471
+ return SharedMemoryCache::create (provider, id, handler, timer );
460
472
};
461
473
462
474
KJ_IF_SOME (cid, cacheId) {
0 commit comments