Flutter 应用加速之本地缓存管理

前言

村里的老人说:“不会写缓存器的码农不是好程序员。

今天我们就来讲讲如何编写一个简单又通用的缓存管理模块。

需求分析

根据以往经验,每一个缓存器,除了要保持被缓存数据/对象之外,还需要同时记录两个与之紧密相关的时间:

  1. 过期时间 expired —— 缓存超过了一定时间,需要更新了,但当前数据仍然有效
  2. 失效时间 deprecated —— 缓存超过了很长时间,当前数据已经无效,不能再用了

在过期时间 expired 之前,缓存数据/对象可直接使用,无需刷新;
超过了过期时间 expired,但未超过失效时间 deprecated,此时缓存数据仍然有效,可以继续使用,但需要委托一个线程去获取最新数据/对象,然后再更新本地缓存;
如果后台线程多次更新失败,当前缓存数据/对象已经严重超时,即超过了 deprecated,此时应该丢弃当前缓存数据/对象,返回空数据/对象给调用者。

模块设计

首先我们设计一个 ```CacheHolder``` 类来保存被缓存数据/对象,以及与之对应的时间信息:

import 'package:object_key/object_key.dart' show Time;


/// Holder for cache value with times in seconds
class CacheHolder <V> {
  CacheHolder(V? cacheValue, double cacheLifeSpan, {double? now})
      : _value = cacheValue, _life = cacheLifeSpan {
    now ??= Time.currentTimestamp;
    _expired = now + cacheLifeSpan;
    _deprecated = now + cacheLifeSpan * 2;
  }

  V? _value;

  final double _life;      // life span (in seconds)
  double _expired = 0;     // time to expired
  double _deprecated = 0;  // time to deprecated

  V? get value => _value;

  /// update cache value with current time in seconds
  void update(V? newValue, {double? now}) {
    _value = newValue;
    now ??= Time.currentTimestamp;
    _expired = now + _life;
    _deprecated = now + _life * 2;
  }

  /// check whether cache is alive with current time in seconds
  bool isAlive({double? now}) {
    now ??= Time.currentTimestamp;
    return now < _expired;
  }

  /// check whether cache is deprecated with current time in seconds
  bool isDeprecated({double? now}) {
    now ??= Time.currentTimestamp;
    return now > _deprecated;
  }

  /// renewal cache with a temporary life span and current time in seconds
  void renewal(double? duration, {double? now}) {
    duration ??= 120;
    now ??= Time.currentTimestamp;
    _expired = now + duration;
    _deprecated = now + _life * 2;
  }

}

该类提供 update() 和 renew() 两个函数来更新缓存信息,前者为获取到最新数据之后调用以更新数据及时间,后者仅刷新一下时间,用以推迟有效时间;
另外提供两个函数 isAlive() 和 isDeprecated(),分别用于判断是否需要更新,以及当前数据是否应该丢弃。

另外,为使 CacheHolder 能适用于任意类型数据/对象,这里使用了“泛型”类型定义。

缓存池

接下来我们需要设计一个缓冲池 ```CachePool```,用于保存同类型的 ```CacheHolder```:

import 'package:object_key/object_key.dart' show Time;
import 'holder.dart';


class CachePair <V> {
  CachePair(this.value, this.holder);

  final V? value;
  final CacheHolder<V> holder;

}


/// Pool for cache holders with keys
class CachePool <K, V> {

  final Map<K, CacheHolder<V>> _holderMap = {};

  Iterable<K> get keys => _holderMap.keys;

  /// update cache holder for key
  CacheHolder<V> update(K key, CacheHolder<V> holder) {
    _holderMap[key] = holder;
    return holder;
  }

  /// update cache value for key with timestamp in seconds
  CacheHolder<V> updateValue(K key, V? value, double life, {double? now}) =>
      update(key, CacheHolder(value, life, now: now));

  /// erase cache for key
  CachePair<V>? erase(K key, {double? now}) {
    CachePair<V>? old;
    if (now != null) {
      // get exists value before erasing
      old = fetch(key, now: now);
    }
    _holderMap.remove(key);
    return old;
  }

  /// fetch cache value & its holder
  CachePair<V>? fetch(K key, {double? now}) {
    CacheHolder<V>? holder = _holderMap[key];
    if (holder == null) {
      // holder not found
      return null;
    } else if (holder.isAlive(now: now)) {
      return CachePair(holder.value, holder);
    } else {
      // holder expired
      return CachePair(null, holder);
    }
  }

  /// clear expired cache holders
  int purge({double? now}) {
    now ??= Time.currentTimestamp;
    int count = 0;
    Iterable allKeys = keys;
    CacheHolder? holder;
    for (K key in allKeys) {
      holder = _holderMap[key];
      if (holder == null || holder.isDeprecated(now: now)) {
        // remove expired holders
        _holderMap.remove(key);
        ++count;
      }
    }
    return count;
  }

}

