Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Integrate better caching methods

laurenspriem8459d7ead648
diff --git a/mobile/lib/services/magic_cache_service.dart b/mobile/lib/services/magic_cache_service.dart
index 462cb851fd..ff647ad93a 100644
--- a/mobile/lib/services/magic_cache_service.dart
+++ b/mobile/lib/services/magic_cache_service.dart
@@ -251,13 +251,12 @@ class MagicCacheService {
final List<MagicCache> magicCaches =
await _nonEmptyMagicResults(magicPromptsData);
w?.log("resultComputed");
- final file = File(await _getCachePath());
- if (!file.existsSync()) {
- file.createSync(recursive: true);
- }
_magicCacheFuture = Future.value(magicCaches);
- await file
- .writeAsBytes(MagicCache.encodeListToJson(magicCaches).codeUnits);
+ await writeToJsonFile<List<MagicCache>>(
+ await _getCachePath(),
+ magicCaches,
+ MagicCache.encodeListToJson,
+ );
w?.log("cacheWritten");
await _resetLastMagicCacheUpdateTime();
w?.logAndReset('done');
@@ -300,20 +299,11 @@ class MagicCacheService {
Future<List<MagicCache>> _readResultFromDisk() async {
_logger.info("Reading magic cache result from disk");
- final file = File(await _getCachePath());
- if (!file.existsSync()) {
- _logger.info("No magic cache found");
- return [];
- }
- try {
- final bytes = await file.readAsBytes();
- final jsonString = String.fromCharCodes(bytes);
- return MagicCache.decodeJsonToList(jsonString);
- } catch (e, s) {
- _logger.severe("Error reading or decoding cache file", e, s);
- await file.delete();
- rethrow;
- }
+ final cache = await decodeJsonFile<List<MagicCache>>(
+ await _getCachePath(),
+ MagicCache.decodeJsonToList,
+ );
+ return cache ?? [];
}
Future<void> clearMagicCache() async {
diff --git a/mobile/lib/services/memories_cache_service.dart b/mobile/lib/services/memories_cache_service.dart
index 4eed3fbba6..90034d03b7 100644
--- a/mobile/lib/services/memories_cache_service.dart
+++ b/mobile/lib/services/memories_cache_service.dart
@@ -201,22 +201,11 @@ class MemoriesCacheService {
Future<MemoriesCache?> _readCacheFromDisk() async {
_logger.info("Reading memories cache result from disk");
- final file = File(await _getCachePath());
- if (!file.existsSync()) {
- _logger.info("No memories cache found");
- return null;
- }
- try {
- final bytes = await file.readAsBytes();
- final jsonString = String.fromCharCodes(bytes);
- final cache = MemoriesCache.decodeFromJsonString(jsonString);
- _logger.info("Reading memories cache result from disk done");
- return cache;
- } catch (e, s) {
- _logger.severe("Error reading or decoding cache file", e, s);
- await file.delete();
- return null;
- }
+ final cache = decodeJsonFile<MemoriesCache>(
+ await _getCachePath(),
+ MemoriesCache.decodeFromJsonString,
+ );
+ return cache;
}
Future<List<SmartMemory>> _fromCacheToMemories(MemoriesCache cache) async {
@@ -325,10 +314,6 @@ class MemoriesCacheService {
}
newCache.baseLocations.addAll(nowResult.baseLocations);
w?.log("added memories to cache");
- final file = File(await _getCachePath());
- if (!file.existsSync()) {
- file.createSync(recursive: true);
- }
_cachedMemories = nowResult.memories
.where((memory) => memory.shouldShowNow())
.toList();
@@ -336,8 +321,10 @@ class MemoriesCacheService {
[...nowResult.memories, ...nextResult.memories],
);
locationService.baseLocations = nowResult.baseLocations;
- await file.writeAsBytes(
- MemoriesCache.encodeToJsonString(newCache).codeUnits,
+ await writeToJsonFile<MemoriesCache>(
+ await _getCachePath(),
+ newCache,
+ MemoriesCache.encodeToJsonString,
);
w?.log("cacheWritten");
await _cacheUpdated();