get<T> method
Retrieves a value from the cache by its identifier.
key - Unique identifier of the value
Returns the stored value or null if it doesn't exist or has expired
Implementation
@override
Future<T?> get<T>(String key) async {
await _ensureInitialized();
final metadata = _metadataCache[key];
if (metadata == null) {
_misses++;
await _saveStats();
return null;
}
// Check if expired
if (metadata.isExpired) {
await remove(key);
_misses++;
await _saveStats();
return null;
}
// Get value from SharedPreferences
final serializedValue = _prefs!.getString('$_keyPrefix$key');
if (serializedValue == null) {
// Inconsistent state, remove metadata
await remove(key);
_misses++;
await _saveStats();
return null;
}
// Deserialize value
final value = _deserializeValue(serializedValue, metadata.dataType);
// Update access information
final now = DateTime.now();
metadata.lastAccessedAt = now;
metadata.accessCount++;
// Update metadata in storage and cache
await _prefs!.setString('$_metadataPrefix$key', metadata.toJson());
_hits++;
await _saveStats();
return value as T?;
}