Skip to content

Commit

Permalink
feat(cache): Invalidate cache on every new card version (RomRider#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomRider authored Jan 27, 2021
1 parent 1d0aca8 commit b1799d9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/apexcharts-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ localForage.config({
localForage
.iterate((data, key) => {
const value: EntityEntryCache = key.endsWith('-raw') ? data : decompress(data);
if (value.card_version !== pjson.version) {
localForage.removeItem(key);
}
const start = new Date();
if (value.span === undefined) {
start.setTime(start.getTime() - value.span);
if (new Date(value.last_fetched) < start) {
localForage.removeItem(key);
} else {
start.setTime(start.getTime() - value.span);
if (new Date(value.last_fetched) < start) {
localForage.removeItem(key);
}
}
})
.catch((err) => {
Expand Down
3 changes: 3 additions & 0 deletions src/graphEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { HOUR_24, moment } from './const';
import parse from 'parse-duration';
import SparkMD5 from 'spark-md5';
import { ChartCardSpanExtConfig } from './types-config';
import * as pjson from '../package.json';

export default class GraphEntry {
private _history?: EntityEntryCache;
Expand Down Expand Up @@ -171,12 +172,14 @@ export default class GraphEntry {
if (history?.data.length) {
history.span = this._graphSpan;
history.last_fetched = new Date();
history.card_version = pjson.version;
if (history.data.length !== 0) {
history.data.push(...newStateHistory);
}
} else {
history = {
span: this._graphSpan,
card_version: pjson.version,
last_fetched: new Date(),
data: newStateHistory,
};
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface ChartCardSeriesConfig extends ChartCardSeriesExternalConfig {

export interface EntityEntryCache {
span: number;
card_version: string;
last_fetched: Date;
data: EntityCachePoints;
}
Expand Down

0 comments on commit b1799d9

Please sign in to comment.