getCDNConfig method

CDNConfig? getCDNConfig()

Gets CDN config from cache if valid, otherwise returns null

Implementation

CDNConfig? getCDNConfig() {
  final configStr = _cache[_configCacheKey];
  final timestampStr = _cache[_configTimestampKey];

  if (configStr == null || timestampStr == null) {
    return null;
  }

  final timestamp = int.tryParse(timestampStr);
  if (timestamp == null) return null;

  final currentTime = DateTime.now().millisecondsSinceEpoch;

  if (currentTime - timestamp > _cacheDuration) {
    return null;
  }

  try {
    return CDNConfig.fromJson(jsonDecode(configStr) as Map<String, dynamic>);
  } catch (error) {
    // ignore: avoid_print
    print('Error parsing cached CDN config: $error');
    return null;
  }
}