搜尋套件、儲存庫與 APP 分類

套件詳情v1.0.0+4

cache_manager

快取管理員用於儲存本機快取。快取管理員是用來在您的 Flutter 應用程式中將各種資料類型(JSON、字串、整數等)作為本機快取儲存的包裝器。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
4
喜歡數
48
Pub 分數
140

安裝套件

flutter pub add cache_manager

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

快取管理員是一個 Flutter 套件,旨在簡化各種資料類型(如 JSON、字串和整數)的本機快取。它提供了一個易於使用的包裝器,可在 Android、iOS、Web、macOS、Windows 和 Linux 等多個平台儲存和檢索快取資料。該套件有效處理檔案儲存,支援快取過期,並可無縫整合至 Flutter 應用程式中,透過減少重複的網路請求來提升效能。非常適合管理暫時性或經常存取的資料。

適用情境

  • 快取 API 回應
  • 將用戶偏好本地儲存
  • 持久化圖像和媒體檔案
  • 減少重複請求的網路負載
  • 管理離線資料存取

主要特色

  • 支援多種資料類型
  • 跨平台相容性
  • 自動快取過期
  • 用於讀寫操作的簡單 API
  • 內建檔案系統整合

適合對象

  • 頻繁進行網路呼叫的應用
  • 離線優先的應用程式
  • 媒體密集型應用
  • 數據密集型Flutter專案
  • 尋求輕量級快取的開發者

使用建議

  • 大型數據集需要手動清理快取
  • 有限的高級快取策略
  • 敏感資料沒有內建加密
  • 在某些平台依賴檔案系統權限
  • 未針對極高頻率的快取更新進行優化

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

⚡Cache Manager

A tidy utility to handle cache of your flutter app like a Boss. It provides support for both iOS and Android platforms (offcourse).

🎖Installing

dependencies:
  cache_manager: ^<latest_version>

⚽Awesome Features

🍧Cache Utilities

🌟CacheManagerUtils.conditionalCache({key, valueType, actionIfNull, actionIfNotNull})

  • Conditional builder based on the value of cache. actionIfNull & actionIfNotNull are dynamic arguments used for Navigation through views, Rendering UI, Debugging values etc. The valueType refers to the datatype of cache (StringValue, BoolValue, IntValue, DoubleValue)

🌟CacheManagerUtils.cacheTextBuilder(textStyle, cacheKey})

  • TextBuilder for the cached value. cacheKey is the key used to address the cache. The builder will return 'Invalid cache' if no cache is missing. Use textStyle to style the value of the cache.
👀Read cache

🌟ReadCache.getJson(key) : Get JSON stored as cache.

🌟ReadCache.getString(key) : Get string stored as cache.

🌟ReadCache.getBool(key) : Get boolean stored as cache.

🌟ReadCache.getInt(key) : Get integer stored as cache.

🌟ReadCache.getDouble(key) : Get double stored as cache.

🖊Write cache

🌟WriteCache.setJson(key,value) : Set JSON as cache.

🌟WriteCache.setString(key,value) : Set a string as cache.

🌟WriteCache.setInt(key,value) : Set an integer as cache.

🌟WriteCache.setBool(key,value) : Set a boolean as cache.

🌟WriteCache.setDouble(key,value) : Set a double as cache.

🌟WriteCache.setListString(key,value) : Set a List of string as cache.

❌Delete cache

🌟DeleteCache.deleteKey(key,[takeAction]) : Delete the cache and perform an action when cache is deleted(Optional).

Example : Login flow with caching userID

//Attached function in a login view
Future<String?> login({
    required BuildContext context,
    required String email,
    required String password,
  }) async {
    try {
      var userId = await _authenticationService.login(
          context: context, email: email, password: password);
      await WriteCache.setString(key: "cache", value: userId!);
    } catch (e) {
      print(e); //Do something if error occurs
   }
 }
 
//Splash view
import 'dart:async';
import 'package:cache_manager/cache_manager.dart';
import 'package:flutter/material.dart';

class SplashView extends StatefulWidget {
  @override
  _SplashViewState createState() => _SplashViewState();
}

class _SplashViewState extends State<SplashView> {
  Future initiateCache() async {
    return CacheManagerUtils.conditionalCache(
        key: "cache",
        valueType: ValueType.StringValue,
        actionIfNull: () {
          Navigator.of(context).pushNamed(AppRoutes.LoginRoute);
        },
        actionIfNotNull: () {
          Navigator.of(context).pushNamed(AppRoutes.HomeRoute);
        });
  }

  @override
  void initState() {
    Timer(Duration(seconds: 1), initiateCache);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("demo app"),
      ),
    );
  }
}

//Home view
import 'package:cache_manager/core/cache_manager_utils.dart';
import 'package:cache_manager/core/delete_cache_service.dart';
import 'package:flutter/material.dart';

class HomeView extends StatefulWidget {
  @override
  _HomeViewState createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CacheManagerUtils.cacheTextBuilder(
            textStyle: TextStyle(color: Colors.white), cacheKey: "cache"),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          DeleteCache.deleteKey(
              "cache", Navigator.of(context).pushNamed(AppRoutes.LoginRoute));
        },
      ),
    );
  }
}
❤Loved the utility? Donate here.
🚀Want to learn more about Flutter? Checkout this out!
💥DM me on Instagram for doubts Follow here

🐛 Bugs/Requests

If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket on Github and I'll look into it. Pull request are also welcome.

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。