@mainawycliffe have you made any progress on this?
@smkhalsa I am just getting started
@mainawycliffe how is this coming along? I am interested in this feature too. Need any help?
@kmcgill88 I have been busy over the last few weeks and haven't made as much progress as I would like. I have done the basics, but I will do my best and see if I can wrap up this over the next two weeks.
@mainawycliffe Do you have any plans to have Duration based cache? My use case needs a query cached for a Duration, say 30 minutes, before marking the data as expired and re-query. I'd be happy to brainstorm and contribute to the feature.
Yes, that can be a useful feature - passing a timeout would be quite useful. I think all cache packages and types should be able to do this.
@mainawycliffe any update on this. We are struggling with the limitations of the current cache. See #423
Sorry been busy at work, will get back to it over the weekend.
For now we just extended the OptimisticCache and changed the _writeToStorage and _readFromStorage so that in stead of writing it to a file manually, we put it in a hive box. So you can call save at any time without blocking the UI. We also toggle between 2 boxes to prevent interruptions while persisting the cache.
Future<dynamic> _writeToStorage() async {
if (_writingToStorage) {
return;
}
_writingToStorage = true;
try {
final cacheKeyBox = await Hive.openBox('cacheKeyBox');
String currentKey = await cacheKeyBox.get('currentKey') ?? 'cache1';
String newKey = currentKey == 'cache1' ? 'cache2' : 'cache1';
final cache = await Hive.openBox(newKey);
await cache.clear();
var promises = <Future>[];
data.forEach((String key, dynamic value) {
promises.add(cache.add(([key, value])));
});
await Future.wait(promises);
await cache.close();
cacheKeyBox.put('currentKey', newKey);
_writingToStorage = false;
} catch (err) {
_writingToStorage = false;
rethrow;
}
return;
}
Future<HashMap<String, dynamic>> _readFromStorage() async {
// return in-memory data when there is data, data can be a new HashMap
if (data != null && data.isNotEmpty) {
return data;
}
try {
final cacheKeyBox = await Hive.openBox('cacheKeyBox');
String currentKey = cacheKeyBox.get('currentKey') ?? 'cache1';
final cache = await Hive.openBox(currentKey);
final values = cache.values;
final HashMap<String, dynamic> storedHashMap = HashMap<String, dynamic>();
values.forEach((keyAndValue) {
storedHashMap[keyAndValue[0] as String] = jsonDecode(jsonEncode(keyAndValue[1]));
});
return storedHashMap;
} catch (error) {
// TODO: handle error
print(error);
return HashMap<String, dynamic>();
}
}
@mainawycliffe do you have any WIP repo so we can help with contribution?
Should be in my folk, but there are also lots of uncommitted changes.
If you will get to some kind of MVP post the link here)
@mainawycliffe I'm not sure how far you've gotten with this, but feel free to take anything you find useful from my hive_store implementation in gql_client.
If I understood correct, guys made something here https://pub.dev/packages/ferry
I'll try to use it in my app and let know how it works.
@jensvrai Can you please share full code. Can't understand how you overrode _writingToStorage
Thank you!
essentially closed via HiveStore in 4.0.0-beta.1 (#648)