UNPKG

matterbridge-hass

Version:
39 lines (38 loc) 1.35 kB
import { AnsiLogger } from 'matterbridge/logger'; const CACHE_KEY = 'stateCache'; export class StateCache { cache = new Map(); log = new AnsiLogger({ logName: 'StateCache', logTimestampFormat: 4 }); async load(context) { this.log.debug('Loading cached states from storage...'); const storedCache = await context.get(CACHE_KEY, []); for (const state of storedCache) { this.cache.set(state.entity_id, state); } this.log.debug(`Loaded ${this.cache.size} cached states from storage`); } async save(context) { this.log.debug('Saving cached states to storage...'); const states = Array.from(this.cache.values()); await context.set(CACHE_KEY, states); this.log.debug(`Saved ${this.cache.size} cached states to storage`); } clear() { this.cache.clear(); this.log.debug('Cleared all cached states from memory'); } size() { return this.cache.size; } add(state) { this.cache.set(state.entity_id, state); this.log.debug(`Added/Updated cached state for entity ${state.entity_id}`); } get(entityId) { return this.cache.get(entityId); } remove(entityId) { this.cache.delete(entityId); this.log.debug(`Removed cached state for entity ${entityId}`); } }