Skip to content

Commit

Permalink
1.0.0 (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamRiddell authored Apr 29, 2024
1 parent fd1ac68 commit 3e62ec7
Show file tree
Hide file tree
Showing 124 changed files with 5,786 additions and 606 deletions.
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "solve",
"name": "Solve",
"version": "0.9.0",
"version": "1.0.0",
"minAppVersion": "0.15.0",
"description": "Supercharge your notes in Obsdian with real-time calculations without AI fuss. From date magic ('Now + 20 days') to arithmetic flair ('10 + 5').",
"author": "Liam Riddell",
Expand All @@ -11,4 +11,4 @@
"GitHub Sponser": "https://github.com/sponsors/LiamRiddell"
},
"isDesktopOnly": false
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solve",
"version": "0.9.0",
"version": "1.0.0",
"description": "Supercharge your notes in Obsdian with real-time calculations without AI fuss. From date magic ('Now + 20 days') to arithmetic flair ('10 + 5').",
"main": "main.js",
"scripts": {
Expand Down
34 changes: 34 additions & 0 deletions src/cache/ResultCache.ts
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);
109 changes: 109 additions & 0 deletions src/cache/strategy/LFUCache.ts
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;
}
}
Loading

0 comments on commit 3e62ec7

Please sign in to comment.