generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd1ac68
commit 3e62ec7
Showing
124 changed files
with
5,786 additions
and
606 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { LFUCache } from "@/cache/strategy/LFUCache"; | ||
import { IProvider } from "@/providers/IProvider"; | ||
import { AnyResult } from "@/results/AnyResult"; | ||
import { fastHash } from "@/utilities/FastHash"; | ||
|
||
export class ResultCache { | ||
private lfuCache: LFUCache<number, [IProvider, AnyResult]>; | ||
|
||
constructor(capacity: number) { | ||
this.lfuCache = new LFUCache<number, [IProvider, AnyResult]>(capacity); | ||
} | ||
|
||
get(expression: string): [IProvider, AnyResult] | undefined { | ||
const hashedKey = fastHash(expression); | ||
return this.lfuCache.get(hashedKey); | ||
} | ||
|
||
set( | ||
expression: string, | ||
value: [IProvider, AnyResult] | ||
): [IProvider, AnyResult] { | ||
const hashedKey = fastHash(expression); | ||
|
||
this.lfuCache.set(hashedKey, value); | ||
|
||
return value; | ||
} | ||
|
||
clear() { | ||
this.lfuCache.clear(); | ||
} | ||
} | ||
|
||
export const GlobalResultCache = new ResultCache(200); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
export class LFUCache<K, V> { | ||
private capacity: number; | ||
private cache: Map<K, { value: V; frequency: number; order: number }>; | ||
private frequencies: Map<number, Set<K>>; | ||
private minFrequency: number; | ||
private order: number; | ||
|
||
constructor(capacity: number) { | ||
this.capacity = capacity; | ||
this.cache = new Map(); | ||
this.frequencies = new Map(); | ||
this.minFrequency = 0; | ||
this.order = 0; | ||
} | ||
|
||
private updateFrequency(key: K): void { | ||
const item = this.cache.get(key); | ||
|
||
if (!item) return; | ||
|
||
const oldFreq = item.frequency; | ||
|
||
item.frequency++; | ||
item.order = this.order++; | ||
|
||
const oldSet = this.frequencies.get(oldFreq); | ||
oldSet?.delete(key); | ||
|
||
if (oldSet && oldSet.size === 0) { | ||
this.frequencies.delete(oldFreq); | ||
|
||
if (oldFreq === this.minFrequency) { | ||
this.minFrequency++; | ||
} | ||
} | ||
|
||
let newSet = this.frequencies.get(item.frequency); | ||
|
||
if (!newSet) { | ||
newSet = new Set(); | ||
this.frequencies.set(item.frequency, newSet); | ||
} | ||
|
||
newSet.add(key); | ||
} | ||
|
||
public get(key: K): V | undefined { | ||
const item = this.cache.get(key); | ||
|
||
if (item) { | ||
this.updateFrequency(key); | ||
return item.value; | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
public set(key: K, value: V): void { | ||
const item = this.cache.get(key); | ||
|
||
if (item) { | ||
item.value = value; | ||
this.updateFrequency(key); | ||
return; | ||
} | ||
|
||
if (this.cache.size === this.capacity) { | ||
const leastUsedSet = this.frequencies.get(this.minFrequency); | ||
|
||
if (leastUsedSet) { | ||
let oldestKey = null; | ||
let oldestOrder = Infinity; | ||
|
||
for (const k of leastUsedSet) { | ||
const order = this.cache.get(k)?.order ?? Infinity; | ||
|
||
if (order < oldestOrder) { | ||
oldestOrder = order; | ||
oldestKey = k; | ||
} | ||
} | ||
|
||
if (oldestKey) { | ||
leastUsedSet.delete(oldestKey); | ||
this.cache.delete(oldestKey); | ||
} | ||
} | ||
} | ||
|
||
this.cache.set(key, { value, frequency: 1, order: this.order++ }); | ||
this.minFrequency = 1; | ||
|
||
let freqSet = this.frequencies.get(1); | ||
|
||
if (!freqSet) { | ||
freqSet = new Set(); | ||
this.frequencies.set(1, freqSet); | ||
} | ||
|
||
freqSet.add(key); | ||
} | ||
|
||
public clear(): void { | ||
this.cache.clear(); | ||
this.frequencies.clear(); | ||
this.minFrequency = 0; | ||
this.order = 0; | ||
} | ||
} |
Oops, something went wrong.