-
-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from zino-app/cache-1
Implement in-memory cache and offline caching.
- Loading branch information
Showing
14 changed files
with
325 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,8 @@ | |
"code", | ||
"doc", | ||
"example", | ||
"ideas" | ||
"ideas", | ||
"review" | ||
] | ||
}, | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.vs/ | ||
|
||
.DS_Store | ||
.dart_tool/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
library graphql_flutter; | ||
|
||
export './src/client.dart'; | ||
export './src/query.dart'; | ||
export './src/mutation.dart'; | ||
|
||
export './src/cache/in_memory.dart'; | ||
|
||
export './src/widgets/query.dart'; | ||
export './src/widgets/mutation.dart'; | ||
export './src/widgets/cache_provider.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import 'dart:async'; | ||
import 'dart:collection'; | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:path_provider/path_provider.dart'; | ||
|
||
class InMemoryCache { | ||
HashMap<String, dynamic> _inMemoryCache = new HashMap<String, dynamic>(); | ||
|
||
Future<String> get _localStoragePath async { | ||
final Directory directory = await getApplicationDocumentsDirectory(); | ||
|
||
return directory.path; | ||
} | ||
|
||
Future<File> get _localStorageFile async { | ||
final String path = await _localStoragePath; | ||
|
||
return File('$path/cache.txt'); | ||
} | ||
|
||
Future<dynamic> _writeToStorage() async { | ||
final File file = await _localStorageFile; | ||
IOSink sink = file.openWrite(); | ||
|
||
_inMemoryCache.forEach((key, value) { | ||
sink.writeln(json.encode([key, value])); | ||
}); | ||
|
||
sink.close(); | ||
|
||
return sink.done; | ||
} | ||
|
||
Future<HashMap<String, dynamic>> _readFromStorage() async { | ||
try { | ||
final File file = await _localStorageFile; | ||
Stream<dynamic> inputStream = file.openRead(); | ||
|
||
final HashMap<String, dynamic> storedHashMap = | ||
new HashMap<String, dynamic>(); | ||
|
||
inputStream | ||
.transform(utf8.decoder) // Decode bytes to UTF8. | ||
.transform(new LineSplitter()) // Convert stream to individual lines. | ||
.listen((String line) { | ||
final List keyAndValue = json.decode(line); | ||
|
||
storedHashMap[keyAndValue[0]] = keyAndValue[1]; | ||
}); | ||
|
||
return storedHashMap; | ||
} on FileSystemException { | ||
// TODO: handle No such file | ||
|
||
return new HashMap<String, dynamic>(); | ||
} catch (error) { | ||
// TODO: handle error | ||
print(error); | ||
|
||
return new HashMap<String, dynamic>(); | ||
} | ||
} | ||
|
||
bool hasEntity(String key) => _inMemoryCache.containsKey(key); | ||
|
||
void save() async { | ||
await _writeToStorage(); | ||
} | ||
|
||
void restore() async { | ||
_inMemoryCache = await _readFromStorage(); | ||
} | ||
|
||
dynamic read(String key) { | ||
if (hasEntity(key)) { | ||
return _inMemoryCache[key]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
void write(String key, dynamic value) { | ||
_inMemoryCache[key] = value; | ||
} | ||
|
||
void reset() { | ||
_inMemoryCache.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.