-
Notifications
You must be signed in to change notification settings - Fork 319
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
a3c0e28
commit 39855a5
Showing
3 changed files
with
139 additions
and
110 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,104 @@ | ||
import NodeCache from "node-cache"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
export class Cache { | ||
private cache: NodeCache; | ||
public cacheDir: string; | ||
|
||
constructor() { | ||
this.cache = new NodeCache({ stdTTL: 300 }); // 5 minutes cache | ||
const __dirname = path.resolve(); | ||
|
||
// Find the 'eliza' folder in the filepath and adjust the cache directory path | ||
const elizaIndex = __dirname.indexOf("eliza"); | ||
if (elizaIndex !== -1) { | ||
const pathToEliza = __dirname.slice(0, elizaIndex + 5); // include 'eliza' | ||
this.cacheDir = path.join(pathToEliza, "cache"); | ||
} else { | ||
this.cacheDir = path.join(__dirname, "cache"); | ||
} | ||
|
||
if (!fs.existsSync(this.cacheDir)) { | ||
fs.mkdirSync(this.cacheDir); | ||
} | ||
} | ||
|
||
private readCacheFromFile<T>(cacheKey: string): T | null { | ||
const filePath = path.join(this.cacheDir, `${cacheKey}.json`); | ||
if (fs.existsSync(filePath)) { | ||
try { | ||
const fileContent = fs.readFileSync(filePath, "utf-8"); | ||
const parsed = JSON.parse(fileContent); | ||
const now = Date.now(); | ||
if (now < parsed.expiry) { | ||
return parsed.data as T; | ||
} else { | ||
fs.unlinkSync(filePath); | ||
} | ||
} catch (error) { | ||
console.error( | ||
`Error reading cache file for key ${cacheKey}:`, | ||
error | ||
); | ||
// Delete corrupted cache file | ||
try { | ||
fs.unlinkSync(filePath); | ||
} catch (e) { | ||
console.error(`Error deleting corrupted cache file:`, e); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private writeCacheToFile<T>(cacheKey: string, data: T): void { | ||
try { | ||
const filePath = path.join(this.cacheDir, `${cacheKey}.json`); | ||
const cacheData = { | ||
data: data, | ||
expiry: Date.now() + 300000, // 5 minutes in milliseconds | ||
}; | ||
fs.writeFileSync(filePath, JSON.stringify(cacheData), "utf-8"); | ||
} catch (error) { | ||
console.error( | ||
`Error writing cache file for key ${cacheKey}:`, | ||
error | ||
); | ||
} | ||
} | ||
|
||
public get<T>(cacheKey: string): T | undefined { | ||
return this.cache.get<T>(cacheKey); | ||
} | ||
|
||
public set<T>(cacheKey: string, data: T): void { | ||
this.cache.set(cacheKey, data); | ||
} | ||
|
||
public getCachedData<T>(cacheKey: string): T | null { | ||
// Check in-memory cache first | ||
const cachedData = this.cache.get<T>(cacheKey); | ||
if (cachedData !== undefined) { | ||
return cachedData; | ||
} | ||
|
||
// Check file-based cache | ||
const fileCachedData = this.readCacheFromFile<T>(cacheKey); | ||
if (fileCachedData) { | ||
// Populate in-memory cache | ||
this.cache.set(cacheKey, fileCachedData); | ||
return fileCachedData; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public setCachedData<T>(cacheKey: string, data: T): void { | ||
// Set in-memory cache | ||
this.cache.set(cacheKey, data); | ||
|
||
// Write to file-based cache | ||
this.writeCacheToFile(cacheKey, data); | ||
} | ||
} |