具有泛型的磁盘缓存 我是一位来自JavaScript和PHP的程序员,并且正在Flutter上开发App,并且难以在手机的内部存储中实现缓存。
我想了解并知道是否可以为JSON创建带有序列化的泛型类型类,以便可以将其存储在文件中。 我已经在内存中完成了Cache的实现,并且运行良好,再加上磁盘上的Cache的实现,我遇到了困难。
内存缓存代码
class MemCache<T> extends Cache<T> {
Duration cacheValidDuration = Duration(minutes: 30);
DateTime lastFetchTime = DateTime.fromMillisecondsSinceEpoch(0);
RList<T> allRecords = RList<T>();
//is updade cache
bool isShouldRefresh() {
return (null == allRecords ||
allRecords.isEmpty ||
null == lastFetchTime ||
lastFetchTime.isBefore(DateTime.now().subtract(cacheValidDuration)));
}
@override
Future<RList<T>> getAll() {
return Future.value(allRecords);
}
@override
Future<void> putAll(RList<T> objects) {
allRecords.addAll(objects);
lastFetchTime = DateTime.now();
}
@override
Future<void> putAllAsync(Future<RList<T>> objects) async {
allRecords = await objects;
}
}
磁盘缓存代码 如何从泛型调用序列化方法
class DiskCache<T> extends Cache<T> {
Duration cacheValidDuration = Duration(minutes: 30);
DateTime lastFetchTime = DateTime.fromMillisecondsSinceEpoch(0);
RList<T> allRecords = RList<T>();
//is update the cache
bool isShouldRefresh() {
return (null == allRecords ||
allRecords.isEmpty ||
null == lastFetchTime ||
lastFetchTime.isBefore(DateTime.now().subtract(cacheValidDuration)));
}
@override
Future<RList<T>> getAll() async {
await _readFromDisk();
return Future.value(allRecords);
}
@override
Future<void> putAll(RList<T> objects) async {
allRecords.addAll(objects);
lastFetchTime = DateTime.now();
await _writeToDisk();
}
@override
Future<void> putAllAsync(Future<RList<T>> objects) async {
allRecords = await objects;
lastFetchTime = DateTime.now();
await _writeToDisk();
}
//parth
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
//file pointer
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/allRecords.json');
}
//write to file
Future<File> _writeToDisk() async {
try {
File recordedFile;
if (allRecords != null) {
var map = allRecords.map((c) {
var item = c as ISerialization;
return item.toJson();
}).toList();
var jsonString = jsonEncode(map);
final file = await _localFile;
// Write the file
recordedFile = await file.writeAsString(jsonString);
}
return recordedFile;
} catch (e) {
print("writeToDisk: " + e.toString());
return null;
}
}
// ************************** issues on this part ******************
Future<RList<T>> _readFromDisk() async {
try {
final file = await _localFile;
// Read the file
String contents = await file.readAsString();
var parsedJson = jsonDecode(contents);
if (allRecords == null) {
allRecords = RList<T>();
}
allRecords.clear();
for (var item in parsedJson) {
// ************************** issues on this part ******************
print(T.fromMap(item));
allRecords.add(T.fromMap(item));
}
return allRecords;
} catch (e) {
print("readFromDisk: " + e.toString());
return null;
}
}
}
错误:未为类“ Type”定义方法“ fromMap”。 -“类型”来自“ dart:core”。 尝试将名称更正为现有方法的名称,或定义一个名为“ fromMap”的方法。 allRecords.add(T.fromMap(item)); ^^^^^^^^