pinia持久化存储方案——pinia-storage(自己写的,持续更新)

news2024/10/2 14:38:58

pinia持久化存储方案——pinia-storage

  • pinia-storage
    • pinia-storage Introduction
    • 版本更新说明(update introduction)
    • 安装(install)
      • npm 安装(npm install)
    • QuickStart
      • create pinia
        • store/index
        • store/indexPinia.ts
        • store/indexPinia.js
      • main.ts | main.js
      • App.vue
    • Why do not use JSON.stringify

pinia-storage

pinia-storage: 0.0.2 pinia-storage pinia-storage 0.0.2 0.0.2

  • author:syf20020816@outlook.com
  • docName:pinia-storage README
  • createDate:20230406
  • updateDate:20230407
  • version:0.0.2
  • des-tag:正式版
  • email:syf20020816@outlook.com

pinia-storage Introduction

Pinia Storage is Pinia’s persistence storage solution written in TypeScript. It can store the data in the state into localStorage and sessionStorage according to business requirements. It relies on the serialization scheme provided by the faster organization with better performance, greater scalability, and better flexibility: fast json stringify and deserialization scheme: secure json parse. Therefore, it is better than Pinia’s persistence plug-in pinia plugin persist in performance. Similarly, users do not need to worry about how to store data. They only need to set the storage location when creating data, and the updated data will be automatically updated for storage.

Pinia-Storage是使用TypeScript编写的Pinia的持久化存储解决方案,可根据业务需求将state中的数据存储到localStorage和sessionStorage中,依赖于性能更好、扩展性更强、灵活度更好的fastify组织提供的序列化方案:fast-json-stringify和反序列化方案:secure-json-parse,因此在性能上强于pinia的持久化插件pinia-plugin-persist。同样用户无需关心如何进行存储,只需要在创建数据时设置存储位置即可,更新过的数据会自动更新存储。

The average speed of updating and storing each data can reach 28-37ms, depending on your environment

平均每条数据更新存储的速度可以达到28~37ms,当然这取决于你的环境

在这里插入图片描述

版本更新说明(update introduction)

版本(version)0.0.2
版本说明(version tag)正式版
更新时间(update date)20230407
技术(technology)pinia+localStorage+sessionStorage+fast-json-stringify+secure-json-parse+ts

可用版本(work version):

  1. 0.0.2(20230406)

更新点(update points):

  1. 更新包结构(0.0.2)

安装(install)

npm 安装(npm install)

npm i pinia-storage
//you should install it's dependies
npm i fast-json-stringify
npm i secure-json-parse
npm i pinia

QuickStart

Usage and examples can be found in GitHub | Gitee for items prefixed with pinia-storage-test

使用方式以及示例可以查看GitHub|Gitee中以pinia-storage-test为前缀的项目

create pinia

store/index

//store/index.ts or index.js
import { createPinia } from "pinia";

export const pinia = createPinia();

store/indexPinia.ts

import { defineStore } from "pinia";
import { PiniaStorage } from "pinia-storage/index";

interface User {
  userId: UserId;
  username: string;
  age: number;
}

type UserId = string | number;
/**
 * user's scheme
 * 请注意anyOf和oneOf会一定程度影响性能
 * $id:注意这个,建议一定要加上,pay attention on $id,it is important,and you should add this param
 * this param is typeof string you can use other way to create it(window.crypto.randomUUID())
 */
const userScheme = {
  $id: window.crypto.randomUUID(),
  title: "user",
  type: "object",
  properties: {
    userId: {
      anyOf: [
        {
          type: "string",
        },
        {
          type: "number",
        },
      ],
    },
    username: {
      type: "string",
    },
    age: {
      type: "number",
    },
  },
};
//new PiniaStorage! in pinia it does not need param
const storage = new PiniaStorage();

export const indexStore = defineStore("index", {
  state: () => {
    return {
      id: 456,
      user: storage.init(
        "index",//this is pinia.$id
        "user",//This depends on your variable name!
        {
          userId: "1658ppo90",
          username: "pinia-storage",
          age: 16,
        },
        userScheme
      ) as User,
      test: storage.persist("index", "test", 56, {
        $id: window.crypto.randomUUID(),
        title: "test",
        type: "number",
      }),
    };
  },
});

