⚡缓存管理器

一个整洁的实用工具,可以像老板一样处理您的 Flutter 应用的缓存。它支持 iOS 和 Android 平台(当然)。

?安装

dependencies:
  cache_manager: ^<latest_version>

⚽绝佳功能

?缓存工具

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

  • 基于缓存值的条件构建器。actionIfNullactionIfNotNull 是用于导航视图、渲染 UI、调试值等的动态参数。valueType 指的是缓存的数据类型(StringValueBoolValueIntValueDoubleValue)。

?CacheManagerUtils.cacheTextBuilder(textStyle, cacheKey})

  • 缓存值的 TextBuilder。cacheKey 是用于寻址缓存的键。如果缓存缺失,构建器将返回“缓存无效”。使用 textStyle 为缓存值设置样式。

?读取缓存

?ReadCache.getJson(key):获取存储为缓存的 JSON。

?ReadCache.getString(key):获取存储为缓存的字符串。

?ReadCache.getBool(key):获取存储为缓存的布尔值。

?ReadCache.getInt(key):获取存储为缓存的整数。

?ReadCache.getDouble(key):获取存储为缓存的双精度值。

?写入缓存

?WriteCache.setJson(key,value):将 JSON 设置为缓存。

?WriteCache.setString(key,value):将字符串设置为缓存。

?WriteCache.setInt(key,value):将整数设置为缓存。

?WriteCache.setBool(key,value):将布尔值设置为缓存。

?WriteCache.setDouble(key,value):将双精度值设置为缓存。

?WriteCache.setListString(key,value):将字符串列表设置为缓存。

❌删除缓存

?DeleteCache.deleteKey(key,[takeAction]):删除缓存并在缓存删除时执行操作(可选)。

示例:带有缓存用户 ID 的登录流程

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

❤喜欢这个实用工具?在此捐赠

?想了解更多关于 Flutter 的信息?看看这个!

?如有疑问,请在 Instagram 上私信我在此关注

? Bug/请求

如果您遇到任何问题,请随时打开一个 issue。如果您觉得该库
缺少某个功能,请在 Github 上提交一个 ticket,我会处理。
Pull request 也欢迎。

GitHub

https://github.com/abhishh1/cache_manager