chore: add ttl to morph cache and new parts of speech to default morph mapping (#5629)
This commit is contained in:
parent
765d8a944f
commit
21900f115a
2 changed files with 42 additions and 8 deletions
|
|
@ -12,11 +12,14 @@ final MorphFeaturesAndTags defaultMorphMapping = MorphFeaturesAndTags.fromJson({
|
|||
"AFFIX",
|
||||
"AUX",
|
||||
"CCONJ",
|
||||
"COMPN",
|
||||
"DET",
|
||||
"IDIOM",
|
||||
"INTJ",
|
||||
"NOUN",
|
||||
"NUM",
|
||||
"PART",
|
||||
"PHRASALV",
|
||||
"PRON",
|
||||
"PUNCT",
|
||||
"SCONJ",
|
||||
|
|
|
|||
|
|
@ -22,6 +22,28 @@ class _APICallCacheItem {
|
|||
_APICallCacheItem(this.time, this.future);
|
||||
}
|
||||
|
||||
class _MorphRepoCacheItem {
|
||||
final DateTime time;
|
||||
final MorphFeaturesAndTags morphs;
|
||||
|
||||
_MorphRepoCacheItem(this.time, this.morphs);
|
||||
|
||||
bool get isExpired =>
|
||||
DateTime.now().difference(time).inMinutes > 1440; // 24 hours
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
"time": time.toIso8601String(),
|
||||
"morphs": morphs.toJson(),
|
||||
};
|
||||
|
||||
factory _MorphRepoCacheItem.fromJson(Map<String, dynamic> json) {
|
||||
return _MorphRepoCacheItem(
|
||||
DateTime.parse(json["time"]),
|
||||
MorphFeaturesAndTags.fromJson(json["morphs"]),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MorphsRepo {
|
||||
// long-term storage of morphs
|
||||
static final GetStorage _morphsStorage = GetStorage('morphs_storage');
|
||||
|
|
@ -32,11 +54,8 @@ class MorphsRepo {
|
|||
static const int _cacheDurationMinutes = 1;
|
||||
|
||||
static void set(String languageCode, MorphFeaturesAndTags response) {
|
||||
_morphsStorage.write(languageCode, response.toJson());
|
||||
}
|
||||
|
||||
static MorphFeaturesAndTags fromJson(Map<String, dynamic> json) {
|
||||
return MorphFeaturesAndTags.fromJson(json);
|
||||
final entry = _MorphRepoCacheItem(DateTime.now(), response);
|
||||
_morphsStorage.write(languageCode, entry.toJson());
|
||||
}
|
||||
|
||||
static Future<MorphFeaturesAndTags> _fetch(String languageCode) async {
|
||||
|
|
@ -51,7 +70,7 @@ class MorphsRepo {
|
|||
);
|
||||
|
||||
final decodedBody = jsonDecode(utf8.decode(res.bodyBytes));
|
||||
final response = MorphsRepo.fromJson(decodedBody);
|
||||
final response = MorphFeaturesAndTags.fromJson(decodedBody);
|
||||
|
||||
set(languageCode, response);
|
||||
|
||||
|
|
@ -81,7 +100,16 @@ class MorphsRepo {
|
|||
// check if we have a cached morphs for this language code
|
||||
final cachedJson = _morphsStorage.read(langCodeShort);
|
||||
if (cachedJson != null) {
|
||||
return MorphsRepo.fromJson(cachedJson);
|
||||
try {
|
||||
final cacheItem = _MorphRepoCacheItem.fromJson(cachedJson);
|
||||
if (!cacheItem.isExpired) {
|
||||
return cacheItem.morphs;
|
||||
} else {
|
||||
_morphsStorage.remove(langCodeShort);
|
||||
}
|
||||
} catch (e) {
|
||||
_morphsStorage.remove(langCodeShort);
|
||||
}
|
||||
}
|
||||
|
||||
// check if we have a cached call for this language code
|
||||
|
|
@ -110,7 +138,10 @@ class MorphsRepo {
|
|||
MatrixState.pangeaController.userController.userL2!.langCodeShort,
|
||||
);
|
||||
if (cachedJson != null) {
|
||||
return MorphsRepo.fromJson(cachedJson);
|
||||
final cacheItem = _MorphRepoCacheItem.fromJson(cachedJson);
|
||||
if (!cacheItem.isExpired) {
|
||||
return cacheItem.morphs;
|
||||
}
|
||||
}
|
||||
return defaultMorphMapping;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue