从零开始手撸一个阅读器--数据结构与数据缓存(4)

117 阅读3分钟

概述

  1. uniapp在App端为原生的 plus.storage,无大小限制,不是缓存,是持久化的。使用 uni.setStorage 就能满足数据存储要求。
  2. 除此之外,其他数据存储方案:
  • H5端还支持websql、indexedDB、sessionStorage
  • App端还支持SQLiteIO文件等本地存储方案。

本地书架存储

  1. 书架除了小说基本信息以外,还存储了几个比较重要的信息
    • uid,是一个uuidv4值,对应的每本小说的章节缓存数据(因为不确定 ni.setStorage 在移动端的读取速率,故每一本小说单独存储了章节缓存内容),uid值为setStorage的key值
    • origins,每本小说的其他书源地址,避免重复请求
    • stickyIndex,置顶排序值,是一个从 1 开始自增的数字,主要用于排序
  2. 存储流程为
    • onLaunch -> initBookShelves(uni.getStorageSync )
    • modify(add/remove) -> change bookShelves -> saveBookShelves(uni.setStorageSync)
type OriginItem = { origin: string; pathname: string };
type Origins = Array<OriginItem>;

interface bookInfo {
  author: string; // 作者
  bookName: string; // 书名
  origin: string; // 当前源
  pathname: string; // 目录页地址
  image: string; // 封面
  latestChapter: string; // 最新章节
  latestUpdateTime: string; // 最新更新时间
  description: string; // 描述
  categories: string; // 分类
  origins: Origins; // 书源信息
  links: { // 目录列表
    href: string; // 章节地址
    text: string; // 标题
  }[];
}
interface BooksShelvesData extends bookInfo {
  uid: string;
  latestReadeTimestamp?: number; // 上一次阅读时间
  chapter: number; // 章节
  pageIndex: number; // 页
  stickyIndex?: number; // 是否置顶
  group?: string; // 所属分组
}

// 小说书架结构
interface BookShelves {
  sort: "default" | "join" | "chart"; // 按最近阅读排序 | 按照添加时间排序 | 按字母排序
  data: BooksShelvesData[];
}

章节内容存储

  1. 章节内容里主要存储了小说的基本信息、章节缓存内容、书签信息
// 章节内容信息
interface chapterInfo {
  pageIndex: number;
  chapter: number;
  cache: cacheInfo[];
  mark: BookMark[];
  bookInfo: bookInfo;
  uid: string;
}
// 书签
interface BookMark {
  pageIndex: number;
  pageCount: number;
  chapter: number;
  title: string;
  time: string;
}
// 章节缓存
interface cacheInfo {
  content: string;
  title: string;
}

存储缓存结构 2.png

反思

  1. 我想了想,如果让我重构一遍,我不会选择上面的结构。对于章节缓存数据的存储应该会选择分表存储,这样易于管理,并且在清除缓存的时候有统一的入口,而且不会像上面的结构一样散乱无序。
  2. 类似于下面结构
interface cacheStorage {
  // 维护一套storage的影视表名
  sheet: Array<{
    sheetName: string;
    length: number;
  }>;
  // 每一张表能存储的最大数据书籍数
  maxSheetNameLength: number;
  // 阅读历史
  history: BooksShelvesData[];
  // 搜索历史
  historySearch: Array<string>;
  // 分组
  groups: BookGrop[];
}


// 当有新书的时候,选择 length 少于 maxSheetNameLength 的表存储数据
uni.setStorageSync(sheetName, JSON.stringify(chapterInfoList));

相关

总结

  1. 还是有很多不足的地方,很多地方的设计在一开始也没有考虑完善。
  2. 感觉没啥好写的了...就这样吧,水完回家过年咯(逃~)