Slight adjustment to rpc-provider LRU (#4198)
This commit is contained in:
@@ -11,6 +11,7 @@ describe('LRUCache', (): void => {
|
||||
keys.forEach((k) => lru.set(k, `${k}${k}${k}`));
|
||||
|
||||
expect(lru.keys().join(', ')).toEqual(keys.reverse().join(', '));
|
||||
expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true);
|
||||
|
||||
keys.forEach((k) => expect(lru.get(k)).toEqual(`${k}${k}${k}`));
|
||||
});
|
||||
@@ -22,6 +23,7 @@ describe('LRUCache', (): void => {
|
||||
keys.forEach((k) => lru.set(k, `${k}${k}${k}`));
|
||||
|
||||
expect(lru.keys().join(', ')).toEqual(keys.slice(2).reverse().join(', '));
|
||||
expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true);
|
||||
|
||||
keys.slice(2).forEach((k) => expect(lru.get(k)).toEqual(`${k}${k}${k}`));
|
||||
});
|
||||
@@ -33,17 +35,21 @@ describe('LRUCache', (): void => {
|
||||
keys.forEach((k) => lru.set(k, `${k}${k}${k}`));
|
||||
|
||||
expect(lru.entries()).toEqual([['5', '555'], ['4', '444'], ['3', '333'], ['2', '222']]);
|
||||
expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true);
|
||||
|
||||
lru.get('3');
|
||||
|
||||
expect(lru.entries()).toEqual([['3', '333'], ['5', '555'], ['4', '444'], ['2', '222']]);
|
||||
expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true);
|
||||
|
||||
lru.set('4', '4433');
|
||||
|
||||
expect(lru.entries()).toEqual([['4', '4433'], ['3', '333'], ['5', '555'], ['2', '222']]);
|
||||
expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true);
|
||||
|
||||
lru.set('6', '666');
|
||||
|
||||
expect(lru.entries()).toEqual([['6', '666'], ['4', '4433'], ['3', '333'], ['5', '555']]);
|
||||
expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -22,17 +22,26 @@ export class LRUCache {
|
||||
readonly #refs: Map<string, LRUNode> = new Map();
|
||||
|
||||
#length = 0;
|
||||
#head: LRUNode = new LRUNode('head');
|
||||
#tail: LRUNode = new LRUNode('tail');
|
||||
#head: LRUNode;
|
||||
#tail: LRUNode;
|
||||
|
||||
constructor (capacity = DEFAULT_CAPACITY) {
|
||||
this.capacity = capacity;
|
||||
this.#head = this.#tail = new LRUNode('<empty>');
|
||||
}
|
||||
|
||||
get length (): number {
|
||||
return this.#length;
|
||||
}
|
||||
|
||||
get lengthData (): number {
|
||||
return this.#data.size;
|
||||
}
|
||||
|
||||
get lengthRefs (): number {
|
||||
return this.#refs.size;
|
||||
}
|
||||
|
||||
entries (): [string, unknown][] {
|
||||
const keys = this.keys();
|
||||
const entries = new Array<[string, unknown]>(keys.length);
|
||||
@@ -49,7 +58,7 @@ export class LRUCache {
|
||||
keys (): string[] {
|
||||
const keys: string[] = [];
|
||||
|
||||
if (this.length) {
|
||||
if (this.#length) {
|
||||
let curr = this.#head;
|
||||
|
||||
while (curr !== this.#tail) {
|
||||
@@ -67,7 +76,7 @@ export class LRUCache {
|
||||
const data = this.#data.get(key);
|
||||
|
||||
if (data) {
|
||||
this.#toFront(key);
|
||||
this.#toHead(key);
|
||||
|
||||
return data as T;
|
||||
}
|
||||
@@ -77,7 +86,7 @@ export class LRUCache {
|
||||
|
||||
set <T> (key: string, value: T): void {
|
||||
if (this.#data.has(key)) {
|
||||
this.#toFront(key);
|
||||
this.#toHead(key);
|
||||
} else {
|
||||
const node = new LRUNode(key);
|
||||
|
||||
@@ -96,7 +105,6 @@ export class LRUCache {
|
||||
this.#refs.delete(this.#tail.key);
|
||||
|
||||
this.#tail = this.#tail.prev;
|
||||
this.#tail.next = this.#head;
|
||||
} else {
|
||||
this.#length += 1;
|
||||
}
|
||||
@@ -105,7 +113,7 @@ export class LRUCache {
|
||||
this.#data.set(key, value);
|
||||
}
|
||||
|
||||
#toFront (key: string): void {
|
||||
#toHead (key: string): void {
|
||||
const ref = this.#refs.get(key);
|
||||
|
||||
if (ref && ref !== this.#head) {
|
||||
|
||||
Reference in New Issue
Block a user