该缓冲池提供了 3 个接口给应用层使用:

  1. 更新缓存信息;
  2. 删除缓存信息;
  3. 获取缓存信息;

另外还提供一个 purge() 函数给缓存管理器调用,以清除已失效的 CacheHolder。

缓存管理器

最后,我们还需要设计一个缓存管理器 ```CacheManager```,去统一管理所有不同类型的 ```CachePool```:

import 'package:object_key/object_key.dart' show Time;
import 'pool.dart';


class CacheManager {
  factory CacheManager() => _instance;
  static final CacheManager _instance = CacheManager._internal();
  CacheManager._internal();

  final Map<String, dynamic> _poolMap = {};

  ///  Get pool with name
  ///
  /// @param name - pool name
  /// @param <K>  - key type
  /// @param <V>  - value type
  /// @return CachePool
  CachePool<K, V> getPool<K, V>(String name) {
    CachePool<K, V>? pool = _poolMap[name];
    if (pool == null) {
      pool = CachePool();
      _poolMap[name] = pool;
    }
    return pool;
  }

  ///  Purge all pools
  ///
  /// @param now - current time
  int purge(double? now) {
    now ??= Time.currentTimestamp;
    int count = 0;
    CachePool? pool;
    Iterable allKeys = _poolMap.keys;
    for (var key in allKeys) {
      pool = _poolMap[key];
      if (pool != null) {
        count += pool.purge(now: now);
      }
    }
    return count;
  }

}

我们这个缓存管理包括两个接口:

  1. 一个工厂方法 getPool(),用于获取/创建缓存池;
  2. 一个清除接口 purge(),供系统在适当的时候(例如系统内存不足时)调用以释放缓存空间。

至此,一个简单高效的本地缓存管理模块就写好了,下面我们来看看怎么用。

应用示例

假设我们有一个类 MetaTable,其作用是从数据库或者网络中获取 meta 信息,考虑到 I/O 的时间,以及数据解析为对象所消耗的 CPU 时间等,如果该类信息访问十分频繁,我们就需要为它加上一层缓存管理。

先来看看代码:


class MetaTable implements MetaDBI {

  @override
  Future<Meta?> getMeta(ID entity) async {
    // 从数据库中获取 meta 信息
  }

  @override
  Future<bool> saveMeta(Meta meta, ID entity) async {
    // 保存 meta 信息到数据库
  }

}

class MetaCache extends MetaTable {

  final CachePool<ID, Meta> _cache = CacheManager().getPool('meta');

  @override
  Future<Meta?> getMeta(ID entity) async {
    CachePair<Meta>? pair;
    CacheHolder<Meta>? holder;
    Meta? value;
    double now = Time.currentTimeSeconds;
    await lock();
    try {
      // 1. check memory cache
      pair = _cache.fetch(entity, now: now);
      holder = pair?.holder;
      value = pair?.value;
      if (value == null) {
        if (holder == null) {
          // not load yet, wait to load
        } else if (holder.isAlive(now: now)) {
          // value not exists
          return null;
        } else {
          // cache expired, wait to reload
          holder.renewal(128, now: now);
        }
        // 2. load from database
        value = await super.getMeta(entity);
        // update cache
        _cache.updateValue(entity, value, 36000, now: now);
      }
    } finally {
      unlock();
    }
    // OK, return cache now
    return value;
  }

  @override
  Future<bool> saveMeta(Meta meta, ID entity) async {
    _cache.updateValue(entity, meta, 36000, now: Time.currentTimeSeconds);
    return await super.saveMeta(meta, entity);
  }

}

带缓存读数据

当需要读取数据时,先通过 ```_cache.fetch()``` 检查当前缓存池中是否存在有效的值:

如果 (值存在),则 {

    直接返回该值;

}

否则检查 holder;

如果 (holder 存在且未过期),则 {

    说明确实不存在该数据,返回空值;

}

否则调用父类接口获取最新数据;

然后再更新本地缓存。

带缓存写数据

写数据就简单了,只需要在调用父类接口写数据库的同时刷新一下缓存即可。

代码引用

由于我已将这部分代码提交到了 pub.dev,所以在实际应用中,你只需要在项目工程文件 ```pubspec.yaml``` 中添加:

dependencies:

    object_key: ^0.1.1

然后在需要使用的 dart 文件头引入即可:

import 'package:object_key/object_key.dart';

全部源码

/* license: https://mit-license.org
 *
 *  ObjectKey : Object & Key kits
 *
 *                               Written in 2023 by Moky <albert.moky@gmail.com>
 *
 * =============================================================================
 * The MIT License (MIT)
 *
 * Copyright (c) 2023 Albert Moky
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * =============================================================================
 */
