refactor(cache): make code more dry and return in-memory cache for test env

This commit is contained in:
WebStreamr 2026-01-22 15:01:00 +00:00
parent d3db3fc512
commit a5539bcf59
No known key found for this signature in database
3 changed files with 21 additions and 17 deletions

View file

@ -1,9 +1,7 @@
// eslint-disable-next-line import/no-named-as-default
import KeyvSqlite from '@keyv/sqlite';
import { Cacheable, CacheableMemory, Keyv } from 'cacheable';
import winston from 'winston';
import { Context, Format, Meta, UrlResult } from '../types';
import { envGet, getCacheDir, isExtractorDisabled, scheduleKeyvSqliteCleanup } from '../utils';
import { createKeyvSqlite, envGet, isExtractorDisabled } from '../utils';
import { Extractor } from './Extractor';
export class ExtractorRegistry {
@ -17,23 +15,19 @@ export class ExtractorRegistry {
this.logger = logger;
this.extractors = extractors;
const urlResultKeyvSqlite = new KeyvSqlite(`sqlite://${getCacheDir()}/webstreamr-extractor-cache.sqlite`);
this.urlResultCache = new Cacheable({
nonBlocking: true,
primary: new Keyv({ store: new CacheableMemory({ lruSize: 1024 }) }),
secondary: new Keyv(urlResultKeyvSqlite),
secondary: createKeyvSqlite('extractor-cache'),
stats: true,
});
scheduleKeyvSqliteCleanup(urlResultKeyvSqlite);
const lazyUrlResultKeyvSqlite = new KeyvSqlite(`sqlite://${getCacheDir()}/webstreamr-extractor-lazy-cache.sqlite`);
this.lazyUrlResultCache = new Cacheable({
nonBlocking: true,
primary: new Keyv({ store: new CacheableMemory({ lruSize: 1024 }) }),
secondary: new Keyv(lazyUrlResultKeyvSqlite),
secondary: createKeyvSqlite('extractor-lazy-cache'),
stats: true,
});
scheduleKeyvSqliteCleanup(lazyUrlResultKeyvSqlite);
}
public stats() {

View file

@ -1,24 +1,20 @@
// eslint-disable-next-line import/no-named-as-default
import KeyvSqlite from '@keyv/sqlite';
import { Cacheable, CacheableMemory, Keyv } from 'cacheable';
import { ContentType } from 'stremio-addon-sdk';
import { NotFoundError } from '../error';
import { Context, CountryCode, Meta } from '../types';
import { getCacheDir, Id, scheduleKeyvSqliteCleanup } from '../utils';
import { createKeyvSqlite, Id } from '../utils';
export interface SourceResult {
url: URL;
meta: Meta;
}
const sourceResultKeyvSqlite = new KeyvSqlite(`sqlite://${getCacheDir()}/webstreamr-source-cache-v2.sqlite`);
const sourceResultCache = new Cacheable({
nonBlocking: true,
primary: new Keyv({ store: new CacheableMemory({ lruSize: 1024 }) }),
secondary: new Keyv(sourceResultKeyvSqlite),
secondary: createKeyvSqlite('source-cache-v2'),
stats: true,
});
scheduleKeyvSqliteCleanup(sourceResultKeyvSqlite);
export abstract class Source {
public abstract readonly id: string;

View file

@ -2,10 +2,12 @@
import fs from 'node:fs';
// eslint-disable-next-line import/no-named-as-default
import KeyvSqlite from '@keyv/sqlite';
import { KeyvCacheableMemory } from 'cacheable';
import { KeyvStoreAdapter } from 'keyv';
import * as sqlite3 from 'sqlite3';
import { envIsTest } from './env';
import { envIsTest, getCacheDir } from './env';
export const scheduleKeyvSqliteCleanup = (keyvSqlite: KeyvSqlite): void => {
const scheduleKeyvSqliteCleanup = (keyvSqlite: KeyvSqlite): void => {
const filename = keyvSqlite.opts.db;
if (envIsTest() || !filename || !fs.existsSync(filename)) {
return;
@ -20,3 +22,15 @@ export const scheduleKeyvSqliteCleanup = (keyvSqlite: KeyvSqlite): void => {
db.close();
}, 60 * 60 * 1000); // every hour
};
export const createKeyvSqlite = (name: string): KeyvStoreAdapter => {
if (envIsTest()) {
return new KeyvCacheableMemory();
}
const keyvSqlite = new KeyvSqlite(`sqlite://${getCacheDir()}/webstreamr-${name}.sqlite`);
scheduleKeyvSqliteCleanup(keyvSqlite);
return keyvSqlite;
};