2021-03-05 21:30:55 +05:30
|
|
|
/*
|
|
|
|
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 14:42:54 +05:30
|
|
|
|
|
|
|
type FindCallback<T> = (value: T) => boolean;
|
2021-03-05 21:30:55 +05:30
|
|
|
/**
|
|
|
|
* Very simple least-recently-used cache implementation
|
|
|
|
* that should be fast enough for very small cache sizes
|
|
|
|
*/
|
2021-10-20 18:54:58 +05:30
|
|
|
export class BaseLRUCache<T> {
|
|
|
|
|
2021-10-21 14:42:54 +05:30
|
|
|
public readonly limit: number;
|
|
|
|
protected _entries: T[];
|
2021-10-20 18:54:58 +05:30
|
|
|
|
|
|
|
constructor(limit: number) {
|
2021-10-21 14:42:54 +05:30
|
|
|
this.limit = limit;
|
2021-03-05 21:30:55 +05:30
|
|
|
this._entries = [];
|
|
|
|
}
|
|
|
|
|
2021-10-20 18:44:17 +05:30
|
|
|
get size() { return this._entries.length; }
|
|
|
|
|
2021-10-21 14:42:54 +05:30
|
|
|
protected _get(findEntryFn: FindCallback<T>) {
|
|
|
|
return this._getByIndexAndMoveUp(this._entries.findIndex(findEntryFn));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _getByIndexAndMoveUp(idx: number) {
|
2021-03-05 21:30:55 +05:30
|
|
|
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 14:42:54 +05:30
|
|
|
protected _set(value: T, findEntryFn?: FindCallback<T>) {
|
|
|
|
let indexToRemove = findEntryFn ? this._entries.findIndex(findEntryFn) : -1;
|
2021-03-05 21:30:55 +05:30
|
|
|
this._entries.unshift(value);
|
|
|
|
if (indexToRemove === -1) {
|
2021-10-21 14:42:54 +05:30
|
|
|
if (this._entries.length > this.limit) {
|
2021-03-05 21:30:55 +05:30
|
|
|
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 14:42:54 +05:30
|
|
|
this.onEvictEntry(this._entries[indexToRemove]);
|
2021-03-05 21:30:55 +05:30
|
|
|
this._entries.splice(indexToRemove, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:42:54 +05:30
|
|
|
protected onEvictEntry(entry: T) {}
|
2021-03-05 21:30:55 +05:30
|
|
|
}
|
|
|
|
|
2021-10-20 18:54:58 +05:30
|
|
|
export class LRUCache<T, K> extends BaseLRUCache<T> {
|
|
|
|
private _keyFn: (T) => K;
|
|
|
|
|
|
|
|
constructor(limit, keyFn: (T) => K) {
|
2021-03-05 21:30:55 +05:30
|
|
|
super(limit);
|
|
|
|
this._keyFn = keyFn;
|
|
|
|
}
|
|
|
|
|
2021-10-20 18:54:58 +05:30
|
|
|
get(key: K): T | undefined {
|
2021-03-05 21:30:55 +05:30
|
|
|
return this._get(e => this._keyFn(e) === key);
|
|
|
|
}
|
|
|
|
|
2021-10-20 18:54:58 +05:30
|
|
|
set(value: T) {
|
2021-03-05 21:30:55 +05:30
|
|
|
const key = this._keyFn(value);
|
|
|
|
this._set(value, e => this._keyFn(e) === key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function tests() {
|
2021-10-20 18:54:58 +05:30
|
|
|
interface NameTuple {
|
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
2021-03-05 21:30:55 +05:30
|
|
|
return {
|
|
|
|
"can retrieve added entries": assert => {
|
2021-10-20 18:54:58 +05:30
|
|
|
const cache = new LRUCache<NameTuple, number>(2, e => e.id);
|
2021-03-05 21:30:55 +05:30
|
|
|
cache.set({id: 1, name: "Alice"});
|
|
|
|
cache.set({id: 2, name: "Bob"});
|
2021-10-20 18:54:58 +05:30
|
|
|
assert.equal(cache.get(1)!.name, "Alice");
|
|
|
|
assert.equal(cache.get(2)!.name, "Bob");
|
2021-03-05 21:30:55 +05:30
|
|
|
},
|
|
|
|
"first entry is evicted first": assert => {
|
2021-10-20 18:54:58 +05:30
|
|
|
const cache = new LRUCache<NameTuple, number>(2, e => e.id);
|
2021-03-05 21:30:55 +05:30
|
|
|
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 18:54:58 +05:30
|
|
|
assert.equal(cache.get(2)!.name, "Bob");
|
|
|
|
assert.equal(cache.get(3)!.name, "Charly");
|
|
|
|
assert.equal(cache.size, 2);
|
2021-03-05 21:30:55 +05:30
|
|
|
},
|
|
|
|
"second entry is evicted if first is requested": assert => {
|
2021-10-20 18:54:58 +05:30
|
|
|
const cache = new LRUCache<NameTuple, number>(2, e => e.id);
|
2021-03-05 21:30:55 +05:30
|
|
|
cache.set({id: 1, name: "Alice"});
|
|
|
|
cache.set({id: 2, name: "Bob"});
|
|
|
|
cache.get(1);
|
|
|
|
cache.set({id: 3, name: "Charly"});
|
2021-10-20 18:54:58 +05:30
|
|
|
assert.equal(cache.get(1)!.name, "Alice");
|
2021-03-05 21:30:55 +05:30
|
|
|
assert.equal(cache.get(2), undefined);
|
2021-10-20 18:54:58 +05:30
|
|
|
assert.equal(cache.get(3)!.name, "Charly");
|
|
|
|
assert.equal(cache.size, 2);
|
2021-03-05 21:30:55 +05:30
|
|
|
},
|
|
|
|
"setting an entry twice removes the first": assert => {
|
2021-10-20 18:54:58 +05:30
|
|
|
const cache = new LRUCache<NameTuple, number>(2, e => e.id);
|
2021-03-05 21:30:55 +05:30
|
|
|
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 18:54:58 +05:30
|
|
|
assert.equal(cache.get(1)!.name, "Al Ice");
|
2021-03-05 21:30:55 +05:30
|
|
|
assert.equal(cache.get(2), undefined);
|
2021-10-20 18:54:58 +05:30
|
|
|
assert.equal(cache.get(3)!.name, "Charly");
|
|
|
|
assert.equal(cache.size, 2);
|
2021-03-05 21:30:55 +05:30
|
|
|
},
|
|
|
|
"evict callback is called": assert => {
|
|
|
|
let evictions = 0;
|
2021-10-20 18:54:58 +05:30
|
|
|
class CustomCache extends LRUCache<NameTuple, number> {
|
2021-10-22 21:00:20 +05:30
|
|
|
onEvictEntry(entry) {
|
2021-03-05 21:30:55 +05:30
|
|
|
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 18:54:58 +05:30
|
|
|
class CustomCache extends LRUCache<NameTuple, number> {
|
2021-10-22 21:00:20 +05:30
|
|
|
onEvictEntry(entry) {
|
2021-03-05 21:30:55 +05:30
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|