has method

  1. @override
Future<bool> has(
  1. String key
)
override

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: true if the key exists and is valid, false otherwise.

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');
  }
}