Added caching to extension requests
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'dart:isolate';
|
import 'dart:isolate';
|
||||||
import 'dart:math';
|
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.dart';
|
||||||
import 'package:k3vinb5_aniyomi_bridge/jni_isolate_message.dart';
|
import 'package:k3vinb5_aniyomi_bridge/jni_isolate_message.dart';
|
||||||
import 'package:k3vinb5_aniyomi_bridge/models/extension.dart';
|
import 'package:k3vinb5_aniyomi_bridge/models/extension.dart';
|
||||||
|
import 'package:logger/logger.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:path/path.dart' as path;
|
import 'package:path/path.dart' as path;
|
||||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/jvideo.dart';
|
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/jschapter.dart';
|
||||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/jpage.dart';
|
import 'package:k3vinb5_aniyomi_bridge/jmodels/jpage.dart';
|
||||||
|
|
||||||
|
import 'config.dart' as config;
|
||||||
|
|
||||||
class AniyomiBridge {
|
class AniyomiBridge {
|
||||||
// static consts
|
// static consts
|
||||||
static const String _aniyomiBridgeAnimeExtensionsDir = "animeExtensions";
|
static const String _aniyomiBridgeAnimeExtensionsDir = "animeExtensions";
|
||||||
@@ -24,6 +28,7 @@ class AniyomiBridge {
|
|||||||
static const String _aniyomiBridgeJarName = "aniyomibridge-core.jar";
|
static const String _aniyomiBridgeJarName = "aniyomibridge-core.jar";
|
||||||
static const String _packageAssetsDir =
|
static const String _packageAssetsDir =
|
||||||
"packages/k3vinb5_aniyomi_bridge/assets";
|
"packages/k3vinb5_aniyomi_bridge/assets";
|
||||||
|
static const Duration _cacheTtl = Duration(minutes: 20);
|
||||||
|
|
||||||
// static variables
|
// static variables
|
||||||
static bool _isReady = false;
|
static bool _isReady = false;
|
||||||
@@ -34,8 +39,14 @@ class AniyomiBridge {
|
|||||||
// instance variables
|
// instance variables
|
||||||
late final String _supportDirectoryPath;
|
late final String _supportDirectoryPath;
|
||||||
final Random _random = Random();
|
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<int, Completer> _pendingRequests = {};
|
||||||
|
final Map<String, (int, dynamic)> _responsesCache = {};
|
||||||
|
|
||||||
AniyomiBridge() {
|
AniyomiBridge() {
|
||||||
_initJvm();
|
_initJvm();
|
||||||
@@ -78,91 +89,120 @@ class AniyomiBridge {
|
|||||||
}
|
}
|
||||||
completer.complete(message.results);
|
completer.complete(message.results);
|
||||||
}
|
}
|
||||||
});
|
});}
|
||||||
}
|
|
||||||
|
|
||||||
bool isReady() {
|
bool isReady() {
|
||||||
return _isReady;
|
return _isReady;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<JSAnime>> getAnimeSearchResults(String query, int page, String source) async {
|
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(
|
JniIsolateMessage(
|
||||||
id: _random.nextInt(1 << 32),
|
id: _random.nextInt(1 << 32),
|
||||||
type: JniIsolateMessageType.getAnimeSearchResults,
|
type: JniIsolateMessageType.getAnimeSearchResults,
|
||||||
args: {
|
args: args,
|
||||||
'query': query,
|
|
||||||
'page': page,
|
|
||||||
'source': source,
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
_cacheResponse(response, JniIsolateMessageType.getAnimeSearchResults, args);
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<JSManga>> getMangaSearchResults(String query, int page, String source) async {
|
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(
|
JniIsolateMessage(
|
||||||
id: _random.nextInt(1 << 32),
|
id: _random.nextInt(1 << 32),
|
||||||
type: JniIsolateMessageType.getMangaSearchResults,
|
type: JniIsolateMessageType.getMangaSearchResults,
|
||||||
args: {
|
args: args,
|
||||||
'query': query,
|
|
||||||
'page': page,
|
|
||||||
'source': source,
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
_cacheResponse(response, JniIsolateMessageType.getMangaSearchResults, args);
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<JSEpisode>> getEpisodeList(JSAnime sAnime, String source) async {
|
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(
|
JniIsolateMessage(
|
||||||
id: _random.nextInt(1 << 32),
|
id: _random.nextInt(1 << 32),
|
||||||
type: JniIsolateMessageType.getEpisodeList,
|
type: JniIsolateMessageType.getEpisodeList,
|
||||||
args: {
|
args: args,
|
||||||
'sAnime': sAnime,
|
|
||||||
'source': source,
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
_cacheResponse(response, JniIsolateMessageType.getEpisodeList, args);
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<JSChapter>> getChapterList(JSManga sManga, String source) async {
|
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(
|
JniIsolateMessage(
|
||||||
id: _random.nextInt(1 << 32),
|
id: _random.nextInt(1 << 32),
|
||||||
type: JniIsolateMessageType.getChapterList,
|
type: JniIsolateMessageType.getChapterList,
|
||||||
args: {
|
args: args,
|
||||||
'sManga': sManga,
|
|
||||||
'source': source,
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
_cacheResponse(response, JniIsolateMessageType.getChapterList, args);
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<JVideo>> getVideoList(JSEpisode sEpisode, String source) async {
|
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(
|
JniIsolateMessage(
|
||||||
id: _random.nextInt(1 << 32),
|
id: _random.nextInt(1 << 32),
|
||||||
type: JniIsolateMessageType.getVideoList,
|
type: JniIsolateMessageType.getVideoList,
|
||||||
args: {
|
args: args,
|
||||||
'sEpisode': sEpisode,
|
|
||||||
'source': source,
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
_cacheResponse(response, JniIsolateMessageType.getVideoList, args);
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<JPage>> getPageList(JSChapter sChapter, String source) async {
|
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(
|
JniIsolateMessage(
|
||||||
id: _random.nextInt(1 << 32),
|
id: _random.nextInt(1 << 32),
|
||||||
type: JniIsolateMessageType.getPageList,
|
type: JniIsolateMessageType.getPageList,
|
||||||
args: {
|
args: args,
|
||||||
'sChapter': sChapter,
|
|
||||||
'source': source,
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
_cacheResponse(response, JniIsolateMessageType.getPageList, args);
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> loadAnimeExtension(String extensionUrl) async {
|
Future<void> loadAnimeExtension(String extensionUrl) async {
|
||||||
@@ -348,4 +388,60 @@ class AniyomiBridge {
|
|||||||
await file.writeAsBytes(buffer, flush: true);
|
await file.writeAsBytes(buffer, flush: true);
|
||||||
return file;
|
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
3
lib/config.dart
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import 'package:k3vinb5_aniyomi_bridge/jni_isolate_message.dart';
|
||||||
|
|
||||||
|
const Set<JniIsolateMessageType> cacheDisabledMessageType = <JniIsolateMessageType>{};
|
||||||
Reference in New Issue
Block a user