import 'package:object_key/object_key.dart' show Time;


/// Holder for cache value with times in seconds
class CacheHolder <V> {
  CacheHolder(V? cacheValue, double cacheLifeSpan, {double? now})
      : _value = cacheValue, _life = cacheLifeSpan {
    now ??= Time.currentTimestamp;
    _expired = now + cacheLifeSpan;
    _deprecated = now + cacheLifeSpan * 2;
  }

  V? _value;

  final double _life;      // life span (in seconds)
  double _expired = 0;     // time to expired
  double _deprecated = 0;  // time to deprecated

  V? get value => _value;

  /// update cache value with current time in seconds
  void update(V? newValue, {double? now}) {
    _value = newValue;
    now ??= Time.currentTimestamp;
    _expired = now + _life;
    _deprecated = now + _life * 2;
  }

  /// check whether cache is alive with current time in seconds
  bool isAlive({double? now}) {
    now ??= Time.currentTimestamp;
    return now < _expired;
  }

  /// check whether cache is deprecated with current time in seconds
  bool isDeprecated({double? now}) {
    now ??= Time.currentTimestamp;
    return now > _deprecated;
  }

  /// renewal cache with a temporary life span and current time in seconds
  void renewal(double? duration, {double? now}) {
    duration ??= 120;
    now ??= Time.currentTimestamp;
    _expired = now + duration;
    _deprecated = now + _life * 2;
  }

}


class CachePair <V> {
  CachePair(this.value, this.holder);

  final V? value;
  final CacheHolder<V> holder;

}


/// Pool for cache holders with keys
class CachePool <K, V> {

  final Map<K, CacheHolder<V>> _holderMap = {};

  Iterable<K> get keys => _holderMap.keys;

  /// update cache holder for key
  CacheHolder<V> update(K key, CacheHolder<V> holder) {
    _holderMap[key] = holder;
    return holder;
  }

  /// update cache value for key with timestamp in seconds
  CacheHolder<V> updateValue(K key, V? value, double life, {double? now}) =>
      update(key, CacheHolder(value, life, now: now));

  /// erase cache for key
  CachePair<V>? erase(K key, {double? now}) {
    CachePair<V>? old;
    if (now != null) {
      // get exists value before erasing
      old = fetch(key, now: now);
    }
    _holderMap.remove(key);
    return old;
  }

  /// fetch cache value & its holder
  CachePair<V>? fetch(K key, {double? now}) {
    CacheHolder<V>? holder = _holderMap[key];
    if (holder == null) {
      // holder not found
      return null;
    } else if (holder.isAlive(now: now)) {
      return CachePair(holder.value, holder);
    } else {
      // holder expired
      return CachePair(null, holder);
    }
  }

  /// clear expired cache holders
  int purge({double? now}) {
    now ??= Time.currentTimestamp;
    int count = 0;
    Iterable allKeys = keys;
    CacheHolder? holder;
    for (K key in allKeys) {
      holder = _holderMap[key];
      if (holder == null || holder.isDeprecated(now: now)) {
        // remove expired holders
        _holderMap.remove(key);
        ++count;
      }
    }
    return count;
  }

}


class CacheManager {
  factory CacheManager() => _instance;
  static final CacheManager _instance = CacheManager._internal();
  CacheManager._internal();

  final Map<String, dynamic> _poolMap = {};

  ///  Get pool with name
  ///
  /// @param name - pool name
  /// @param <K>  - key type
  /// @param <V>  - value type
  /// @return CachePool
  CachePool<K, V> getPool<K, V>(String name) {
    CachePool<K, V>? pool = _poolMap[name];
    if (pool == null) {
      pool = CachePool();
      _poolMap[name] = pool;
    }
    return pool;
  }

  ///  Purge all pools
  ///
  /// @param now - current time
  int purge(double? now) {
    now ??= Time.currentTimestamp;
    int count = 0;
    CachePool? pool;
    Iterable allKeys = _poolMap.keys;
    for (var key in allKeys) {
      pool = _poolMap[key];
      if (pool != null) {
        count += pool.purge(now: now);
      }
    }
    return count;
  }

}

GitHub 地址:

https://github.com/moky/ObjectKey/tree/main/object_key/lib/src/mem

结语

这里向大家展示了一个简单高效的本地缓存管理模块,该模块能有效避免重复创建相同对象,同时也可避免内存泄漏等问题。

合理使用该模块,可以令你的应用程序访问数据的平均速度大幅提升,特别是在重复滚动展示大量数据的列表时,能让你的应用体验更加丝滑。

