搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目

暂无相关数据。