当前位置: 首页 > news >正文

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 expireddouble _deprecated = 0;  // time to deprecatedV? get value => _value;/// update cache value with current time in secondsvoid 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 secondsbool isAlive({double? now}) {now ??= Time.currentTimestamp;return now < _expired;}/// check whether cache is deprecated with current time in secondsbool isDeprecated({double? now}) {now ??= Time.currentTimestamp;return now > _deprecated;}/// renewal cache with a temporary life span and current time in secondsvoid 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 keyCacheHolder<V> update(K key, CacheHolder<V> holder) {_holderMap[key] = holder;return holder;}/// update cache value for key with timestamp in secondsCacheHolder<V> updateValue(K key, V? value, double life, {double? now}) =>update(key, CacheHolder(value, life, now: now));/// erase cache for keyCachePair<V>? erase(K key, {double? now}) {CachePair<V>? old;if (now != null) {// get exists value before erasingold = fetch(key, now: now);}_holderMap.remove(key);return old;}/// fetch cache value & its holderCachePair<V>? fetch(K key, {double? now}) {CacheHolder<V>? holder = _holderMap[key];if (holder == null) {// holder not foundreturn null;} else if (holder.isAlive(now: now)) {return CachePair(holder.value, holder);} else {// holder expiredreturn CachePair(null, holder);}}/// clear expired cache holdersint 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 CachePoolCachePool<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 timeint 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 {@overrideFuture<Meta?> getMeta(ID entity) async {// 从数据库中获取 meta 信息}@overrideFuture<bool> saveMeta(Meta meta, ID entity) async {// 保存 meta 信息到数据库}}class MetaCache extends MetaTable {final CachePool<ID, Meta> _cache = CacheManager().getPool('meta');@overrideFuture<Meta?> getMeta(ID entity) async {CachePair<Meta>? pair;CacheHolder<Meta>? holder;Meta? value;double now = Time.currentTimeSeconds;await lock();try {// 1. check memory cachepair = _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 existsreturn null;} else {// cache expired, wait to reloadholder.renewal(128, now: now);}// 2. load from databasevalue = await super.getMeta(entity);// update cache_cache.updateValue(entity, value, 36000, now: now);}} finally {unlock();}// OK, return cache nowreturn value;}@overrideFuture<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';

全部源码

GitHub 地址:

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

结语

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

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

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

相关文章:

  • zookeeper、kakfa添加用户加密
  • k8s基础命令集合
  • Wake Lock API:保持设备唤醒的利器
  • Oracle阅读Java帮助文档
  • Pytorch-Padding Layers
  • 【定义通讯数据类型】LCM搭建系统通讯
  • 重温react-01
  • Mongodb学习
  • Nginx之HTTP模块详解
  • 【LeetCode 5.】 最长回文子串
  • 主窗体设计
  • 2023年的Top20 AI应用在近一年表现怎么样?
  • Postman如何做接口测试:什么?postman 还可以做压力测试?
  • Windows 服务器Nginx 下载、部署、配置流程(图文教程)
  • 一次基于 rebase 的 PR 提交
  • 分享的文章《人生如棋》
  • 「面试题」如何实现一个圣杯布局?
  • 【翻译】babel对TC39装饰器草案的实现
  • CentOS 7 防火墙操作
  • C语言笔记(第一章:C语言编程)
  • ES6系统学习----从Apollo Client看解构赋值
  • JSDuck 与 AngularJS 融合技巧
  • Linux各目录及每个目录的详细介绍
  • magento 货币换算
  • MYSQL如何对数据进行自动化升级--以如果某数据表存在并且某字段不存在时则执行更新操作为例...
  • PHP 的 SAPI 是个什么东西
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • 程序员最讨厌的9句话,你可有补充?
  • 缓存与缓冲
  • 简析gRPC client 连接管理
  • 前端代码风格自动化系列(二)之Commitlint
  • 试着探索高并发下的系统架构面貌
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • ​【C语言】长篇详解,字符系列篇3-----strstr,strtok,strerror字符串函数的使用【图文详解​】
  • ​520就是要宠粉,你的心头书我买单
  • # Kafka_深入探秘者(2):kafka 生产者
  • #14vue3生成表单并跳转到外部地址的方式
  • ()、[]、{}、(())、[[]]命令替换
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (Python第六天)文件处理
  • (十)【Jmeter】线程(Threads(Users))之jp@gc - Stepping Thread Group (deprecated)
  • (十)DDRC架构组成、效率Efficiency及功能实现
  • (四)JPA - JQPL 实现增删改查
  • (五)activiti-modeler 编辑器初步优化
  • .java 指数平滑_转载:二次指数平滑法求预测值的Java代码
  • .NET Conf 2023 回顾 – 庆祝社区、创新和 .NET 8 的发布
  • .Net 基于.Net8开发的一个Asp.Net Core Webapi小型易用框架
  • .NET单元测试
  • .net分布式压力测试工具(Beetle.DT)
  • .net后端程序发布到nignx上,通过nginx访问
  • .NET企业级应用架构设计系列之技术选型
  • .NET中GET与SET的用法
  • @Transactional注解下,循环取序列的值,但得到的值都相同的问题
  • [2018/11/18] Java数据结构(2) 简单排序 冒泡排序 选择排序 插入排序
  • [android] 练习PopupWindow实现对话框