Add FileCache model

This commit is contained in:
Max Isom 2021-11-18 20:55:57 -05:00
parent d805da906a
commit 04c7e61fc0
No known key found for this signature in database
GPG key ID: 25C9B1A7F6798880
7 changed files with 26 additions and 9 deletions

View file

@ -28,7 +28,7 @@ import Shuffle from './commands/shuffle.js';
import Skip from './commands/skip.js'; import Skip from './commands/skip.js';
import Unskip from './commands/unskip.js'; import Unskip from './commands/unskip.js';
import ThirdParty from './services/third-party.js'; import ThirdParty from './services/third-party.js';
import CacheProvider from './services/cache.js'; import KeyValueCacheProvider from './services/key-value-cache.js';
const container = new Container(); const container = new Container();
@ -76,6 +76,6 @@ container.bind(TYPES.Config).toConstantValue(new ConfigProvider());
// Static libraries // Static libraries
container.bind(TYPES.ThirdParty).to(ThirdParty); container.bind(TYPES.ThirdParty).to(ThirdParty);
container.bind(TYPES.Cache).to(CacheProvider); container.bind(TYPES.KeyValueCache).to(KeyValueCacheProvider);
export default container; export default container;

14
src/models/file-cache.ts Normal file
View file

@ -0,0 +1,14 @@
import {Table, Column, PrimaryKey, Model} from 'sequelize-typescript';
@Table
export default class FileCache extends Model<FileCache> {
@PrimaryKey
@Column
hash!: string;
@Column
kbits!: number;
@Column
accessedAt!: Date;
}

View file

@ -1,8 +1,10 @@
import FileCache from './file-cache.js';
import KeyValueCache from './key-value-cache.js'; import KeyValueCache from './key-value-cache.js';
import Settings from './settings.js'; import Settings from './settings.js';
import Shortcut from './shortcut.js'; import Shortcut from './shortcut.js';
export { export {
FileCache,
KeyValueCache, KeyValueCache,
Settings, Settings,
Shortcut, Shortcut,

View file

@ -14,7 +14,7 @@ import {TYPES} from '../types.js';
import {cleanUrl} from '../utils/url.js'; import {cleanUrl} from '../utils/url.js';
import ThirdParty from './third-party.js'; import ThirdParty from './third-party.js';
import Config from './config.js'; import Config from './config.js';
import CacheProvider from './cache.js'; import KeyValueCacheProvider from './key-value-cache.js';
type QueuedSongWithoutChannel = Except<QueuedSong, 'addedInChannelId'>; type QueuedSongWithoutChannel = Except<QueuedSong, 'addedInChannelId'>;
@ -26,14 +26,14 @@ export default class {
private readonly youtube: YouTube; private readonly youtube: YouTube;
private readonly youtubeKey: string; private readonly youtubeKey: string;
private readonly spotify: Spotify; private readonly spotify: Spotify;
private readonly cache: CacheProvider; private readonly cache: KeyValueCacheProvider;
private readonly ytsrQueue: PQueue; private readonly ytsrQueue: PQueue;
constructor( constructor(
@inject(TYPES.ThirdParty) thirdParty: ThirdParty, @inject(TYPES.ThirdParty) thirdParty: ThirdParty,
@inject(TYPES.Config) config: Config, @inject(TYPES.Config) config: Config,
@inject(TYPES.Cache) cache: CacheProvider) { @inject(TYPES.KeyValueCache) cache: KeyValueCacheProvider) {
this.youtube = thirdParty.youtube; this.youtube = thirdParty.youtube;
this.youtubeKey = config.YOUTUBE_API_KEY; this.youtubeKey = config.YOUTUBE_API_KEY;
this.spotify = thirdParty.spotify; this.spotify = thirdParty.spotify;

View file

@ -12,7 +12,7 @@ type Options = {
const futureTimeToDate = (time: Seconds) => new Date(new Date().getTime() + (time * 1000)); const futureTimeToDate = (time: Seconds) => new Date(new Date().getTime() + (time * 1000));
@injectable() @injectable()
export default class CacheProvider { export default class KeyValueCacheProvider {
async wrap<T extends [...any[], Options], F>(func: (...options: any) => Promise<F>, ...options: T): Promise<F> { async wrap<T extends [...any[], Options], F>(func: (...options: any) => Promise<F>, ...options: T): Promise<F> {
if (options.length === 0) { if (options.length === 0) {
throw new Error('Missing cache options'); throw new Error('Missing cache options');

View file

@ -1,6 +1,7 @@
export const TYPES = { export const TYPES = {
Bot: Symbol('Bot'), Bot: Symbol('Bot'),
Cache: Symbol('Cache'), KeyValueCache: Symbol('KeyValueCache'),
FileCache: Symbol('FileCache'),
Client: Symbol('Client'), Client: Symbol('Client'),
Config: Symbol('Config'), Config: Symbol('Config'),
Command: Symbol('Command'), Command: Symbol('Command'),

View file

@ -1,12 +1,12 @@
import {Sequelize} from 'sequelize-typescript'; import {Sequelize} from 'sequelize-typescript';
import path from 'path'; import path from 'path';
import {DATA_DIR} from '../services/config.js'; import {DATA_DIR} from '../services/config.js';
import {KeyValueCache, Settings, Shortcut} from '../models/index.js'; import {FileCache, KeyValueCache, Settings, Shortcut} from '../models/index.js';
export const sequelize = new Sequelize({ export const sequelize = new Sequelize({
dialect: 'sqlite', dialect: 'sqlite',
database: 'muse', database: 'muse',
storage: path.join(DATA_DIR, 'db.sqlite'), storage: path.join(DATA_DIR, 'db.sqlite'),
models: [KeyValueCache, Settings, Shortcut], models: [FileCache, KeyValueCache, Settings, Shortcut],
logging: false, logging: false,
}); });