increment method

  1. @override
Future<int> increment(
  1. String key, [
  2. int amount = 1
])
override

Increment the value of an item in the cache.

  • Parameters:
    • key: The key of the item to increment.
    • amount: The amount to increment by (default is 1).
  • Returns: The new value of the item.

Implementation

@override
Future<int> increment(String key, [int amount = 1]) async {
  final value = await get(key);
  final int currentValue =
      (value is int) ? value : int.tryParse(value.toString()) ?? 0;
  final newValue = currentValue + amount;
  await put(
    key,
    newValue,
    const Duration(days: 365 * 100),
  ); // Long TTL for counters usually
  return newValue;
}