Explorar el Código

ref: interface & cache stuff

Sv443 hace 1 año
padre
commit
a8fa27ea77
Se han modificado 2 ficheros con 13 adiciones y 6 borrados
  1. 11 4
      src/features/lyricsCache.ts
  2. 2 2
      src/interface.ts

+ 11 - 4
src/features/lyricsCache.ts

@@ -9,6 +9,11 @@ export type LyricsCache = {
   cache: LyricsCacheEntry[];
 };
 
+/** A fraction of this max value will be removed from the "last viewed" timestamp when adding penalized cache entries */
+const maxViewedPenalty = 1000 * 60 * 60 * 24 * 5; // 5 days
+/** A fraction of this max value will be removed from the "added" timestamp when adding penalized cache entries */
+const maxAddedPenalty = 1000 * 60 * 60 * 24 * 15; // 15 days
+
 let canCompress = true;
 
 const lyricsCacheMgr = new ConfigManager<LyricsCache>({
@@ -100,7 +105,7 @@ export function addLyricsCacheEntry(artist: string, song: string, url: string) {
   if(cache.length > getFeatures().lyricsCacheMaxSize)
     cache.pop();
 
-  emitInterface("bytm:lyricsCacheEntryAdded", entry);
+  emitInterface("bytm:lyricsCacheEntryAdded", { entry, type: "best" });
 
   return lyricsCacheMgr.setData({ cache });
 }
@@ -117,8 +122,9 @@ export function addLyricsCacheEntryPenalized(artist: string, song: string, url:
 
   penaltyFr = clamp(penaltyFr, 0, 1);
 
-  const viewedPenalty = 1000 * 60 * 60 * 24 * 5 * penaltyFr; // 5 days
-  const addedPenalty = 1000 * 60 * 60 * 24 * 15 * penaltyFr; // 15 days
+  const viewedPenalty = maxViewedPenalty * penaltyFr;
+  const addedPenalty = maxAddedPenalty * penaltyFr;
+
   const entry = {
     artist,
     song,
@@ -129,10 +135,11 @@ export function addLyricsCacheEntryPenalized(artist: string, song: string, url:
 
   cache.push(entry);
   cache.sort((a, b) => b.viewed - a.viewed);
+
   if(cache.length > getFeatures().lyricsCacheMaxSize)
     cache.pop();
 
-  emitInterface("bytm:lyricsCacheEntryAdded", entry);
+  emitInterface("bytm:lyricsCacheEntryAdded", { entry, type: "penalized" });
 
   return lyricsCacheMgr.setData({ cache });
 }

+ 2 - 2
src/interface.ts

@@ -36,8 +36,8 @@ export type InterfaceEvents = {
   "bytm:lyricsCacheReady": LyricsCache;
   /** Emitted when the lyrics cache has been cleared */
   "bytm:lyricsCacheCleared": undefined;
-  /** Emitted when an entry is added to the lyrics cache */
-  "bytm:lyricsCacheEntryAdded": LyricsCacheEntry;
+  /** Emitted when an entry is added to the lyrics cache - "penalized" entries have a lower TTL because they were less related in lyrics lookups, as opposed to the "best" entries */
+  "bytm:lyricsCacheEntryAdded": { type: "best" | "penalized", entry: LyricsCacheEntry };
 
   // additionally all events from SiteEventsMap in `src/siteEvents.ts`
   // are emitted in this format: "bytm:siteEvent:nameOfSiteEvent"