Browse Source

Stickers cache TTL

master
Eduard Kuzmenko 4 years ago
parent
commit
f316222a45
  1. 3
      src/layer.d.ts
  2. 7
      src/lib/appManagers/appStickersManager.ts
  3. 5
      src/scripts/in/schema_additional_params.json

3
src/layer.d.ts vendored

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

7
src/lib/appManagers/appStickersManager.ts

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

5
src/scripts/in/schema_additional_params.json

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