store/indexPinia.js

import { defineStore } from 'pinia'
import { PiniaStorage } from 'pinia-storage/pinia-storage'

/**
 * user's scheme
 * 请注意anyOf和oneOf会一定程度影响性能
 * $id:注意这个,建议一定要加上,pay attention on $id,it is important,and you should add this param
 * this param is typeof string you can use other way to create it(window.crypto.randomUUID())
 */
const userScheme = {
  $id: window.crypto.randomUUID(),
  title: 'user',
  type: 'object',
  properties: {
    userId: {
      anyOf: [
        {
          type: 'string'
        },
        {
          type: 'number'
        }
      ]
    },
    username: {
      type: 'string'
    },
    age: {
      type: 'number'
    }
  }
}
//new PiniaStorage! in pinia it does not need param
const storage = new PiniaStorage()

export const indexStore = defineStore('index', {
  state: () => {
    return {
      id: 456,
      user: storage.init(
        'index',
        'user',
        {
          userId: '1658ppo90',
          username: 'pinia-storage',
          age: 16
        },
        userScheme
      ),
      test: storage.persist('index', 'test', 56, {
        $id: window.crypto.randomUUID(),
        title: 'test',
        type: 'number'
      })
    }
  }
})

main.ts | main.js

import { createApp } from "vue";
import "./style.css";
import App from "./App.vue";
import { pinia } from "./store/index";

createApp(App).use(pinia).mount("#app");

App.vue

<template>
  <div id="app">
    <div>
      <ul>
        <li>not use pinia-storage: {{ store.id }}</li>
        <li>use pinia-storage-session:{{ store.user.userId }}</li>
        <li>use pinia-storage-session:{{ store.user.username }}</li>
        <li>use pinia-storage-session:{{ store.user.age }}</li>
        <li>use pinia-storage-local:{{ store.test }}</li>
      </ul>
    </div>
    <button @click="change">change pinia state</button>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { indexStore } from './store/indexPinia'
import { MutationType } from 'pinia'
import { PiniaStorage } from 'pinia-storage/index'
const store = indexStore()
//init PiniaStorage
const storage = new PiniaStorage(store)
//update data
//we should use try...catch
const change = () => {
  try {
     //update date core == pinia.$patch(state=>{...})
    storage.update((state: any) => {
      state.user.age++
      state.test += 5
    })
  } catch (error) {
    console.log('====state.id not define to use pinia-storage====')
  }
  store.id++
}
//subscribe the update!
storage.watch()
</script>

<style lang="scss" scoped>
</style>

Why do not use JSON.stringify

The performance of using JSON. stringify()mainly depends on the size and structure of the serialized object, as well as the hardware configuration of the machine. Generally speaking, serializing a small simple object may not have significant performance issues, but handling large and complex objects may lead to performance issues.

The following are some factors that may affect the performance of JSON. stringify():

  • Object size and depth: The larger the object, the longer the serialization time required.
  • Object structure: If the object structure is too complex, such as containing circular references or nested objects, it can also lead to serialization performance issues.
  • Hardware and browser performance.

Overall, if you need to serialize large or complex objects, you should try to avoid calling ‘JSON. stringify()’ frequently, cache the serialization results as much as possible, and avoid duplicate serialization.

使用 JSON.stringify() 的性能主要取决于被序列化对象的大小和结构,以及机器的硬件配置。一般来说,序列化一个小型简单对象的性能不会有太大的问题,但是处理大型复杂对象时可能会导致性能问题。

以下是一些可能影响 JSON.stringify() 性能的因素:

  • 对象的大小和深度:对象越大,序列化所需的时间就越长。
  • 对象的结构:对象结构过于复杂,如包含循环引用或嵌套的对象,也会导致序列化的性能问题。
  • 硬件和浏览器的性能。

