搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。

所在榜單