mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
Minor changes to fs cache
This commit is contained in:
parent
65118d3100
commit
8428b3da42
@ -190,6 +190,7 @@ export const buildSpeedtestDomainSet = task(import.meta.path, async (span) => {
|
|||||||
'Tokyo',
|
'Tokyo',
|
||||||
'Singapore',
|
'Singapore',
|
||||||
'Korea',
|
'Korea',
|
||||||
|
'Seoul',
|
||||||
'Canada',
|
'Canada',
|
||||||
'Toronto',
|
'Toronto',
|
||||||
'Montreal',
|
'Montreal',
|
||||||
|
|||||||
@ -15,19 +15,20 @@ const enum CacheStatus {
|
|||||||
Miss = 'miss'
|
Miss = 'miss'
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CacheOptions {
|
export interface CacheOptions<S = string> {
|
||||||
/** Path to sqlite file dir */
|
/** Path to sqlite file dir */
|
||||||
cachePath?: string,
|
cachePath?: string,
|
||||||
/** Time before deletion */
|
/** Time before deletion */
|
||||||
tbd?: number,
|
tbd?: number,
|
||||||
/** Cache table name */
|
/** Cache table name */
|
||||||
tableName?: string
|
tableName?: string,
|
||||||
|
type?: S extends string ? 'string' : 'buffer'
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CacheApplyNonStringOption<T> {
|
interface CacheApplyNonStringOption<T, S = string> {
|
||||||
ttl?: number | null,
|
ttl?: number | null,
|
||||||
serializer: (value: T) => string,
|
serializer: (value: T) => S,
|
||||||
deserializer: (cached: string) => T,
|
deserializer: (cached: S) => T,
|
||||||
temporaryBypass?: boolean
|
temporaryBypass?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +37,7 @@ interface CacheApplyStringOption {
|
|||||||
temporaryBypass?: boolean
|
temporaryBypass?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
type CacheApplyOption<T> = T extends string ? CacheApplyStringOption : CacheApplyNonStringOption<T>;
|
type CacheApplyOption<T, S = string> = T extends string ? CacheApplyStringOption : CacheApplyNonStringOption<T, S>;
|
||||||
|
|
||||||
const randomInt = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min;
|
const randomInt = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min;
|
||||||
|
|
||||||
@ -62,7 +63,7 @@ export const TTL = {
|
|||||||
TWO_WEEKS: () => randomInt(10, 14) * ONE_DAY
|
TWO_WEEKS: () => randomInt(10, 14) * ONE_DAY
|
||||||
};
|
};
|
||||||
|
|
||||||
export class Cache {
|
export class Cache<S = string> {
|
||||||
db: Database;
|
db: Database;
|
||||||
/** Time before deletion */
|
/** Time before deletion */
|
||||||
tbd = 60 * 1000;
|
tbd = 60 * 1000;
|
||||||
@ -70,12 +71,19 @@ export class Cache {
|
|||||||
cachePath: string;
|
cachePath: string;
|
||||||
/** Table name */
|
/** Table name */
|
||||||
tableName: string;
|
tableName: string;
|
||||||
|
type: S extends string ? 'string' : 'buffer';
|
||||||
|
|
||||||
constructor({ cachePath = path.join(os.tmpdir() || '/tmp', 'hdc'), tbd, tableName = 'cache' }: CacheOptions = {}) {
|
constructor({
|
||||||
|
cachePath = path.join(os.tmpdir() || '/tmp', 'hdc'), tbd, tableName = 'cache', type }: CacheOptions<S> = {}) {
|
||||||
this.cachePath = cachePath;
|
this.cachePath = cachePath;
|
||||||
mkdirSync(this.cachePath, { recursive: true });
|
mkdirSync(this.cachePath, { recursive: true });
|
||||||
if (tbd != null) this.tbd = tbd;
|
if (tbd != null) this.tbd = tbd;
|
||||||
this.tableName = tableName;
|
this.tableName = tableName;
|
||||||
|
if (type) {
|
||||||
|
this.type = type;
|
||||||
|
} else {
|
||||||
|
this.type = 'string' as any;
|
||||||
|
}
|
||||||
|
|
||||||
const db = new Database(path.join(this.cachePath, 'cache.db'));
|
const db = new Database(path.join(this.cachePath, 'cache.db'));
|
||||||
|
|
||||||
@ -84,7 +92,7 @@ export class Cache {
|
|||||||
db.exec('PRAGMA temp_store = memory;');
|
db.exec('PRAGMA temp_store = memory;');
|
||||||
db.exec('PRAGMA optimize;');
|
db.exec('PRAGMA optimize;');
|
||||||
|
|
||||||
db.prepare(`CREATE TABLE IF NOT EXISTS ${this.tableName} (key TEXT PRIMARY KEY, value TEXT, ttl REAL NOT NULL);`).run();
|
db.prepare(`CREATE TABLE IF NOT EXISTS ${this.tableName} (key TEXT PRIMARY KEY, value ${this.type === 'string' ? 'TEXT' : 'BLOB'}, ttl REAL NOT NULL);`).run();
|
||||||
db.prepare(`CREATE INDEX IF NOT EXISTS cache_ttl ON ${this.tableName} (ttl);`).run();
|
db.prepare(`CREATE INDEX IF NOT EXISTS cache_ttl ON ${this.tableName} (ttl);`).run();
|
||||||
|
|
||||||
const date = new Date();
|
const date = new Date();
|
||||||
@ -121,7 +129,7 @@ export class Cache {
|
|||||||
|
|
||||||
get(key: string, defaultValue?: string): string | undefined {
|
get(key: string, defaultValue?: string): string | undefined {
|
||||||
const rv = this.db.prepare<{ value: string }, string>(
|
const rv = this.db.prepare<{ value: string }, string>(
|
||||||
`SELECT value FROM ${this.tableName} WHERE key = ?`
|
`SELECT value FROM ${this.tableName} WHERE key = ? LIMIT 1`
|
||||||
).get(key);
|
).get(key);
|
||||||
|
|
||||||
if (!rv) return defaultValue;
|
if (!rv) return defaultValue;
|
||||||
@ -194,6 +202,8 @@ export const fsFetchCache = traceSync('initializing filesystem cache for fetch',
|
|||||||
// fsFetchCache.destroy();
|
// fsFetchCache.destroy();
|
||||||
// });
|
// });
|
||||||
|
|
||||||
|
// export const fsCache = traceSync('initializing filesystem cache', () => new Cache<Uint8Array>({ cachePath: path.resolve(import.meta.dir, '../../.cache'), type: 'buffer' }));
|
||||||
|
|
||||||
const separator = '\u0000';
|
const separator = '\u0000';
|
||||||
// const textEncoder = new TextEncoder();
|
// const textEncoder = new TextEncoder();
|
||||||
// const textDecoder = new TextDecoder();
|
// const textDecoder = new TextDecoder();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user