Files
chartdb/src/lib/data/export-metadata/export-sql-cache.ts
Jonathan Fishner e5dbbf2eaa fix(AI exports): add cahching layer to SQL exports (#390)
* fix(AI exports): add cahching layer to SQL exports

* remove logs

---------

Co-authored-by: Guy Ben-Aharon <baguy3@gmail.com>
2024-11-13 17:18:27 +02:00

28 lines
772 B
TypeScript

import type { DatabaseType } from '@/lib/domain/database-type';
import { sha256 } from '@/lib/utils';
export const getFromCache = (key: string): string | null => {
try {
return localStorage.getItem(`sql-export-${key}`);
} catch (e) {
console.warn('Failed to read from localStorage:', e);
return null;
}
};
export const setInCache = (key: string, value: string): void => {
try {
localStorage.setItem(`sql-export-${key}`, value);
} catch (e) {
console.warn('Failed to write to localStorage:', e);
}
};
export const generateCacheKey = async (
databaseType: DatabaseType,
sqlScript: string
): Promise<string> => {
const rawKey = `${databaseType}:${sqlScript}`;
return await sha256(rawKey);
};