概念定义
内核把空闲内存变成文件内容的透明缓存。应用无需改代码就获得读加速与异步写——同时继承“不知道哪些字节真正落盘”的含糊性。
工作机制
读取填充页面;脏页按阈值与时间由回写线程刷盘。fsync 冲刷文件并在设备确认后才返回。回收时干净页即刻丢弃、脏页必须先写,所以脏页比例就是压力表。mmap 直接映射页面并与缓存共享。
使用场景
数据库与队列的持久化设计:没有对页缓存行为的理解,WAL 加 fsync 纪律形同虚设。容量规划:缓存大小决定重复读性能。容器内存:页缓存记账会让宿主与编排器大吃一惊。
常见误区
write() 返回不等于数据持久:它只是拷进页缓存,fsync 或同步标志才推到存储。页缓存是内核全局的——别的进程与内存回收都能赶走你的热页。进程崩溃时页缓存还在,写而未 fsync 的数据幸存;掉电或内核崩溃则不然——这条分界线定义了持久化系统。突发期的脏页爆炸会让回写把写入拖停数秒;Direct I/O 绕开缓存,忘掉这点就丢了应用期待的双缓冲收益。
代码示例
class CacheNode<K, V> {
public previous: CacheNode<K, V> | null = null;
public next: CacheNode<K, V> | null = null;
public constructor(
public readonly key: K,
public value: V,
public frequency: number,
) {}
}
class FrequencyList<K, V> {
private head: CacheNode<K, V> | null = null;
private tail: CacheNode<K, V> | null = null;
public size = 0;
public addFront(node: CacheNode<K, V>): void {
node.previous = null;
node.next = this.head;
if (this.head !== null) this.head.previous = node;
this.head = node;
if (this.tail === null) this.tail = node;
this.size += 1;
}
public remove(node: CacheNode<K, V>): void {
if (node.previous !== null) node.previous.next = node.next;
else this.head = node.next;
if (node.next !== null) node.next.previous = node.previous;
else this.tail = node.previous;
node.previous = null;
node.next = null;
this.size -= 1;
}
public removeLast(): CacheNode<K, V> | null {
if (this.tail === null) return null;
const node = this.tail;
this.remove(node);
return node;
}
}
export class LfuCache<K, V> {
private readonly entries = new Map<K, CacheNode<K, V>>();
private readonly frequencies = new Map<number, FrequencyList<K, V>>();
private lowestFrequency = 0;
public constructor(private readonly capacity: number) {
if (!Number.isInteger(capacity) || capacity < 1) {
throw new RangeError("capacity must be a positive integer");
}
}
public get size(): number {
return this.entries.size;
}
public get(key: K): V | undefined {
const node = this.entries.get(key);
if (node === undefined) return undefined;
this.touch(node);
return node.value;
}
public peek(key: K): V | undefined {
return this.entries.get(key)?.value;
}
public put(key: K, value: V): this {
const existing = this.entries.get(key);
if (existing !== undefined) {
existing.value = value;
this.touch(existing);
return this;
}
if (this.entries.size >= this.capacity) this.evict();
const node = new CacheNode(key, value, 1);
this.entries.set(key, node);
this.listFor(1).addFront(node);
this.lowestFrequency = 1;
return this;
}
public delete(key: K): boolean {
const node = this.entries.get(key);
if (node === undefined) return false;
this.removeNode(node);
return true;
}
private touch(node: CacheNode<K, V>): void {
const oldFrequency = node.frequency;
const oldList = this.frequencies.get(oldFrequency);
if (oldList === undefined) {
throw new Error("cache invariant violated");
}
oldList.remove(node);
if (oldList.size === 0) {
this.frequencies.delete(oldFrequency);
if (this.lowestFrequency === oldFrequency) {
this.lowestFrequency = oldFrequency + 1;
}
}
node.frequency = oldFrequency + 1;
this.listFor(node.frequency).addFront(node);
}
private evict(): void {
const list = this.frequencies.get(this.lowestFrequency);
if (list === undefined) return;
const node = list.removeLast();
if (node === null) return;
this.entries.delete(node.key);
if (list.size === 0) this.frequencies.delete(this.lowestFrequency);
}
private removeNode(node: CacheNode<K, V>): void {
const list = this.frequencies.get(node.frequency);
if (list === undefined) {
throw new Error("cache invariant violated");
}
list.remove(node);
this.entries.delete(node.key);
if (list.size === 0) this.frequencies.delete(node.frequency);
}
private listFor(frequency: number): FrequencyList<K, V> {
let list = this.frequencies.get(frequency);
if (list === undefined) {
list = new FrequencyList<K, V>();
this.frequencies.set(frequency, list);
}
return list;
}
}
const cache = new LfuCache<string, number>(2);
cache.put("a", 1).put("b", 2);
console.log(cache.get("a"));
cache.put("c", 3);
console.log(cache.get("b"), cache.peek("a"), cache.get("c"));
cache.put("d", 4);
console.log(cache.get("a"), cache.get("c"), cache.get("d"));
console.log(cache.size);