Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: using isolates to handle file related tasks #23

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions lib/cached_video_player_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ library cached_video_player_plus;

import 'dart:async';
import 'dart:io';
import 'dart:isolate';
import 'dart:math' as math;

import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -434,7 +435,9 @@ class CachedVideoPlayerPlusController

if (dataSourceType == DataSourceType.network && _isCachingSupported) {
final cacheManager = VideoCacheManager();
FileInfo? cachedFile = await cacheManager.getFileFromCache(dataSource);
FileInfo? cachedFile = await Isolate.run(() {
return cacheManager.getFileFromCache(dataSource);
});

debugPrint('Cached video of [$dataSource] is: ${cachedFile?.file.path}');

Expand All @@ -457,27 +460,32 @@ class CachedVideoPlayerPlusController

if (difference > invalidateCacheIfOlderThan) {
debugPrint('Cache of [$dataSource] expired. Removing...');
await cacheManager.removeFile(dataSource);
await Isolate.run(() {
return cacheManager.removeFile(dataSource);
});
cachedFile = null;
}
} else {
debugPrint('Cache of [$dataSource] expired. Removing...');
await cacheManager.removeFile(dataSource);
await Isolate.run(() {
return cacheManager.removeFile(dataSource);
});
cachedFile = null;
}
}

if (cachedFile == null) {
cacheManager
.downloadFile(dataSource, authHeaders: httpHeaders)
.then((value) {
storage.write(
'cached_video_player_plus_video_expiration_of_${Uri.parse(
dataSource,
)}',
DateTime.timestamp().millisecondsSinceEpoch,
);
debugPrint('Cached video [$dataSource] successfully.');
Isolate.run(() async {
await cacheManager
.downloadFile(dataSource, authHeaders: httpHeaders)
.then((_) {
storage.write(
'cached_video_player_plus_video_expiration_of_'
'${Uri.parse(dataSource)}',
DateTime.timestamp().millisecondsSinceEpoch,
);
debugPrint('Cached video [$dataSource] successfully.');
});
});
} else {
isCacheAvailable = true;
Expand Down
4 changes: 1 addition & 3 deletions lib/src/video_cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class VideoCacheManager extends CacheManager with ImageCacheManager {
static final VideoCacheManager _instance = VideoCacheManager._();

/// Returns the singleton instance of the [VideoCacheManager].
factory VideoCacheManager() {
return _instance;
}
factory VideoCacheManager() => _instance;

/// Creates a new instance of the [VideoCacheManager].
VideoCacheManager._() : super(Config(key));
Expand Down
Loading