104 lines
3.1 KiB
Dart
104 lines
3.1 KiB
Dart
// ignore_for_file: implementation_imports
|
|
|
|
import 'package:dart_blowfish/dart_blowfish.dart';
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
import 'package:flutter_cache_manager/src/storage/cache_object.dart';
|
|
import 'package:freezer/type_adapters/uri.dart';
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
|
|
class FreezerCacheManager extends CacheManager {
|
|
static const key = 'freezerImageCache';
|
|
|
|
void init(String path, {String? boxName}) {
|
|
_instance =
|
|
FreezerCacheManager._(FreezerCacheInfoRepository(boxName ?? key, path));
|
|
}
|
|
|
|
static late final FreezerCacheManager _instance;
|
|
factory FreezerCacheManager() => _instance;
|
|
|
|
FreezerCacheManager._(FreezerCacheInfoRepository repo)
|
|
: super(Config(key, repo: repo));
|
|
}
|
|
|
|
class FreezerCacheInfoRepository extends CacheInfoRepository {
|
|
final String boxName;
|
|
final String path;
|
|
late final LazyBox<CacheObject> _box;
|
|
bool _isOpen;
|
|
FreezerCacheInfoRepository(this.boxName, this.path);
|
|
|
|
@override
|
|
Future<bool> exists() => Hive.boxExists(boxName, path: path);
|
|
|
|
@override
|
|
Future<bool> open() async {
|
|
if (_isOpen) return true;
|
|
|
|
_box = await Hive.openLazyBox<CacheObject>(boxName, path: path);
|
|
_isOpen = true;
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
Future<dynamic> updateOrInsert(CacheObject cacheObject) {
|
|
if (cacheObject.id == null) {
|
|
return insert(cacheObject);
|
|
} else {
|
|
return update(cacheObject);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<CacheObject> insert(CacheObject cacheObject,
|
|
{bool setTouchedToNow = true}) {
|
|
final id = await _box.add(cacheObject);
|
|
}
|
|
|
|
/// Gets a [CacheObject] by [key]
|
|
@override
|
|
Future<CacheObject?> get(String key);
|
|
|
|
/// Deletes a cache object by [id]
|
|
@override
|
|
Future<int> delete(int id);
|
|
|
|
/// Deletes items with [ids] from the repository
|
|
@override
|
|
Future<int> deleteAll(Iterable<int> ids);
|
|
|
|
/// Updates an existing [cacheObject]
|
|
@override
|
|
Future<int> update(CacheObject cacheObject, {bool setTouchedToNow = true});
|
|
|
|
/// Gets the list of all objects in the cache
|
|
@override
|
|
Future<List<CacheObject>> getAllObjects();
|
|
|
|
/// 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
|
|
@override
|
|
Future<List<CacheObject>> getObjectsOverCapacity(int capacity);
|
|
|
|
/// Returns a list of [CacheObject] that are older than [maxAge]
|
|
@override
|
|
Future<List<CacheObject>> getOldObjects(Duration maxAge);
|
|
|
|
/// Close the connection to the repository. If this is the last connection
|
|
/// to the repository it will return true and the repository is truly
|
|
/// closed. If there are still open connections it will return false;
|
|
@override
|
|
Future<bool> close();
|
|
|
|
/// Deletes the cache data file including all cache data.
|
|
@override
|
|
Future<void> deleteDataFile();
|
|
}
|
|
|
|
class CacheObjectAdapter extends TypeAdapter<CacheObject> {
|
|
@override
|
|
// TODO: implement typeId
|
|
int get typeId => throw UnimplementedError();
|
|
} |