From 153747e39cc1edbcab59d7ea510d8f5a684324af Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sun, 1 May 2022 15:43:44 +0200 Subject: [PATCH] mod: fix checking for existing entries in EventDataCache --- .../typescript/main/cache/EventDataCache.ts | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/modules/events-mod/src/main/typescript/main/cache/EventDataCache.ts b/modules/events-mod/src/main/typescript/main/cache/EventDataCache.ts index d61e801..4dbaa09 100644 --- a/modules/events-mod/src/main/typescript/main/cache/EventDataCache.ts +++ b/modules/events-mod/src/main/typescript/main/cache/EventDataCache.ts @@ -62,7 +62,7 @@ export namespace EventUpdates { resetLastUpdated: boolean, expirationDurationTicks?: uint, ) { - const entry: CacheEntry = this.getCacheEntry(key) ?? + const entry: CacheEntry = this.getCacheEntry(key, type) ?? Manager.createCacheEntry(key, type) mutate(entry.packet) if (resetLastUpdated) { @@ -80,9 +80,10 @@ export namespace EventUpdates { */ public setExpiration( key: PacketKey, + type: PacketType, expirationDurationTicks: uint, ) { - const entry = this.getCacheEntry(key) + const entry = this.getCacheEntry(key, type) if (entry != undefined) { entry.expirationDurationTicks = expirationDurationTicks } @@ -102,22 +103,27 @@ export namespace EventUpdates { global.cache.delete(key) } } - if (countExpired + countTotal > 0) { - log(`cache expired items count: ${countExpired}, total: ${countTotal}`) + + const hasExpiredItems = countExpired > 0 + const hasAnyItem = countTotal > 0 + + if (hasExpiredItems || (game.tick % 30 == 0 && hasAnyItem)) { + log(`[extractExpired] expired items: ${countExpired}, total: ${countTotal}`) } return expiredData } private getCacheEntry( - key: PacketKey + key: EventUpdates.PacketKey, + type: EventUpdates.PacketType, ): CacheEntry | undefined { const hash = Manager.hashKey(key) if (!global.cache.has(hash)) { return undefined } const value: CacheEntry = global.cache.get(hash) - if (this.isEntryInstanceOf(value, key)) { + if (this.isEntryInstanceOf(value, type)) { return value } else { // Type mismatch. This shouldn't happen... @@ -134,7 +140,7 @@ export namespace EventUpdates { type: type, key: key, } - let entry: CacheEntry = new CacheEntry(data) + const entry: CacheEntry = new CacheEntry(data) const hash = Manager.hashKey(key) global.cache.set(hash, entry) return entry @@ -156,17 +162,17 @@ export namespace EventUpdates { isEntryInstanceOf( entry: CacheEntry | undefined, - key: PacketKey, + type: EventUpdates.PacketType, ): entry is CacheEntry { - return this.isDataInstanceOf(entry?.packet, key) + return this.isDataInstanceOf(entry?.packet, type) } isDataInstanceOf( packet: PACKET | undefined, - key: PacketKey, + type: EventUpdates.PacketType, ): packet is PACKET { - return packet != undefined && packet.key == key + return packet != undefined && packet.type == type } private static hashKey(