Enable caching BitChute metadata

This commit is contained in:
Xaekai 2022-01-26 18:36:54 -08:00
parent 8dde08ac6d
commit c506ccf05b
2 changed files with 14 additions and 4 deletions

View File

@ -85,6 +85,9 @@ module.exports.init = function (newDB) {
require('@cytube/mediaquery/lib/provider/youtube').setCache( require('@cytube/mediaquery/lib/provider/youtube').setCache(
new MetadataCacheDB(db) new MetadataCacheDB(db)
); );
require('@cytube/mediaquery/lib/provider/bitchute').setCache(
new MetadataCacheDB(db)
);
}).catch(error => { }).catch(error => {
LOGGER.error(error.stack); LOGGER.error(error.stack);
process.exit(1); process.exit(1);

View File

@ -9,6 +9,8 @@ function mediaquery2cytube(type) {
switch (type) { switch (type) {
case 'youtube': case 'youtube':
return 'yt'; return 'yt';
case 'bitchute':
return 'bc';
default: default:
throw new Error(`mediaquery2cytube: no mapping for ${type}`); throw new Error(`mediaquery2cytube: no mapping for ${type}`);
} }
@ -18,14 +20,17 @@ function cytube2mediaquery(type) {
switch (type) { switch (type) {
case 'yt': case 'yt':
return 'youtube'; return 'youtube';
case 'bc':
return 'bitchute';
default: default:
throw new Error(`cytube2mediaquery: no mapping for ${type}`); throw new Error(`cytube2mediaquery: no mapping for ${type}`);
} }
} }
const cachedResultAge = new Summary({ const cachedResultAge = new Summary({
name: 'cytube_yt_cache_result_age_seconds', name: 'cytube_media_cache_result_age_seconds',
help: 'Age (in seconds) of cached record' help: 'Age (in seconds) of cached record',
labelNames: ['source']
}); });
class MetadataCacheDB { class MetadataCacheDB {
@ -62,10 +67,11 @@ class MetadataCacheDB {
return null; return null;
} }
let age = 0;
try { try {
let age = (Date.now() - row.updated_at.getTime())/1000; age = (Date.now() - row.updated_at.getTime())/1000;
if (age > 0) { if (age > 0) {
cachedResultAge.observe(age); cachedResultAge.labels(type).observe(age);
} }
} catch (error) { } catch (error) {
LOGGER.error('Failed to record cachedResultAge metric: %s', error.stack); LOGGER.error('Failed to record cachedResultAge metric: %s', error.stack);
@ -73,6 +79,7 @@ class MetadataCacheDB {
let metadata = JSON.parse(row.metadata); let metadata = JSON.parse(row.metadata);
metadata.type = cytube2mediaquery(metadata.type); metadata.type = cytube2mediaquery(metadata.type);
metadata.meta.cacheAge = age;
return new Media(metadata); return new Media(metadata);
}); });
} }