搜索插件、代码仓与 APP 分类

插件详情v0.3.0

ttl_cache

一个简单的键值存储,其条目在用户定义的存活时间(TTL)后过期

AndroidiOSLinuxmacOSWebWindows
近 30 天下载
10.3万
喜欢数
3
Pub 分数
160

安装插件

flutter pub add ttl_cache

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

ttl_cache 是一个轻量级、兼容 Flutter 的包,提供基于用户定义的生存时间(TTL)自动过期条目的键值存储。它通过在 TTL 过期后移除过时条目,实现高效的临时数据存储,降低内存使用并确保数据的新鲜度。适用于缓存 API 响应、会话数据或任何临时信息。该包支持所有主要平台,包括 Android、iOS、Web 和桌面环境。

适合场景

  • 缓存 API 响应
  • 存储会话令牌
  • 临时数据持久化
  • 减少冗余的网络调用
  • 管理短期配置
  • 提升应用性能

主要特点

  • 自动条目过期
  • 跨平台支持
  • 简单的键值接口
  • 每个条目可配置的 TTL
  • 内存高效设计
  • 线程安全操作

适合对象

  • 需要短期数据存储的应用
  • 寻求轻量级缓存的开发者
  • 需要自动清理的项目
  • 具有新鲜度保证的实时数据
  • 高性能应用

使用建议

  • 条目不会在应用重启之间持久化
  • TTL 精度取决于系统时钟
  • 不适合长期存储
  • 无内置序列化
  • 仅限内存存储
  • 需要手动管理大型数据集

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

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

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。

所在榜单