Skip to content
Open
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
8 changes: 7 additions & 1 deletion flutter_cache_manager/lib/src/cache_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class CacheStore {

Duration get _maxAge => _config.stalePeriod;

Duration get _maxCapacityAgeFilterDuration =>
_config.maxCapacityAgeFilterDuration ?? const Duration(days: 1);

DateTime lastCleanupRun = DateTime.now();
Timer? _scheduledCleanup;

Expand Down Expand Up @@ -146,7 +149,10 @@ class CacheStore {
final toRemove = <int>[];
final provider = await _cacheInfoRepository;

final overCapacity = await provider.getObjectsOverCapacity(_capacity);
final overCapacity = await provider.getObjectsOverCapacity(
_capacity,
maxAge: _maxCapacityAgeFilterDuration,
);
for (final cacheObject in overCapacity) {
_removeCachedFile(cacheObject, toRemove);
}
Expand Down
5 changes: 5 additions & 0 deletions flutter_cache_manager/lib/src/config/_config_unsupported.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Config implements def.Config {
FileSystem? fileSystem,
//ignore: avoid_unused_constructor_parameters
FileService? fileService,
//ignore: avoid_unused_constructor_parameters
Duration? maxCapacityAgeFilterDuration,
}) {
throw UnsupportedError('Platform is not supported');
}
Expand All @@ -39,4 +41,7 @@ class Config implements def.Config {

@override
FileService get fileService => throw UnimplementedError();

@override
Duration? get maxCapacityAgeFilterDuration => throw UnimplementedError();
}
3 changes: 3 additions & 0 deletions flutter_cache_manager/lib/src/config/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ abstract class Config {
CacheInfoRepository repo,
FileSystem fileSystem,
FileService fileService,
Duration? maxCapacityAgeFilterDuration,
}) = impl.Config;

String get cacheKey;
Expand All @@ -41,4 +42,6 @@ abstract class Config {
FileSystem get fileSystem;

FileService get fileService;

Duration? get maxCapacityAgeFilterDuration;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ abstract class CacheInfoRepository {
/// Gets the list of [CacheObject] that can be removed if the repository is over capacity.
///
/// The exact implementation is up to the repository, but implementations should
/// return a preferred list of items. For example, the least recently accessed
Future<List<CacheObject>> getObjectsOverCapacity(int capacity);
/// return a preferred list of items. For example, the least recently accessed.
/// [maxAge] filters objects older than the given duration (defaults to 1 day).
Future<List<CacheObject>> getObjectsOverCapacity(
int capacity, {
Duration? maxAge,
});

/// Returns a list of [CacheObject] that are older than [maxAge]
Future<List<CacheObject>> getOldObjects(Duration maxAge);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ class CacheObjectProvider extends CacheInfoRepository
}

@override
Future<List<CacheObject>> getObjectsOverCapacity(int capacity) async {
Future<List<CacheObject>> getObjectsOverCapacity(
int capacity, {
Duration? maxAge,
}) async {
return CacheObject.fromMapList(
await db!.query(
_tableCacheObject,
Expand All @@ -169,7 +172,7 @@ class CacheObjectProvider extends CacheInfoRepository
where: '${CacheObject.columnTouched} < ?',
whereArgs: [
DateTime.now()
.subtract(const Duration(days: 1))
.subtract(maxAge ?? const Duration(days: 1))
.millisecondsSinceEpoch,
],
limit: 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,20 @@ class JsonCacheInfoRepository extends CacheInfoRepository
}

@override
Future<List<CacheObject>> getObjectsOverCapacity(int capacity) async {
final allSorted = _cacheObjects.values.toList()
..sort((c1, c2) => c1.touched!.compareTo(c2.touched!));
if (allSorted.length <= capacity) return [];
return allSorted.getRange(0, allSorted.length - capacity).toList();
Future<List<CacheObject>> getObjectsOverCapacity(
int capacity, {
Duration? maxAge,
}) async {
final threshold = DateTime.now().subtract(
maxAge ?? const Duration(days: 1),
);
final filtered =
_cacheObjects.values
.where((c) => c.touched!.isBefore(threshold))
.toList()
..sort((c1, c2) => c1.touched!.compareTo(c2.touched!));
if (filtered.length <= capacity) return [];
return filtered.getRange(0, filtered.length - capacity).toList();
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class NonStoringObjectProvider implements CacheInfoRepository {
}

@override
Future<List<CacheObject>> getObjectsOverCapacity(int capacity) {
Future<List<CacheObject>> getObjectsOverCapacity(
int capacity, {
Duration? maxAge,
}) {
return Future.value([]);
}

Expand Down
Loading