Skip to content

Commit

Permalink
feat: translator can set the cache time uniformly via the constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
yxw007 committed Nov 3, 2024
1 parent 026ffdb commit 300dc02
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ const cache = new Cache();

class Translator {
private engines: Map<string, Engine>;
constructor() {
private cache_time: number;
constructor(cache_time: number = 60 * 1000) {
this.engines = new Map<string, Engine>();
this.cache_time = cache_time;
}
use(engine: Engine) {
if (this.engines.has(engine.name)) {
Expand Down Expand Up @@ -49,7 +51,7 @@ class Translator {
}

return engineInstance.translate(text, options).then((translated) => {
cache.set(key, translated, cache_time);
cache.set(key, translated, cache_time ?? this.cache_time);
return translated;
});
}
Expand All @@ -60,6 +62,7 @@ const translator = new Translator();
export default {
engines,
translator,
Translator,
Cache,
getLanguage,
};
Expand Down

0 comments on commit 300dc02

Please sign in to comment.