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
44 changes: 23 additions & 21 deletions flutter_cache_manager_firebase/lib/src/firebase_cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@ import 'firebase_http_file_service.dart';
class FirebaseCacheManager extends CacheManager {
static const key = 'firebaseCache';

static final FirebaseCacheManager _instance = FirebaseCacheManager._(
retryOptions: _retryOptions,
bucket: _bucket,
);

static RetryOptions? _retryOptions;

static String? _bucket;
static final Map<String, FirebaseCacheManager> _instances = {};

factory FirebaseCacheManager({RetryOptions? retryOptions, String? bucket}) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (optional): the factory now returns one shared instance per bucket, and retryOptions only takes effect on the first call for that bucket; later calls with different options get the existing instance unchanged. Neither is documented. AGENTS.md asks for "Public API documented with /// doc comments where applicable". Fix: add a /// comment on the factory that says both.

_bucket = bucket;
_retryOptions = retryOptions;
return _instance;
final cacheKey = bucket ?? key;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: the raw bucket name is now the Config cache key, and that key becomes both the sqflite/JSON database name and the cache directory name under the app's temp dir. A bucket whose name matches another cache manager's key in the app (for example libCachedImageData, or firebaseCache itself) shares that manager's database and files, and bucket: '' gives key '', so the files land in the temp dir root and the database is named .db. Users who already passed bucket: also find their cache under a new name and the old firebaseCache entries left behind.

Fix: namespace the key, for example:

Suggested change
final cacheKey = bucket ?? key;
final cacheKey = bucket == null ? key : '${key}_$bucket';

(and update the test expectations to match).

return _instances.putIfAbsent(
cacheKey,
() => FirebaseCacheManager._(
key: cacheKey,
retryOptions: retryOptions,
bucket: bucket,
),
);
}

FirebaseCacheManager._({RetryOptions? retryOptions, String? bucket})
: super(
Config(
key,
fileService: FirebaseHttpFileService(
retryOptions: retryOptions,
bucket: bucket,
),
),
);
FirebaseCacheManager._({
required String key,
RetryOptions? retryOptions,
String? bucket,
}) : super(
Config(
key,
fileService: FirebaseHttpFileService(
retryOptions: retryOptions,
bucket: bucket,
),
),
);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,59 @@
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:flutter_cache_manager_firebase/flutter_cache_manager_firebase.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

const pathProviderChannel = MethodChannel('plugins.flutter.io/path_provider');
late Directory tempDir;

setUp(() {
tempDir = Directory.systemTemp.createTempSync('fcm_firebase_test_');
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(pathProviderChannel, (call) async {
return tempDir.path;
});
});

tearDown(() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (optional): _instances is a static cache that outlives each test, but this tearDown deletes the temp directory those instances were built against. A second test that calls FirebaseCacheManager(bucket: 'bucket-a') gets the cached instance whose path_provider directory is gone, so it fails or writes outside the mock. Fix: use unique bucket names per test, or add a @visibleForTesting way to clear _instances and call it here.

TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(pathProviderChannel, null);
if (tempDir.existsSync()) {
tempDir.deleteSync(recursive: true);
}
});

test('FirebaseCacheManager key is set', () {
expect(FirebaseCacheManager.key, 'firebaseCache');
});

test('FirebaseCacheManager returns separate instances per bucket', () {
final bucketA = FirebaseCacheManager(bucket: 'bucket-a');
final bucketAAgain = FirebaseCacheManager(bucket: 'bucket-a');
final bucketB = FirebaseCacheManager(bucket: 'bucket-b');
final defaultInstance = FirebaseCacheManager();

expect(identical(bucketA, bucketAAgain), isTrue);
expect(identical(bucketA, bucketB), isFalse);
expect(identical(bucketA, defaultInstance), isFalse);

expect(bucketA.config.cacheKey, 'bucket-a');
expect(
(bucketA.config.fileService as FirebaseHttpFileService).bucket,
'bucket-a',
);
expect(bucketB.config.cacheKey, 'bucket-b');
expect(
(bucketB.config.fileService as FirebaseHttpFileService).bucket,
'bucket-b',
);
expect(defaultInstance.config.cacheKey, FirebaseCacheManager.key);
expect(
(defaultInstance.config.fileService as FirebaseHttpFileService).bucket,
isNull,
);
});
}