From d0ead1fdb843fb7dbfa2b261e48dd8f506845c97 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 25 May 2022 15:39:55 -1000 Subject: [PATCH] Fix history cache when there is cacheConfig (#12788) --- src/data/cached-history.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/data/cached-history.ts b/src/data/cached-history.ts index 595c30af7b..563d3d898b 100644 --- a/src/data/cached-history.ts +++ b/src/data/cached-history.ts @@ -34,7 +34,7 @@ const RECENT_THRESHOLD = 60000; // 1 minute const RECENT_CACHE: { [cacheKey: string]: RecentCacheResults } = {}; const stateHistoryCache: { [cacheKey: string]: CachedResults } = {}; -// Cached type 1 unction. Without cache config. +// Cached type 1 function. Without cache config. export const getRecent = ( hass: HomeAssistant, entityId: string, @@ -103,13 +103,14 @@ export const getRecentWithCache = ( language: string ) => { const cacheKey = cacheConfig.cacheKey; + const fullCacheKey = cacheKey + `_${cacheConfig.hoursToShow}`; const endTime = new Date(); const startTime = new Date(endTime); startTime.setHours(startTime.getHours() - cacheConfig.hoursToShow); let toFetchStartTime = startTime; let appendingToCache = false; - let cache = stateHistoryCache[cacheKey + `_${cacheConfig.hoursToShow}`]; + let cache = stateHistoryCache[fullCacheKey]; if ( cache && toFetchStartTime >= cache.startTime && @@ -123,7 +124,7 @@ export const getRecentWithCache = ( return cache.prom; } } else { - cache = stateHistoryCache[cacheKey] = getEmptyCache( + cache = stateHistoryCache[fullCacheKey] = getEmptyCache( language, startTime, endTime @@ -152,7 +153,7 @@ export const getRecentWithCache = ( ]); fetchedHistory = results[1]; } catch (err: any) { - delete stateHistoryCache[cacheKey]; + delete stateHistoryCache[fullCacheKey]; throw err; } const stateHistory = computeHistory(hass, fetchedHistory, localize);