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


    前言

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

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

    需求分析

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

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

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

    模块设计

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

    1. import 'package:object_key/object_key.dart' show Time;
    2. /// Holder for cache value with times in seconds
    3. class CacheHolder <V> {
    4. CacheHolder(V? cacheValue, double cacheLifeSpan, {double? now})
    5. : _value = cacheValue, _life = cacheLifeSpan {
    6. now ??= Time.currentTimestamp;
    7. _expired = now + cacheLifeSpan;
    8. _deprecated = now + cacheLifeSpan * 2;
    9. }
    10. V? _value;
    11. final double _life; // life span (in seconds)
    12. double _expired = 0; // time to expired
    13. double _deprecated = 0; // time to deprecated
    14. V? get value => _value;
    15. /// update cache value with current time in seconds
    16. void update(V? newValue, {double? now}) {
    17. _value = newValue;
    18. now ??= Time.currentTimestamp;
    19. _expired = now + _life;
    20. _deprecated = now + _life * 2;
    21. }
    22. /// check whether cache is alive with current time in seconds
    23. bool isAlive({double? now}) {
    24. now ??= Time.currentTimestamp;
    25. return now < _expired;
    26. }
    27. /// check whether cache is deprecated with current time in seconds
    28. bool isDeprecated({double? now}) {
    29. now ??= Time.currentTimestamp;
    30. return now > _deprecated;
    31. }
    32. /// renewal cache with a temporary life span and current time in seconds
    33. void renewal(double? duration, {double? now}) {
    34. duration ??= 120;
    35. now ??= Time.currentTimestamp;
    36. _expired = now + duration;
    37. _deprecated = now + _life * 2;
    38. }
    39. }

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

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

    缓存池

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

    1. import 'package:object_key/object_key.dart' show Time;
    2. import 'holder.dart';
    3. class CachePair <V> {
    4. CachePair(this.value, this.holder);
    5. final V? value;
    6. final CacheHolder holder;
    7. }
    8. /// Pool for cache holders with keys
    9. class CachePool <K, V> {
    10. final Map> _holderMap = {};
    11. Iterable get keys => _holderMap.keys;
    12. /// update cache holder for key
    13. CacheHolder update(K key, CacheHolder holder) {
    14. _holderMap[key] = holder;
    15. return holder;
    16. }
    17. /// update cache value for key with timestamp in seconds
    18. CacheHolder updateValue(K key, V? value, double life, {double? now}) =>
    19. update(key, CacheHolder(value, life, now: now));
    20. /// erase cache for key
    21. CachePair? erase(K key, {double? now}) {
    22. CachePair? old;
    23. if (now != null) {
    24. // get exists value before erasing
    25. old = fetch(key, now: now);
    26. }
    27. _holderMap.remove(key);
    28. return old;
    29. }
    30. /// fetch cache value & its holder
    31. CachePair? fetch(K key, {double? now}) {
    32. CacheHolder? holder = _holderMap[key];
    33. if (holder == null) {
    34. // holder not found
    35. return null;
    36. } else if (holder.isAlive(now: now)) {
    37. return CachePair(holder.value, holder);
    38. } else {
    39. // holder expired
    40. return CachePair(null, holder);
    41. }
    42. }
    43. /// clear expired cache holders
    44. int purge({double? now}) {
    45. now ??= Time.currentTimestamp;
    46. int count = 0;
    47. Iterable allKeys = keys;
    48. CacheHolder? holder;
    49. for (K key in allKeys) {
    50. holder = _holderMap[key];
    51. if (holder == null || holder.isDeprecated(now: now)) {
    52. // remove expired holders
    53. _holderMap.remove(key);
    54. ++count;
    55. }
    56. }
    57. return count;
    58. }
    59. }

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

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

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

    缓存管理器

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

    1. import 'package:object_key/object_key.dart' show Time;
    2. import 'pool.dart';
    3. class CacheManager {
    4. factory CacheManager() => _instance;
    5. static final CacheManager _instance = CacheManager._internal();
    6. CacheManager._internal();
    7. final Map<String, dynamic> _poolMap = {};
    8. /// Get pool with name
    9. ///
    10. /// @param name - pool name
    11. /// @param <K> - key type
    12. /// @param <V> - value type
    13. /// @return CachePool
    14. CachePool getPool(String name) {
    15. CachePool? pool = _poolMap[name];
    16. if (pool == null) {
    17. pool = CachePool();
    18. _poolMap[name] = pool;
    19. }
    20. return pool;
    21. }
    22. /// Purge all pools
    23. ///
    24. /// @param now - current time
    25. int purge(double? now) {
    26. now ??= Time.currentTimestamp;
    27. int count = 0;
    28. CachePool? pool;
    29. Iterable allKeys = _poolMap.keys;
    30. for (var key in allKeys) {
    31. pool = _poolMap[key];
    32. if (pool != null) {
    33. count += pool.purge(now: now);
    34. }
    35. }
    36. return count;
    37. }
    38. }

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

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

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

    应用示例

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

    先来看看代码:

    1. class MetaTable implements MetaDBI {
    2. @override
    3. Future getMeta(ID entity) async {
    4. // 从数据库中获取 meta 信息
    5. }
    6. @override
    7. Future<bool> saveMeta(Meta meta, ID entity) async {
    8. // 保存 meta 信息到数据库
    9. }
    10. }
    11. class MetaCache extends MetaTable {
    12. final CachePool _cache = CacheManager().getPool('meta');
    13. @override
    14. Future getMeta(ID entity) async {
    15. CachePair? pair;
    16. CacheHolder? holder;
    17. Meta? value;
    18. double now = Time.currentTimeSeconds;
    19. await lock();
    20. try {
    21. // 1. check memory cache
    22. pair = _cache.fetch(entity, now: now);
    23. holder = pair?.holder;
    24. value = pair?.value;
    25. if (value == null) {
    26. if (holder == null) {
    27. // not load yet, wait to load
    28. } else if (holder.isAlive(now: now)) {
    29. // value not exists
    30. return null;
    31. } else {
    32. // cache expired, wait to reload
    33. holder.renewal(128, now: now);
    34. }
    35. // 2. load from database
    36. value = await super.getMeta(entity);
    37. // update cache
    38. _cache.updateValue(entity, value, 36000, now: now);
    39. }
    40. } finally {
    41. unlock();
    42. }
    43. // OK, return cache now
    44. return value;
    45. }
    46. @override
    47. Future<bool> saveMeta(Meta meta, ID entity) async {
    48. _cache.updateValue(entity, meta, 36000, now: Time.currentTimeSeconds);
    49. return await super.saveMeta(meta, entity);
    50. }
    51. }

    带缓存读数据

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

    如果 (值存在),则 {

        直接返回该值;

    }

    否则检查 holder;

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

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

    }

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

    然后再更新本地缓存。

    带缓存写数据

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

    代码引用

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

    dependencies:

        object_key: ^0.1.1

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

    import 'package:object_key/object_key.dart';

    全部源码

    1. /* license: https://mit-license.org
    2. *
    3. * ObjectKey : Object & Key kits
    4. *
    5. * Written in 2023 by Moky
    6. *
    7. * =============================================================================
    8. * The MIT License (MIT)
    9. *
    10. * Copyright (c) 2023 Albert Moky
    11. *
    12. * Permission is hereby granted, free of charge, to any person obtaining a copy
    13. * of this software and associated documentation files (the "Software"), to deal
    14. * in the Software without restriction, including without limitation the rights
    15. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    16. * copies of the Software, and to permit persons to whom the Software is
    17. * furnished to do so, subject to the following conditions:
    18. *
    19. * The above copyright notice and this permission notice shall be included in all
    20. * copies or substantial portions of the Software.
    21. *
    22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    28. * SOFTWARE.
    29. * =============================================================================
    30. */
    31. import 'package:object_key/object_key.dart' show Time;
    32. /// Holder for cache value with times in seconds
    33. class CacheHolder <V> {
    34. CacheHolder(V? cacheValue, double cacheLifeSpan, {double? now})
    35. : _value = cacheValue, _life = cacheLifeSpan {
    36. now ??= Time.currentTimestamp;
    37. _expired = now + cacheLifeSpan;
    38. _deprecated = now + cacheLifeSpan * 2;
    39. }
    40. V? _value;
    41. final double _life; // life span (in seconds)
    42. double _expired = 0; // time to expired
    43. double _deprecated = 0; // time to deprecated
    44. V? get value => _value;
    45. /// update cache value with current time in seconds
    46. void update(V? newValue, {double? now}) {
    47. _value = newValue;
    48. now ??= Time.currentTimestamp;
    49. _expired = now + _life;
    50. _deprecated = now + _life * 2;
    51. }
    52. /// check whether cache is alive with current time in seconds
    53. bool isAlive({double? now}) {
    54. now ??= Time.currentTimestamp;
    55. return now < _expired;
    56. }
    57. /// check whether cache is deprecated with current time in seconds
    58. bool isDeprecated({double? now}) {
    59. now ??= Time.currentTimestamp;
    60. return now > _deprecated;
    61. }
    62. /// renewal cache with a temporary life span and current time in seconds
    63. void renewal(double? duration, {double? now}) {
    64. duration ??= 120;
    65. now ??= Time.currentTimestamp;
    66. _expired = now + duration;
    67. _deprecated = now + _life * 2;
    68. }
    69. }
    70. class CachePair <V> {
    71. CachePair(this.value, this.holder);
    72. final V? value;
    73. final CacheHolder holder;
    74. }
    75. /// Pool for cache holders with keys
    76. class CachePool <K, V> {
    77. final Map> _holderMap = {};
    78. Iterable get keys => _holderMap.keys;
    79. /// update cache holder for key
    80. CacheHolder update(K key, CacheHolder holder) {
    81. _holderMap[key] = holder;
    82. return holder;
    83. }
    84. /// update cache value for key with timestamp in seconds
    85. CacheHolder updateValue(K key, V? value, double life, {double? now}) =>
    86. update(key, CacheHolder(value, life, now: now));
    87. /// erase cache for key
    88. CachePair? erase(K key, {double? now}) {
    89. CachePair? old;
    90. if (now != null) {
    91. // get exists value before erasing
    92. old = fetch(key, now: now);
    93. }
    94. _holderMap.remove(key);
    95. return old;
    96. }
    97. /// fetch cache value & its holder
    98. CachePair? fetch(K key, {double? now}) {
    99. CacheHolder? holder = _holderMap[key];
    100. if (holder == null) {
    101. // holder not found
    102. return null;
    103. } else if (holder.isAlive(now: now)) {
    104. return CachePair(holder.value, holder);
    105. } else {
    106. // holder expired
    107. return CachePair(null, holder);
    108. }
    109. }
    110. /// clear expired cache holders
    111. int purge({double? now}) {
    112. now ??= Time.currentTimestamp;
    113. int count = 0;
    114. Iterable allKeys = keys;
    115. CacheHolder? holder;
    116. for (K key in allKeys) {
    117. holder = _holderMap[key];
    118. if (holder == null || holder.isDeprecated(now: now)) {
    119. // remove expired holders
    120. _holderMap.remove(key);
    121. ++count;
    122. }
    123. }
    124. return count;
    125. }
    126. }
    127. class CacheManager {
    128. factory CacheManager() => _instance;
    129. static final CacheManager _instance = CacheManager._internal();
    130. CacheManager._internal();
    131. final Map<String, dynamic> _poolMap = {};
    132. /// Get pool with name
    133. ///
    134. /// @param name - pool name
    135. /// @param <K> - key type
    136. /// @param <V> - value type
    137. /// @return CachePool
    138. CachePool getPool(String name) {
    139. CachePool? pool = _poolMap[name];
    140. if (pool == null) {
    141. pool = CachePool();
    142. _poolMap[name] = pool;
    143. }
    144. return pool;
    145. }
    146. /// Purge all pools
    147. ///
    148. /// @param now - current time
    149. int purge(double? now) {
    150. now ??= Time.currentTimestamp;
    151. int count = 0;
    152. CachePool? pool;
    153. Iterable allKeys = _poolMap.keys;
    154. for (var key in allKeys) {
    155. pool = _poolMap[key];
    156. if (pool != null) {
    157. count += pool.purge(now: now);
    158. }
    159. }
    160. return count;
    161. }
    162. }

    GitHub 地址:

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

    结语

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

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

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

  • 相关阅读:
    基于SSM的培训学校教学管理平台的设计与实现
    手把手教你使用LabVIEW人工智能视觉工具包快速实现图像读取与采集(含源码)
    【公司UI自动化学习】
    基于Matlab求解高教社杯全国大学生数学建模竞赛(CUMCM2012A题)-葡萄酒的评价(源码+数据)
    通过内网穿透技术实现USB设备共享(USB Redirector)逆向共享
    Leetcode hot 100
    【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
    LVGL 虚拟键盘使用
    “高级小程序开发指南“
    【来点小剧场--项目测试报告】个人博客项目自动化测试
  • 原文地址:https://blog.csdn.net/moky/article/details/139711545