Added caching to extension requests

This commit is contained in:
2025-10-21 21:33:35 +01:00
parent c10b11488b
commit 0d649b1b6a
2 changed files with 134 additions and 35 deletions

View File

@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'dart:math';
@@ -7,6 +8,7 @@ import 'package:jni/jni.dart';
import 'package:k3vinb5_aniyomi_bridge/jni_isolate.dart';
import 'package:k3vinb5_aniyomi_bridge/jni_isolate_message.dart';
import 'package:k3vinb5_aniyomi_bridge/models/extension.dart';
import 'package:logger/logger.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
import 'package:k3vinb5_aniyomi_bridge/jmodels/jvideo.dart';
@@ -16,6 +18,8 @@ import 'package:k3vinb5_aniyomi_bridge/jmodels/jsmanga.dart';
import 'package:k3vinb5_aniyomi_bridge/jmodels/jschapter.dart';
import 'package:k3vinb5_aniyomi_bridge/jmodels/jpage.dart';
import 'config.dart' as config;
class AniyomiBridge {
// static consts
static const String _aniyomiBridgeAnimeExtensionsDir = "animeExtensions";
@@ -24,6 +28,7 @@ class AniyomiBridge {
static const String _aniyomiBridgeJarName = "aniyomibridge-core.jar";
static const String _packageAssetsDir =
"packages/k3vinb5_aniyomi_bridge/assets";
static const Duration _cacheTtl = Duration(minutes: 20);
// static variables
static bool _isReady = false;
@@ -34,8 +39,14 @@ class AniyomiBridge {
// instance variables
late final String _supportDirectoryPath;
final Random _random = Random();
final Logger _logger = Logger(
printer: PrettyPrinter(methodCount: 9),
level: Level.debug,
// output: FileOutput(),
filter: ProductionFilter(),
);
final Map<int, Completer> _pendingRequests = {};
final Map<String, (int, dynamic)> _responsesCache = {};
AniyomiBridge() {
_initJvm();
@@ -78,91 +89,120 @@ class AniyomiBridge {
}
completer.complete(message.results);
}
});
}
});}
bool isReady() {
return _isReady;
}
Future<List<JSAnime>> getAnimeSearchResults(String query, int page, String source) async {
return _sendJniIsolateMessage<List<JSAnime>>(
Map<String, dynamic> args = {
'query': query,
'page': page,
'source': source,
};
Future<List<JSAnime>>? cachedResponse = _getCachedResponse<Future<List<JSAnime>>>(JniIsolateMessageType.getAnimeSearchResults, args);
if (cachedResponse != null) return cachedResponse;
Future<List<JSAnime>> response = _sendJniIsolateMessage<List<JSAnime>>(
JniIsolateMessage(
id: _random.nextInt(1 << 32),
type: JniIsolateMessageType.getAnimeSearchResults,
args: {
'query': query,
'page': page,
'source': source,
},
args: args,
),
);
_cacheResponse(response, JniIsolateMessageType.getAnimeSearchResults, args);
return response;
}
Future<List<JSManga>> getMangaSearchResults(String query, int page, String source) async {
return _sendJniIsolateMessage<List<JSManga>>(
Map<String, dynamic> args = {
'query': query,
'page': page,
'source': source,
};
Future<List<JSManga>>? cachedResponse = _getCachedResponse<Future<List<JSManga>>>(JniIsolateMessageType.getMangaSearchResults, args);
if (cachedResponse != null) return cachedResponse;
Future<List<JSManga>> response = _sendJniIsolateMessage<List<JSManga>>(
JniIsolateMessage(
id: _random.nextInt(1 << 32),
type: JniIsolateMessageType.getMangaSearchResults,
args: {
'query': query,
'page': page,
'source': source,
},
args: args,
),
);
_cacheResponse(response, JniIsolateMessageType.getMangaSearchResults, args);
return response;
}
Future<List<JSEpisode>> getEpisodeList(JSAnime sAnime, String source) async {
return _sendJniIsolateMessage<List<JSEpisode>>(
Map<String, dynamic> args = {
'sAnime': sAnime,
'source': source,
};
Future<List<JSEpisode>>? cachedResponse = _getCachedResponse<Future<List<JSEpisode>>>(JniIsolateMessageType.getEpisodeList, args);
if (cachedResponse != null) return cachedResponse;
Future<List<JSEpisode>> response = _sendJniIsolateMessage<List<JSEpisode>>(
JniIsolateMessage(
id: _random.nextInt(1 << 32),
type: JniIsolateMessageType.getEpisodeList,
args: {
'sAnime': sAnime,
'source': source,
},
args: args,
),
);
_cacheResponse(response, JniIsolateMessageType.getEpisodeList, args);
return response;
}
Future<List<JSChapter>> getChapterList(JSManga sManga, String source) async {
return _sendJniIsolateMessage<List<JSChapter>>(
Map<String, dynamic> args = {
'sManga': sManga,
'source': source,
};
Future<List<JSChapter>>? cachedResponse = _getCachedResponse<Future<List<JSChapter>>>(JniIsolateMessageType.getChapterList, args);
if (cachedResponse != null) return cachedResponse;
Future<List<JSChapter>> response = _sendJniIsolateMessage<List<JSChapter>>(
JniIsolateMessage(
id: _random.nextInt(1 << 32),
type: JniIsolateMessageType.getChapterList,
args: {
'sManga': sManga,
'source': source,
},
args: args,
),
);
_cacheResponse(response, JniIsolateMessageType.getChapterList, args);
return response;
}
Future<List<JVideo>> getVideoList(JSEpisode sEpisode, String source) async {
return _sendJniIsolateMessage<List<JVideo>>(
Map<String, dynamic> args = {
'sEpisode': sEpisode,
'source': source,
};
Future<List<JVideo>>? cachedResponse = _getCachedResponse<Future<List<JVideo>>>(JniIsolateMessageType.getVideoList, args);
if (cachedResponse != null) return cachedResponse;
Future<List<JVideo>> response = _sendJniIsolateMessage<List<JVideo>>(
JniIsolateMessage(
id: _random.nextInt(1 << 32),
type: JniIsolateMessageType.getVideoList,
args: {
'sEpisode': sEpisode,
'source': source,
},
args: args,
),
);
_cacheResponse(response, JniIsolateMessageType.getVideoList, args);
return response;
}
Future<List<JPage>> getPageList(JSChapter sChapter, String source) async {
return _sendJniIsolateMessage<List<JPage>>(
Map<String, dynamic> args = {
'sChapter': sChapter,
'source': source,
};
Future<List<JPage>>? cachedResponse = _getCachedResponse<Future<List<JPage>>>(JniIsolateMessageType.getPageList, args);
if (cachedResponse != null) return cachedResponse;
Future<List<JPage>> response = _sendJniIsolateMessage<List<JPage>>(
JniIsolateMessage(
id: _random.nextInt(1 << 32),
type: JniIsolateMessageType.getPageList,
args: {
'sChapter': sChapter,
'source': source,
},
args: args,
),
);
_cacheResponse(response, JniIsolateMessageType.getPageList, args);
return response;
}
Future<void> loadAnimeExtension(String extensionUrl) async {
@@ -348,4 +388,60 @@ class AniyomiBridge {
await file.writeAsBytes(buffer, flush: true);
return file;
}
T? _getCachedResponse<T>(JniIsolateMessageType messageType, Map<String, dynamic> args) {
// if (!_isCacheable(messageType)) return null;
final cacheKey = _generateCacheKey(messageType, args);
if (_responsesCache.containsKey(cacheKey)) {
_logger.d("Cached response found for ${messageType.name}");
try {
final (expiry, cachedResponse as T) =
_responsesCache[cacheKey]!;
if (expiry < DateTime.now().millisecondsSinceEpoch) {
_logger.d(
"Cached response for ${messageType.name} with cache key $cacheKey has expired, making new request",
);
_responsesCache.remove(cacheKey);
} else {
_logger.d("Cached response for ${messageType.name} with cache key $cacheKey is still valid for ${(expiry - DateTime.now().millisecondsSinceEpoch) / 1000}s");
return cachedResponse;
}
} catch (e, stackTrace) {
_logger.e(
"Error while retrieving cached response for ${messageType.name}: $e",
stackTrace: stackTrace,
);
_responsesCache.remove(cacheKey);
return null;
}
}
_logger.d("No cached response found for ${messageType.name} with cache key $cacheKey");
return null;
}
void _cacheResponse<T>(T response, JniIsolateMessageType messageType, Map<String, dynamic> args) {
final cacheKey = _generateCacheKey(messageType, args);
_logger.d("Caching response for ${messageType.name} with cache key $cacheKey");
_responsesCache[cacheKey] =
(
DateTime.now().millisecondsSinceEpoch + _cacheTtl.inMilliseconds,
response,
);
}
String _generateCacheKey(JniIsolateMessageType messageType, Map<String, dynamic> args) {
// Create a stable string representation of the args
final argsString = jsonEncode(args);
return "${messageType.name}_$argsString";
}
// bool _isCacheable(JniIsolateMessageType messageType) {
// if (config.cacheDisabledMessageType.contains(messageType)){
// _logger.w("Endpoint $messageType is disabled for caching");
// return false;
// }
// return true;
// }
}

3
lib/config.dart Normal file
View File

@@ -0,0 +1,3 @@
import 'package:k3vinb5_aniyomi_bridge/jni_isolate_message.dart';
const Set<JniIsolateMessageType> cacheDisabledMessageType = <JniIsolateMessageType>{};