总的来说,如果需要序列化大型或复杂对象,应该尽量避免频繁地调用 JSON.stringify(),尽可能缓存序列化结果,避免重复序列化。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/436908.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Direct3D 12——几何着色器——几何着色器概念

几何着色器 几何着色器这个可选阶段便位于顶点着色器与像素着色器之间。几何着色器所输出的图元由顶点列表定义而成。在退岀几何着色器时&#xff0c;必将顶点的位置变换到齐次 裁剪空间。换言之&#xff0c;经过几何着色器阶段的处理后&#xff0c;我们就得到了位于齐次裁剪空…

MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点3:EXPLAIN ANALYZE

文章目录 MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点3&#xff1a;EXPLAIN ANALYZEEXPLAIN ANALYZE介绍EXPLAIN ANALYZE的特性EXPLAIN 和EXPLAIN ANALYZE的结果对比例题例题解析参考 MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点3&#xff1a;EXPLAIN ANALYZE EXPLAIN…

部门来了个测试人,听说是00后,上来一顿操作给我看呆了...

今天上班开早会就是新人见面仪式&#xff0c;听说来了个很厉害的大佬&#xff0c;年纪还不大&#xff0c;是上家公司离职过来的&#xff0c;薪资已经达到中高等水平&#xff0c;很多人都好奇不已&#xff0c;能拿到这个薪资应该人不简单&#xff0c;果然&#xff0c;自我介绍的…

网络原理(TCP/UDP)

目录 一. 网络基础 1. IP地址 2. 端口号 3. 协议 4. OSI七层模型 二. UDP协议 2.1 UDP的协议端格式&#xff1a; 2.2 UDP的特点 三. TCP协议 3.1 TCP协议段格式 3.2 TCP原理 &#xff08;1&#xff09;确认应答机制 &#xff08;2&#xff09;超时重传机制 &#xff…

CmBacktrace库在工程中的添加和应用

CmBacktrace 介绍在工程中添加CmBacktrace断言打印全局变量的值循环输出错误信息串口处理看门狗处理 介绍 CmBacktrace下载 CmBacktrace &#xff08;Cortex Microcontroller Backtrace&#xff09;是一款针对 ARM Cortex-M 系列 MCU 的错误代码自动追踪、定位&#xff0c;错…

Javaee Spring template实现查询数据库表内容 基于半xml半注解

昨天用基于xml配置实现template查询数据库&#xff0c;今天基于半xml半注解方式实现,使用注解需要导入spring-aop-5.3.8.jar 导入jar包 项目结构&#xff1a; 其他代码在&#xff0c;先前上一篇文章已经给出 AccountServiceImpl package wwx.dao;import org.springframework…

定时器中断实验

实现内容 利用TIM3的定时器中断来控制DS1的翻转&#xff0c;在主函数用DS0 的翻转来提示程序正在运行。 定时器介绍 定时器可以认为是一个计数器&#xff1b;给定计数器一个初值&#xff0c;每当计数一次&#xff0c;就会走过一个固定的时间&#xff0c;当达到我们给定的初值…

强大的图像处理:ImageKit10.E ActiveX Crack

强大的图像处理&#xff01; ImageKit10 ActiveX 是一个组件&#xff0c;允许您快速轻松地向应用程序添加图像处理功能。使用 ImageKit10 ActiveX&#xff0c;您可以编写从 TWAIN 扫描仪和数码相机检索图像的应用程序;加载和保存图像文件并将图像从一种格式转换为另一种格式;编…

数字电路和模拟电路-半导体三极管

目录 1 什么是三极管&#xff1f; 1.1 放大状态时的偏执条件 1.1.1发射结加正向电压&#xff0c;扩散运动形成发射极电流IE 1.1.2扩散到基区的自由电子与空穴的复合运动形成基极电流IB 1.1.3集电结加反向电压&#xff0c;漂移运动形成集电极电流Ⅰc 2 三极管工作原理 2.…

5.图论(0x3f:从周赛中学算法 2022下)

