-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add generalized cache handling * Update entry.dart * Update constants.dart * Update pubspec.yaml * Update README.md
- Loading branch information
Showing
16 changed files
with
158 additions
and
163 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
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,6 @@ | ||
const version = '2.5.2'; | ||
const version = '3.0.0'; | ||
const configJsonCacheVersion = 'v2'; | ||
const configJsonName = 'config_v5.json'; | ||
final DateTime distantPast = DateTime.utc(1970, 01, 01); | ||
final DateTime distantFuture = | ||
DateTime.now().toUtc().add(const Duration(days: 1000)); | ||
DateTime.now().toUtc().add(const Duration(days: 1000 * 365)); |
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,53 @@ | ||
import 'dart:convert'; | ||
|
||
import 'constants.dart'; | ||
import 'json/config.dart'; | ||
|
||
class Entry { | ||
final Config config; | ||
final String configJsonString; | ||
final String eTag; | ||
final DateTime fetchTime; | ||
|
||
Entry(this.configJsonString, this.config, this.eTag, this.fetchTime); | ||
|
||
bool get isEmpty => identical(this, empty); | ||
|
||
Entry withTime(DateTime time) => Entry(configJsonString, config, eTag, time); | ||
|
||
static Entry empty = Entry('', Config.empty, '', distantPast); | ||
|
||
String serialize() { | ||
return '${fetchTime.millisecondsSinceEpoch}\n$eTag\n$configJsonString'; | ||
} | ||
|
||
static Entry fromConfigJson(String configJson, String eTag, DateTime time) { | ||
final decoded = jsonDecode(configJson); | ||
final config = Config.fromJson(decoded); | ||
return Entry(configJson, config, eTag, time); | ||
} | ||
|
||
static Entry fromCached(String cached) { | ||
final timeIndex = cached.indexOf('\n'); | ||
if (timeIndex == -1) { | ||
throw FormatException("Number of values is fewer than expected."); | ||
} | ||
|
||
final eTagIndex = cached.indexOf('\n', timeIndex + 1); | ||
if (eTagIndex == -1) { | ||
throw FormatException("Number of values is fewer than expected."); | ||
} | ||
|
||
final timeString = cached.substring(0, timeIndex); | ||
final time = int.tryParse(timeString); | ||
if (time == null) { | ||
throw FormatException("Invalid fetch time: $timeString"); | ||
} | ||
|
||
final fetchTime = DateTime.fromMillisecondsSinceEpoch(time, isUtc: true); | ||
final eTag = cached.substring(timeIndex + 1, eTagIndex); | ||
final configJson = cached.substring(eTagIndex + 1); | ||
|
||
return fromConfigJson(configJson, eTag, fetchTime); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.