set method

Future<void> set(
  1. dynamic key,
  2. dynamic data, {
  3. Duration? ttl,
})

Manually set data in cache Key can be a String or Map<String, dynamic (like React Query) Users must pass already serialized/prepared data

Implementation

Future<void> set(dynamic key, dynamic data, {Duration? ttl}) async {
  final normalizedKey = _normalizeKey(key);

  // Calculate size based on JSON string representation
  final size = _calculateSize(data);

  // Check if eviction needed
  await _evictIfNeeded(size);

  // Create cache entry
  final now = DateTime.now();
  final entry = CacheEntry(
    data: data,
    timestamp: now,
    lastAccessTime: now,
    accessCount: 1,
    sizeInBytes: size,
    ttl: ttl, // Store per-key TTL
  );

  // Store in cache
  _metadata[normalizedKey] = entry;
  await storage.write(normalizedKey, jsonEncode(entry.toJson()));
}