Stickers cache TTL

This commit is contained in:
Eduard Kuzmenko 2021-05-10 02:11:20 +04:00
parent 7500e14f35
commit f316222a45
3 changed files with 11 additions and 4 deletions

3
src/layer.d.ts vendored
View File

@ -3852,7 +3852,8 @@ export namespace MessagesStickerSet {
_: 'messages.stickerSet',
set: StickerSet,
packs: Array<StickerPack>,
documents: Array<Document>
documents: Array<Document>,
refreshTime?: number
};
}

View File

@ -13,7 +13,7 @@ import AppStorage from '../storage';
import { MOUNT_CLASS_TO } from '../../config/debug';
import { forEachReverse } from '../../helpers/array';
// TODO: если пак будет сохранён и потом обновлён, то недостающие стикеры не подгрузит
const CACHE_TIME = 3600e3;
export class AppStickersManager {
private storage = new AppStorage<Record<string, MessagesStickerSet>>({
@ -53,10 +53,10 @@ export class AppStickersManager {
return this.getStickerSetPromises[set.id];
}
return this.getStickerSetPromises[set.id] = new Promise(async(resolve, reject) => {
return this.getStickerSetPromises[set.id] = new Promise(async(resolve) => {
if(!params.overwrite) {
const cachedSet = await this.storage.get(set.id);
if(cachedSet && cachedSet.documents?.length) {
if(cachedSet && cachedSet.documents?.length && (Date.now() - cachedSet.refreshTime) < CACHE_TIME) {
this.saveStickers(cachedSet.documents);
resolve(cachedSet);
delete this.getStickerSetPromises[set.id];
@ -120,6 +120,7 @@ export class AppStickersManager {
//console.log('stickers wrote', this.stickerSets);
const needSave = stickerSet.set.installed_date || id === 'emoji';
stickerSet.refreshTime = Date.now();
this.storage.set({[id]: stickerSet}, !needSave);
}

View File

@ -214,4 +214,9 @@
{"name": "channel_id", "type": "number"}
],
"type": "Update"
}, {
"predicate": "messages.stickerSet",
"params": [
{"name": "refreshTime", "type": "number"}
]
}]