如有其他问题,可以下载登录 Tarsier​​​​​​​ 与我交流(默认通讯录i找 Albert Moky)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: Flutter应用模板是为开发Flutter应用程序提供的基本框架和结构。它为开发者提供了一种快速启动新项目的方法,减少了从零开始建立应用程序所需的时间和努力。 Flutter应用模板通常包括以下内容: 1. 主界面布局:应用模板通常包含一个主界面布局,作为用户与应用程序进行交互的主要界面。它可以包含各种组件,如按钮、文本框和图像等,用于构建用户界面。 2. 导航栏和底部导航:应用模板通常包含导航栏和/或底部导航栏,用于在应用程序的不同页面之间进行导航。导航栏通常包含应用程序的标题和一些常见的导航按钮,如返回按钮。 3. 状态管理Flutter应用模板通常使用一些状态管理解决方案,如Provider或Bloc,来帮助开发者跟踪和管理应用程序的状态。这些解决方案可以帮助应用程序在不同的页面和组件之间共享数据。 4. 路由管理应用模板通常包含路由管理,用于定义应用程序中不同页面之间的路由。这使得开发者可以方便地导航到不同的页面,并管理页面之间的传递参数。 5. 主题和样式:应用模板通常包含主题和样式定义,用于控制应用程序的外观和感觉。开发者可以根据需要修改这些样式,以适应其特定的设计需求。 总的来说,Flutter应用模板提供了一个开箱即用的基础架构,可以帮助开发者快速启动新项目,并提供了一些常见的功能和组件,以加速开发流程。开发者可以在这些模板的基础上进行修改和扩展,以满足其特定的需求。 ### 回答2: Flutter应用模板是一种用于快速搭建Flutter应用的基础代码结构和组织方式。它提供了一套默认的文件夹结构和相关文件,使开发人员能够更高效地开发Flutter应用。 一个典型的Flutter应用模板包括以下几个主要文件和文件夹: 1. `lib`文件夹:这是应用的主要代码目录,包含了应用的各种逻辑和界面组件。 2. `main.dart`文件:这是应用的入口文件,其中包括了应用的启动配置和主要逻辑。 3. `pubspec.yaml`文件:这是Flutter应用的配置文件,用于声明应用所依赖的包和资源文件。 4. `assets`文件夹:这是应用的资源文件夹,包括图片、字体等静态资源文件。 5. `android`和`ios`文件夹:这两个文件夹分别是Android和iOS平台的配置文件和代码,用于构建和运行应用Flutter应用模板还可以包含其他一些常用的功能和模块,比如路由管理、国际化、网络请求等。这些功能通常通过引入相应的Flutter包或代码实现,以提高开发效率和代码质量。 使用Flutter应用模板,开发人员可以快速搭建应用的基本架构,减少重复劳动,提高开发效率。同时,模板还提供了良好的代码组织和结构,使得代码更易于维护和扩展。开发人员可以根据具体项目需求,对模板进行个性化定制,以满足应用的特定需求。 总之,Flutter应用模板是一种帮助开发人员快速搭建Flutter应用的基础代码结构和组织方式。它提供了一系列默认的文件和文件夹,以及常用功能和模块的集成,从而提高了开发效率和代码质量。 ### 回答3: Flutter应用模板是一种预定义的项目结构和代码框架,旨在提供一个起点,帮助开发人员更快速地构建Flutter应用程序。 Flutter应用模板通常包含以下主要部分: 1. 主要目录结构:应用模板通常会按照一定的规范来组织代码文件和资源文件,例如将源代码放在“lib”目录下,将资源文件放在“assets”目录下,将测试文件放在“test”目录下等。这些规范可以让项目更加有条理和易于维护。 2. 配置文件:应用模板会提供一些默认的配置文件,例如“pubspec.yaml”文件用于管理项目的依赖库和资源文件,以及“AndroidManifest.xml”和“Info.plist”文件用于配置Android和iOS应用的一些基本信息。 3. 路由管理应用模板通常会提供一种路由管理机制,用于管理应用中不同页面的跳转和传递参数,帮助开发人员更好地组织和管理页面的切换流程。 4. 预定义UI组件:应用模板往往会包含一些常用的UI组件,例如按钮、文本输入框、图片容器等,开发人员可以直接在这些组件的基础上进行定制和扩展,提高开发效率。 5. 状态管理应用模板通常会提供一种状态管理机制,例如使用“Provider”或“Bloc”等库来管理全局状态和组件内部的状态,以便实现页面间的数据共享和状态更新。 使用Flutter应用模板可以节省开发人员的精力和时间,快速搭建项目的骨架,并且遵循一定的结构和规范,有助于团队协作和项目的扩展与维护。当然,开发人员也可以根据实际需求进行定制和修改,以满足特定的业务需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值