mongo-bolt
Version:
A lightweight, beginner-friendly MongoDB wrapper with inbuilt caching and simplified joins/indexing.
72 lines (71 loc) • 2.18 kB
JavaScript
class LinkedListNode {
constructor(key, value) {
this.key = key;
this.value = value;
this.next = null;
this.prev = null;
}
}
class CacheManager {
constructor(maxsize = 100) {
this.dllhead = new LinkedListNode("head", "head");
this.dlltail = new LinkedListNode("tail", "tail");
this.maxSize = 100;
this.dllhead.next = this.dlltail;
this.dlltail.prev = this.dllhead;
this.maxSize = maxsize;
this.cache = new Map();
}
set(key, value) {
if (this.cache.size >= this.maxSize) {
this.remove();
}
var node = new LinkedListNode(key, value);
this.cache.set(key, node);
this.dllhead.next = node;
node.prev = this.dllhead;
node.next = this.dlltail;
this.dlltail.prev = node;
}
get(key) {
var node = this.cache.get(key);
if (node === undefined || node === null)
return null;
node.prev.next = node.next;
node.next.prev = node.prev;
node.prev = this.dllhead;
node.next = this.dllhead.next;
this.dllhead.next.prev = node;
this.dllhead.next = node;
return node.value;
}
has(key) {
return this.cache.has(key);
}
delete(key) {
var nodeToBeDeleted = this.cache.get(key);
nodeToBeDeleted.prev.next = nodeToBeDeleted.next;
nodeToBeDeleted.next.prev = nodeToBeDeleted.prev;
nodeToBeDeleted.prev = null;
nodeToBeDeleted.next = null;
return this.cache.delete(key);
}
remove() {
if (this.cache.size === 0)
return;
var nodeToBeDeleted = this.dlltail.prev;
if (nodeToBeDeleted === this.dllhead)
return;
nodeToBeDeleted.prev.next = this.dlltail;
nodeToBeDeleted.next.prev = nodeToBeDeleted.prev;
nodeToBeDeleted.prev = null;
nodeToBeDeleted.next = null;
this.cache.delete(nodeToBeDeleted.key);
}
clear() {
this.cache.clear();
this.dllhead.next = this.dlltail;
this.dlltail.prev = this.dllhead;
}
}
export default CacheManager;