import 'dart:convert'; import 'package:localstore/localstore.dart'; abstract class RequestCache { static const int cacheNothing = 0; static const int cacheMinute = 60; static const int cacheHour = 60 * 60; static const int cacheDay = 60 * 60 * 24; int maxCacheTime; Function(T) onUpdate; bool? renew; RequestCache(this.maxCacheTime, this.onUpdate, {this.renew = false}); void start(String file, String document) async { Map? tableData = await Localstore.instance.collection(file).doc(document).get(); if(tableData != null) { onUpdate(onLocalData(tableData['json'])); } if(DateTime.now().millisecondsSinceEpoch - (maxCacheTime * 1000) < (tableData?['lastupdate'] ?? 0)) { if(renew == null || !renew!) return; } T newValue = await onLoad(); onUpdate(newValue); Localstore.instance.collection(file).doc(document).set({ "json": jsonEncode(newValue), "lastupdate": DateTime.now().millisecondsSinceEpoch }); } T onLocalData(String json); Future onLoad(); }