Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: switch to local custom LRUCache #3388

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@
"gray-matter": "^4.0.3",
"lint-staged": "^15.2.0",
"lodash.template": "^4.5.0",
"lru-cache": "^10.1.0",
"markdown-it": "^14.0.0",
"markdown-it-anchor": "^8.6.7",
"markdown-it-attrs": "^4.1.6",
Expand Down
11 changes: 4 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/client/theme-default/components/VPLocalSearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
import type { ModalTranslations } from '../../../../types/local-search'
import { pathToFile } from '../../app/utils'
import { useData } from '../composables/data'
import { LRUCache } from '../support/lru'
import { LRUCache } from '../../shared'
import { createSearchTranslate } from '../support/translation'

const emit = defineEmits<{
Expand Down
37 changes: 0 additions & 37 deletions src/client/theme-default/support/lru.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { resolveTitleFromToken } from '@mdit-vue/shared'
import _debug from 'debug'
import fs from 'fs-extra'
import { LRUCache } from 'lru-cache'
import path from 'path'
import type { SiteConfig } from './config'
import {
Expand All @@ -12,6 +11,7 @@ import {
import {
EXTERNAL_URL_RE,
slash,
LRUCache,
type HeadConfig,
type MarkdownEnv,
type PageData
Expand All @@ -20,7 +20,7 @@ import { getGitTimestamp } from './utils/getGitTimestamp'
import { processIncludes } from './utils/processIncludes'

const debug = _debug('vitepress:md')
const cache = new LRUCache<string, MarkdownCompileResult>({ max: 1024 })
const cache = new LRUCache<string, MarkdownCompileResult>(1024)

export interface MarkdownCompileResult {
vueSrc: string
Expand Down
55 changes: 55 additions & 0 deletions src/shared/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,58 @@ export function sanitizeFileName(name: string): string {
export function slash(p: string): string {
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()
}
}