has method
Checks if a key exists in the cache and has not expired.
Returns true if the key is present in the cache and its TTL has not expired,
otherwise returns false.
Throws an exception if the key is null or empty.
- Parameters:
key: A non-null, non-empty string representing the cache key.
- Returns:
trueif the key exists and is valid,falseotherwise.
Implementation
@override
Future<bool> has(String key) async {
if (key.isEmpty) {
throw ArgumentError('Cache key cannot be empty');
}
final entry = _store[key];
if (entry == null) {
_stats.misses++;
return false;
}
if (entry.isExpired()) {
_store.remove(key);
_stats.misses++;
_stats.expirations++;
return false;
}
_stats.hits++;
return true;
}