Files
openwrt-R7800-nss/package/utils/cli/files/usr/share/ucode/cli/cache.uc
Felix Fietkau 248b66b44f cli: add OpenWrt CLI
This provides an easy to use modular CLI that can be used to interact with
OpenWrt services. It has full support for context sensitive tab completion
and help.
Extra modules can be provided by packages and can extend the existing node
structure in any place.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2025-02-13 19:00:30 +01:00

61 lines
1.0 KiB
Ucode

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2025 Felix Fietkau <nbd@nbd.name>
'use strict';
const CACHE_DEFAULT_TIMEOUT = 5;
function cache_get(key, fn, timeout)
{
let now = time();
let entry = this.entries[key];
if (entry) {
if (now < entry.timeout)
return entry.data;
if (!fn)
delete this.entries[key];
}
if (!fn)
return;
let data = fn();
if (!entry)
this.entries[key] = entry = {};
timeout ??= CACHE_DEFAULT_TIMEOUT;
entry.timeout = now + timeout;
entry.data = data;
return data;
}
function cache_remove(key)
{
delete this.entries[key];
}
function cache_gc() {
let now = time();
for (let key, entry in this.entries)
if (now > entry.timeout)
delete this.entries[key];
}
const cache_proto = {
get: cache_get,
remove: cache_remove,
gc: cache_gc,
};
export function new(model) {
model.cache_proto ??= { model, ...cache_proto };
let cache = proto({
entries: {},
}, model.cache_proto);
cache.gc_interval = model.uloop.interval(10000, () => {
cache.gc();
});
return cache;
};