Files
plezy/lib/services/image_cache_service.dart
T

51 lines
1.9 KiB
Dart
Raw Normal View History

2026-05-12 15:11:16 +02:00
import 'dart:async';
// CE's public conditional export hides the IO-only httpClientFactory parameter
// behind a narrower unsupported-platform stub.
// ignore: implementation_imports
import 'package:cached_network_image_ce/src/cache/default_cache_manager.dart' as ce_cache;
2026-04-08 00:43:08 +02:00
import 'package:http/http.dart' as http;
2026-04-26 02:34:09 +02:00
import '../utils/media_server_http_client.dart';
2026-04-06 20:13:09 +02:00
2026-05-07 04:14:31 +02:00
final _artworkHttpClient = MediaServerHttpClient(usePlexApiClient: true);
2026-05-12 15:11:16 +02:00
Future<void> closeArtworkHttpClientGracefully({Duration drainTimeout = const Duration(seconds: 5)}) {
return _artworkHttpClient.closeGracefully(drainTimeout: drainTimeout);
}
/// Shared cache manager for media-server image artwork. Used for both Plex and
/// Jellyfin artwork (the class name predates Jellyfin support — it's
/// backend-neutral).
///
2026-04-08 00:43:08 +02:00
/// Uses the platform-native HTTP client so iOS/macOS (CupertinoClient) and
/// Android (CronetClient) benefit from HTTP/2 connection multiplexing —
/// many concurrent image downloads over a single connection instead of
2026-05-07 04:14:31 +02:00
/// being limited to a handful of HTTP/1.1 connections. On Linux this uses the
/// same finite-connection tuning as Plex API traffic.
class PlexImageCacheManager extends ce_cache.DefaultCacheManager {
static final PlexImageCacheManager instance = PlexImageCacheManager._();
PlexImageCacheManager._()
: super(
2026-04-24 06:42:30 +02:00
stalePeriod: const Duration(days: 14),
maxNrOfCacheObjects: 3000,
2026-05-07 04:14:31 +02:00
httpClientFactory: () => _SharedHttpClient(_artworkHttpClient.inner),
2026-04-18 12:40:35 +02:00
);
2026-04-06 20:13:09 +02:00
}
/// CE closes each factory-created client after a download. Wrap the app-wide
/// shared client so image requests reuse its platform transport without
/// transferring ownership of its lifecycle.
class _SharedHttpClient extends http.BaseClient {
final http.Client _inner;
2026-04-06 20:13:09 +02:00
_SharedHttpClient(this._inner);
2026-04-06 20:13:09 +02:00
@override
Future<http.StreamedResponse> send(http.BaseRequest request) => _inner.send(request);
2026-04-06 20:13:09 +02:00
@override
void close() {}
2026-04-06 20:13:09 +02:00
}