frida-java-bridge
Version:
Java runtime interop from Frida
45 lines (36 loc) • 963 B
JavaScript
// Based on https://stackoverflow.com/a/46432113
export default class LRU {
constructor (capacity, destroy) {
this.items = new Map();
this.capacity = capacity;
this.destroy = destroy;
}
dispose (env) {
const { items, destroy } = this;
items.forEach(val => { destroy(val, env); });
items.clear();
}
get (key) {
const { items } = this;
const item = items.get(key);
if (item !== undefined) {
items.delete(key);
items.set(key, item);
}
return item;
}
set (key, val, env) {
const { items } = this;
const existingVal = items.get(key);
if (existingVal !== undefined) {
items.delete(key);
this.destroy(existingVal, env);
} else if (items.size === this.capacity) {
const oldestKey = items.keys().next().value;
const oldestVal = items.get(oldestKey);
items.delete(oldestKey);
this.destroy(oldestVal, env);
}
items.set(key, val);
}
}