2021-03-05 17:00:55 +01:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-10-21 11:12:54 +02:00
|
|
|
|
|
|
|
type FindCallback<T> = (value: T) => boolean;
|
2021-03-05 17:00:55 +01:00
|
|
|
/**
|
|
|
|
* Very simple least-recently-used cache implementation
|
|
|
|
* that should be fast enough for very small cache sizes
|
|
|
|
*/
|
2021-10-20 15:24:58 +02:00
|
|
|
export class BaseLRUCache<T> {
|
|
|
|
|
2021-10-21 11:12:54 +02:00
|
|
|
public readonly limit: number;
|
|
|
|
protected _entries: T[];
|
2021-10-20 15:24:58 +02:00
|
|
|
|
|
|
|
constructor(limit: number) {
|
2021-10-21 11:12:54 +02:00
|
|
|
this.limit = limit;
|
2021-03-05 17:00:55 +01:00
|
|
|
this._entries = [];
|
|
|
|
}
|
|
|
|
|
2021-10-20 15:14:17 +02:00
|
|
|
get size() { return this._entries.length; }
|
|
|
|
|
2021-10-21 11:12:54 +02:00
|
|
|
protected _get(findEntryFn: FindCallback<T>) {
|
|
|
|
return this._getByIndexAndMoveUp(this._entries.findIndex(findEntryFn));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _getByIndexAndMoveUp(idx: number) {
|
2021-03-05 17:00:55 +01:00
|
|
|
if (idx !== -1) {
|
|
|
|
const entry = this._entries[idx];
|
|
|
|
// move to top
|
|
|
|
if (idx > 0) {
|
|
|
|
this._entries.splice(idx, 1);
|
|
|
|
this._entries.unshift(entry);
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 11:12:54 +02:00
|
|
|
protected _set(value: T, findEntryFn?: FindCallback<T>) {
|
|
|
|
let indexToRemove = findEntryFn ? this._entries.findIndex(findEntryFn) : -1;
|
2021-03-05 17:00:55 +01:00
|
|
|
this._entries.unshift(value);
|
|
|
|
if (indexToRemove === -1) {
|
2021-10-21 11:12:54 +02:00
|
|
|
if (this._entries.length > this.limit) {
|
2021-03-05 17:00:55 +01:00
|
|
|
indexToRemove = this._entries.length - 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we added the entry at the start since we looked for the index
|
|
|
|
indexToRemove += 1;
|
|
|
|
}
|
|
|
|
if (indexToRemove !== -1) {
|
2021-10-21 11:12:54 +02:00
|
|
|
this.onEvictEntry(this._entries[indexToRemove]);
|
2021-03-05 17:00:55 +01:00
|
|
|
this._entries.splice(indexToRemove, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 11:12:54 +02:00
|
|
|
protected onEvictEntry(entry: T) {}
|
2021-03-05 17:00:55 +01:00
|
|
|
}
|
|
|
|
|
2021-10-20 15:24:58 +02:00
|
|
|
export class LRUCache<T, K> extends BaseLRUCache<T> {
|
|
|
|
private _keyFn: (T) => K;
|
|
|
|
|
|
|
|
constructor(limit, keyFn: (T) => K) {
|
2021-03-05 17:00:55 +01:00
|
|
|
super(limit);
|
|
|
|
this._keyFn = keyFn;
|
|
|
|
}
|
|
|
|
|
2021-10-20 15:24:58 +02:00
|
|
|
get(key: K): T | undefined {
|
2021-03-05 17:00:55 +01:00
|
|
|
return this._get(e => this._keyFn(e) === key);
|
|
|
|
}
|
|
|
|
|
2021-10-20 15:24:58 +02:00
|
|
|
set(value: T) {
|
2021-03-05 17:00:55 +01:00
|
|
|
const key = this._keyFn(value);
|
|
|
|
this._set(value, e => this._keyFn(e) === key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function tests() {
|
2021-10-20 15:24:58 +02:00
|
|
|
interface NameTuple {
|
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
2021-03-05 17:00:55 +01:00
|
|
|
return {
|
|
|
|
"can retrieve added entries": assert => {
|
2021-10-20 15:24:58 +02:00
|
|
|
const cache = new LRUCache<NameTuple, number>(2, e => e.id);
|
2021-03-05 17:00:55 +01:00
|
|
|
cache.set({id: 1, name: "Alice"});
|
|
|
|
cache.set({id: 2, name: "Bob"});
|
2021-10-20 15:24:58 +02:00
|
|
|
assert.equal(cache.get(1)!.name, "Alice");
|
|
|
|
assert.equal(cache.get(2)!.name, "Bob");
|
2021-03-05 17:00:55 +01:00
|
|
|
},
|
|
|
|
"first entry is evicted first": assert => {
|
2021-10-20 15:24:58 +02:00
|
|
|
const cache = new LRUCache<NameTuple, number>(2, e => e.id);
|
2021-03-05 17:00:55 +01:00
|
|
|
cache.set({id: 1, name: "Alice"});
|
|
|
|
cache.set({id: 2, name: "Bob"});
|
|
|
|
cache.set({id: 3, name: "Charly"});
|
|
|
|
assert.equal(cache.get(1), undefined);
|
2021-10-20 15:24:58 +02:00
|
|
|
assert.equal(cache.get(2)!.name, "Bob");
|
|
|
|
assert.equal(cache.get(3)!.name, "Charly");
|
|
|
|
assert.equal(cache.size, 2);
|
2021-03-05 17:00:55 +01:00
|
|
|
},
|
|
|
|
"second entry is evicted if first is requested": assert => {
|
2021-10-20 15:24:58 +02:00
|
|
|
const cache = new LRUCache<NameTuple, number>(2, e => e.id);
|
2021-03-05 17:00:55 +01:00
|
|
|
cache.set({id: 1, name: "Alice"});
|
|
|
|
cache.set({id: 2, name: "Bob"});
|
|
|
|
cache.get(1);
|
|
|
|
cache.set({id: 3, name: "Charly"});
|
2021-10-20 15:24:58 +02:00
|
|
|
assert.equal(cache.get(1)!.name, "Alice");
|
2021-03-05 17:00:55 +01:00
|
|
|
assert.equal(cache.get(2), undefined);
|
2021-10-20 15:24:58 +02:00
|
|
|
assert.equal(cache.get(3)!.name, "Charly");
|
|
|
|
assert.equal(cache.size, 2);
|
2021-03-05 17:00:55 +01:00
|
|
|
},
|
|
|
|
"setting an entry twice removes the first": assert => {
|
2021-10-20 15:24:58 +02:00
|
|
|
const cache = new LRUCache<NameTuple, number>(2, e => e.id);
|
2021-03-05 17:00:55 +01:00
|
|
|
cache.set({id: 1, name: "Alice"});
|
|
|
|
cache.set({id: 2, name: "Bob"});
|
|
|
|
cache.set({id: 1, name: "Al Ice"});
|
|
|
|
cache.set({id: 3, name: "Charly"});
|
2021-10-20 15:24:58 +02:00
|
|
|
assert.equal(cache.get(1)!.name, "Al Ice");
|
2021-03-05 17:00:55 +01:00
|
|
|
assert.equal(cache.get(2), undefined);
|
2021-10-20 15:24:58 +02:00
|
|
|
assert.equal(cache.get(3)!.name, "Charly");
|
|
|
|
assert.equal(cache.size, 2);
|
2021-03-05 17:00:55 +01:00
|
|
|
},
|
|
|
|
"evict callback is called": assert => {
|
|
|
|
let evictions = 0;
|
2021-10-20 15:24:58 +02:00
|
|
|
class CustomCache extends LRUCache<NameTuple, number> {
|
2021-10-22 17:30:20 +02:00
|
|
|
onEvictEntry(entry) {
|
2021-03-05 17:00:55 +01:00
|
|
|
assert.equal(entry.name, "Alice");
|
|
|
|
evictions += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const cache = new CustomCache(2, e => e.id);
|
|
|
|
cache.set({id: 1, name: "Alice"});
|
|
|
|
cache.set({id: 2, name: "Bob"});
|
|
|
|
cache.set({id: 3, name: "Charly"});
|
|
|
|
assert.equal(evictions, 1);
|
|
|
|
},
|
|
|
|
"evict callback is called when replacing entry with same identity": assert => {
|
|
|
|
let evictions = 0;
|
2021-10-20 15:24:58 +02:00
|
|
|
class CustomCache extends LRUCache<NameTuple, number> {
|
2021-10-22 17:30:20 +02:00
|
|
|
onEvictEntry(entry) {
|
2021-03-05 17:00:55 +01:00
|
|
|
assert.equal(entry.name, "Alice");
|
|
|
|
evictions += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const cache = new CustomCache(2, e => e.id);
|
|
|
|
cache.set({id: 1, name: "Alice"});
|
|
|
|
cache.set({id: 1, name: "Bob"});
|
|
|
|
assert.equal(evictions, 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|