refactor: switch to local custom LRUCache

pull/3388/head
zonemeen 2 years ago
parent 9c20e3b5f8
commit aedcd0ba61

@ -162,7 +162,6 @@
"gray-matter": "^4.0.3", "gray-matter": "^4.0.3",
"lint-staged": "^15.2.0", "lint-staged": "^15.2.0",
"lodash.template": "^4.5.0", "lodash.template": "^4.5.0",
"lru-cache": "^10.1.0",
"markdown-it": "^14.0.0", "markdown-it": "^14.0.0",
"markdown-it-anchor": "^8.6.7", "markdown-it-anchor": "^8.6.7",
"markdown-it-attrs": "^4.1.6", "markdown-it-attrs": "^4.1.6",

@ -1,9 +1,5 @@
lockfileVersion: '6.0' lockfileVersion: '6.0'
settings:
autoInstallPeers: false
excludeLinksFromLockfile: false
overrides: overrides:
ora>string-width: ^5 ora>string-width: ^5
@ -186,9 +182,6 @@ importers:
lodash.template: lodash.template:
specifier: ^4.5.0 specifier: ^4.5.0
version: 4.5.0 version: 4.5.0
lru-cache:
specifier: ^10.1.0
version: 10.1.0
markdown-it: markdown-it:
specifier: ^14.0.0 specifier: ^14.0.0
version: 14.0.0 version: 14.0.0
@ -4758,3 +4751,7 @@ packages:
resolution: {integrity: sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==} resolution: {integrity: sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==}
engines: {node: '>=12.20'} engines: {node: '>=12.20'}
dev: true dev: true
settings:
autoInstallPeers: false
excludeLinksFromLockfile: false

@ -29,7 +29,7 @@ import {
import type { ModalTranslations } from '../../../../types/local-search' import type { ModalTranslations } from '../../../../types/local-search'
import { pathToFile } from '../../app/utils' import { pathToFile } from '../../app/utils'
import { useData } from '../composables/data' import { useData } from '../composables/data'
import { LRUCache } from '../support/lru' import { LRUCache } from '../../shared'
import { createSearchTranslate } from '../support/translation' import { createSearchTranslate } from '../support/translation'
const emit = defineEmits<{ const emit = defineEmits<{

@ -1,37 +0,0 @@
// adapted from https://stackoverflow.com/a/46432113/11613622
export class LRUCache<K, V> {
private max: number
private cache: Map<K, V>
constructor(max: number = 10) {
this.max = max
this.cache = new Map<K, V>()
}
get(key: K): V | undefined {
let item = this.cache.get(key)
if (item !== undefined) {
// refresh key
this.cache.delete(key)
this.cache.set(key, item)
}
return item
}
set(key: K, val: V): void {
// refresh key
if (this.cache.has(key)) this.cache.delete(key)
// evict oldest
else if (this.cache.size === this.max) this.cache.delete(this.first()!)
this.cache.set(key, val)
}
first(): K | undefined {
return this.cache.keys().next().value
}
clear(): void {
this.cache.clear()
}
}

@ -1,7 +1,6 @@
import { resolveTitleFromToken } from '@mdit-vue/shared' import { resolveTitleFromToken } from '@mdit-vue/shared'
import _debug from 'debug' import _debug from 'debug'
import fs from 'fs-extra' import fs from 'fs-extra'
import { LRUCache } from 'lru-cache'
import path from 'path' import path from 'path'
import type { SiteConfig } from './config' import type { SiteConfig } from './config'
import { import {
@ -12,6 +11,7 @@ import {
import { import {
EXTERNAL_URL_RE, EXTERNAL_URL_RE,
slash, slash,
LRUCache,
type HeadConfig, type HeadConfig,
type MarkdownEnv, type MarkdownEnv,
type PageData type PageData
@ -20,7 +20,7 @@ import { getGitTimestamp } from './utils/getGitTimestamp'
import { processIncludes } from './utils/processIncludes' import { processIncludes } from './utils/processIncludes'
const debug = _debug('vitepress:md') const debug = _debug('vitepress:md')
const cache = new LRUCache<string, MarkdownCompileResult>({ max: 1024 }) const cache = new LRUCache<string, MarkdownCompileResult>(1024)
export interface MarkdownCompileResult { export interface MarkdownCompileResult {
vueSrc: string vueSrc: string

@ -174,3 +174,58 @@ export function sanitizeFileName(name: string): string {
export function slash(p: string): string { export function slash(p: string): string {
return p.replace(/\\/g, '/') return p.replace(/\\/g, '/')
} }
// adapted from https://stackoverflow.com/a/46432113/11613622
export class LRUCache<K, V> {
private max: number
private cache: Map<K, V>
constructor(max: number = 10) {
this.max = max
this.cache = new Map<K, V>()
}
get(key: K): V | undefined {
let item = this.cache.get(key)
if (item !== undefined) {
// refresh key
this.cache.delete(key)
this.cache.set(key, item)
}
return item
}
set(key: K, val: V): void {
// refresh key
if (this.cache.has(key)) this.cache.delete(key)
// evict oldest
else if (this.cache.size === this.max) this.cache.delete(this.first()!)
this.cache.set(key, val)
}
first(): K | undefined {
return this.cache.keys().next().value
}
find(fn: (v: V, k: K, self: LRUCache<K, V>) => boolean): V | undefined {
for (const [key, value] of this.cache.entries()) {
if (fn(value, key, this)) {
return value
}
}
return undefined
}
delete(key: K): boolean {
if (this.cache.has(key)) {
const deleted = this.cache.delete(key)
return deleted
}
return false
}
clear(): void {
this.cache.clear()
}
}

Loading…
Cancel
Save