chore: refresh SVG cache once a day (#2776)

This commit is contained in:
ggurdin 2025-05-13 09:24:54 -04:00 committed by GitHub
parent bf881ac0d4
commit 1260e056b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -26,8 +26,6 @@ class CustomizedSvg extends StatefulWidget {
/// If you want to keep the aspect ratio, set only the width
final double? height;
static final GetStorage _svgStorage = GetStorage('svg_cache');
const CustomizedSvg({
super.key,
required this.svgUrl,
@ -47,6 +45,8 @@ class _CustomizedSvgState extends State<CustomizedSvg> {
bool _hasError = false;
bool _showProgressIndicator = false;
static final GetStorage _svgStorage = GetStorage('svg_cache');
@override
void initState() {
super.initState();
@ -77,8 +77,6 @@ class _CustomizedSvgState extends State<CustomizedSvg> {
}
}
final DateTime _cacheClearDate = DateTime(2025, 4, 13);
Future<void> _loadSvg() async {
try {
final cached = _getSvgFromCache();
@ -116,7 +114,7 @@ class _CustomizedSvgState extends State<CustomizedSvg> {
"svgUrl": widget.svgUrl,
},
);
await CustomizedSvg._svgStorage.write(
await _svgStorage.write(
widget.svgUrl,
{'timestamp': DateTime.now().millisecondsSinceEpoch},
);
@ -124,7 +122,7 @@ class _CustomizedSvgState extends State<CustomizedSvg> {
}
final String svgContent = response.body;
await CustomizedSvg._svgStorage.write(widget.svgUrl, {
await _svgStorage.write(widget.svgUrl, {
'svg': svgContent,
'timestamp': DateTime.now().millisecondsSinceEpoch,
});
@ -141,13 +139,13 @@ class _CustomizedSvgState extends State<CustomizedSvg> {
}
String? _getSvgFromCache() {
final cachedSvgEntry = CustomizedSvg._svgStorage.read(widget.svgUrl);
final cachedSvgEntry = _svgStorage.read(widget.svgUrl);
if (cachedSvgEntry != null &&
cachedSvgEntry is Map<String, dynamic> &&
cachedSvgEntry['svg'] is String &&
cachedSvgEntry['timestamp'] is int &&
DateTime.fromMillisecondsSinceEpoch(cachedSvgEntry['timestamp'])
.isAfter(_cacheClearDate)) {
.isAfter(DateTime.now().subtract(const Duration(days: 1)))) {
return _modifySVG(cachedSvgEntry['svg'] as String);
}
return null;