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');
}
try {
final result = await _executeCommand(['EXISTS', key], isRead: true);
if (result == 1) {
return true;
} else {
_stats.misses++;
_stats.hits--; // Correct the hit count since EXISTS returned 0
return false;
}
} catch (e) {
_stats.misses++;
_stats.hits--; // Correct the hit count
throw CacheException('Failed to check cache item "$key": $e');
}
}