mirror of
https://github.com/ConnectedHumber/Air-Quality-Web
synced 2024-11-22 06:23:01 +00:00
Prune the reading cache, but I'm unsure how effective it is.
This commit is contained in:
parent
8d61990828
commit
835768830c
1 changed files with 33 additions and 2 deletions
|
@ -158,19 +158,50 @@ class LayerHeatmap {
|
||||||
* @return {Promise} The requested data array, as the return value of a promise
|
* @return {Promise} The requested data array, as the return value of a promise
|
||||||
*/
|
*/
|
||||||
async fetch_data(datetime, reading_type) {
|
async fetch_data(datetime, reading_type) {
|
||||||
let cache_key = Symbol.for(`${reading_type}|${datetime.toISOString()}`);
|
let cache_key = `${reading_type}|${datetime.toISOString()}`;
|
||||||
let result = this.reading_cache.get(cache_key);
|
let result = this.reading_cache.get(cache_key);
|
||||||
|
|
||||||
if(typeof result == "undefined") {
|
if(typeof result == "undefined") {
|
||||||
result = JSON.parse(await GetFromUrl(
|
result = JSON.parse(await GetFromUrl(
|
||||||
`${Config.api_root}?action=fetch-data&datetime=${encodeURIComponent(datetime.toISOString())}&reading_type=${encodeURIComponent(reading_type)}`
|
`${Config.api_root}?action=fetch-data&datetime=${encodeURIComponent(datetime.toISOString())}&reading_type=${encodeURIComponent(reading_type)}`
|
||||||
));
|
));
|
||||||
this.reading_cache.set(cache_key, result);
|
this.prune_cache(100);
|
||||||
|
this.reading_cache.set(cache_key, { data: result, inserted: new Date() });
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
result = result.data;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prunes the reading cache, leaving at most newest_count items behind.
|
||||||
|
* The items inserted first are deleted first.
|
||||||
|
* @param {Number} newest_count The numebr of items to leave behind in the cache.
|
||||||
|
* @returns {Number} The number of items deleted from the cache.
|
||||||
|
*/
|
||||||
|
prune_cache(newest_count) {
|
||||||
|
let items = [];
|
||||||
|
for(let next_key of this.reading_cache) {
|
||||||
|
let cache_item = this.reading_cache.get(next_key);
|
||||||
|
if(typeof cache_item == "undefined") {
|
||||||
|
this.reading_cache.delete(cache_item);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
items.push({
|
||||||
|
key: next_key,
|
||||||
|
date: cache_item.inserted
|
||||||
|
});
|
||||||
|
}
|
||||||
|
items.sort((a, b) => a.date - b.date);
|
||||||
|
let deleted = 0;
|
||||||
|
for(let i = 0; i < items.length - newest_count; i++) {
|
||||||
|
this.reading_cache.delete(this.items[i].key);
|
||||||
|
deleted++;
|
||||||
|
}
|
||||||
|
return deleted;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether the reading cache contains data for the given datetime & reading type.
|
* Whether the reading cache contains data for the given datetime & reading type.
|
||||||
* @param {Date} datetime The datetime to check.
|
* @param {Date} datetime The datetime to check.
|
||||||
|
|
Loading…
Reference in a new issue