Search packages, repositories and app categories

Package detailsv0.3.0

ttl_cache

A simple key-value store whose entires expire after a user-defined time-to-live (TTL)

AndroidiOSLinuxmacOSWebWindows
30-day downloads
103K
Likes
3
Pub points
160

Install package

flutter pub add ttl_cache

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

ttl_cache is a lightweight, Flutter-compatible package that provides a key-value store with automatic expiration of entries based on a user-defined time-to-live (TTL). It enables efficient temporary data storage by removing stale entries after their TTL expires, reducing memory usage and ensuring data freshness. Ideal for caching API responses, session data, or any transient information. The package supports all major platforms including Android, iOS, Web, and desktop environments.

Use cases

  • Caching API responses
  • Storing session tokens
  • Temporary data persistence
  • Reducing redundant network calls
  • Managing short-lived configuration
  • Improving app performance

Key features

  • Automatic entry expiration
  • Cross-platform support
  • Simple key-value interface
  • Configurable TTL per entry
  • Memory-efficient design
  • Thread-safe operations

Suitable for

  • Apps needing short-term data storage
  • Developers seeking lightweight caching
  • Projects requiring automatic cleanup
  • Real-time data with freshness guarantees
  • High-performance applications

Considerations

  • Entries are not persisted across app restarts
  • TTL precision depends on system clock
  • Not suitable for long-term storage
  • No built-in serialization
  • Limited to in-memory storage
  • Requires manual management of large datasets

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

ttl_cache

Powered by Mason License: MIT

A simple Dart key-value store with optional entry expiration.

Usage

A simple usage example:

import 'package:ttl_cache/ttl_cache.dart';

void main() {
  final cache = TtlCache<String, int>(defaultTtl: Duration(seconds: 1));

  // Set 'a' and 'b' with a default TTL of 1 second.
  // These lines are equivalent.
  cache['a'] = 1;
  cache.set('b', 2);

  // Set 'c' with a TTL of 3 seconds.
  cache.set('c', 3, ttl: Duration(seconds: 3));

  // Set 'd' with no TTL. This entry will remain in the cache until it is
  // manually removed.
  cache.set('d', 4, ttl: null);

  print(cache['a']); // 1
  print(cache['b']); // 2
  print(cache['c']); // 3
  print(cache['d']); // 4

  // Entries with the default TTL will have expired. 'c' has a TTL of 3 seconds
  // and will still be available.
  Future.delayed(Duration(seconds: 2), () {
    print(cache['a']); // null
    print(cache['b']); // null
    print(cache['c']); // 3
    print(cache['d']); // 4
  });

  // All entries from above with TTLs will have expired. 'd' has no TTL and will
  // still be available.
  Future.delayed(Duration(seconds: 4), () {
    // 'a' and 'b' will still be null
    print(cache['c']); // null
    print(cache['d']); // 4
  });
}

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package

No data available yet.

Rankings