来自0x3f【从周赛中学算法 - 2022 年周赛题目总结&#xff08;下篇&#xff09;】&#xff1a;https://leetcode.cn/circle/discuss/WR1MJP/ 周赛中的图论题目比较少&#xff0c;除了下面选的 DFS、BFS、拓扑排序、基环树、二分图判定等&#xff0c;还有最短路、DFS 时间戳等&a…

CloudCompare二次开发之如何设计界面ui与功能实现?

文章目录 0.引言1.创建界面ui相关文件2.添加界面ui相关文件到CloudCompare工程3.修改工程相关文件4.结果展示 0.引言 CloudCompare源代码编译成功后&#xff0c;即可进行二次开发&#xff0c;可以通过修改源码实现二次开发&#xff0c;二次开发基础功能见&#xff08;CloudComp…

什么是文件共享软件?文件传输软件如何共享?

它是一个文件共享软件应用程序&#xff0c;可让强大的数据保护层下将任何大小的文件发送到世界上的任何地方。以光速发送和共享无限数量的文件。可以提交门户并使用语言&#xff0c;品牌&#xff0c;存储等自定义门户。可以选择一个存储点&#xff0c;例如文件传输软件&#xf…

[入门必看]数据结构4.2:串的模式匹配

[入门必看]数据结构4.2&#xff1a;串的模式匹配 第四章 串4.2 串的模式匹配知识总览4.2.1_朴素模式匹配算法4.2.2_1_KMP算法4.2.2_2_求next数组4.2.3_KMP算法的进一步优化 4.2.1_朴素模式匹配算法什么是字符串的模式匹配朴素模式匹配算法通过数组下标实现朴素模式匹配算法代码…

http(1)

主要介绍http 1.0 我们在浏览器中输入一个网址&#xff0c;稍等片刻就看见了网页 客户端会发送一个http请求&#xff0c;要求返回cn.bing.com这个网址&#xff0c;服务器收到请求后就会返回一个html页面 &#xff08;服务器根据请求找到客户端想要的资源&#xff0c;然后把这个…

[LeetCode]路径总和

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径&#xff0c;这条路径上所有节点值相加等于目标和 targetSum 。如果存在&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 叶子节点 是指没有子节点…

【并发编程】线程池的原理和源码分析

线程使用上可能的问题 我们一般通过new Thread().start();来创建和运行一个线程&#xff0c;如果在业务过程中有大量场景需要使用多线程来并发&#xff0c;那么就会有以下问题 需要频繁的创建和销毁线程 &#xff0c;需要消耗CPU资源如果创建和销毁的线程的数量过多(大于CPU核…

CMOS图像传感器——从传感器冗余说起

在这先抛出一个概念,什么是成像圈?众所周知,相机的镜头近似于圆柱体,光线透过圆筒子投射出的大都是圆形。我们可以拿一个镜头演示一下,当这个圆圈投在传感器所在焦平面时,我们称之为像场。像场的边界我们称之为成像圈,成像圈是圆的,但是传感器是矩形,天圆地方的怎么放…

Lombok插件下载与离线安装

Lombok插件下载与离线安装 首先你既然搜要离线安装或下载&#xff0c;那么肯定也是在IDEA工具里面&#xff0c;无法搜索到&#xff0c;或者自动下载安装失败吧&#xff1f; 安装包下载地址 记得和 idea版本一样&#xff0c; 如果不知道啥版本看下面

CleanMyMac X4.15重大更新 新功能菜单发布

CleanMyMac&#xff0c;一款电脑清理软件&#xff0c;可以帮助你清理垃圾文件、优化系统性能、管理应用程序等。它就像你的电脑管家&#xff0c;让你的电脑始终保持最佳状态。无论是手机还是电脑&#xff0c;在使用一段时间之后都可能会发生卡顿的现象&#xff0c;很多小伙伴会…

C++ 高级数据结构————[ 单调栈 ]

每周一篇的算法文章来了 今天讲解的是高级数据结构中的——单调栈 单调栈&#xff0c;顾名思义&#xff0c;就是升级版的栈&#xff08;&#xff09; 先回顾一下栈把 栈&#xff0c;是一种线性表&#xff0c;它的特点是只能从一边进出&#xff0c;并且先进后出&#xff0c;后进…