# compatible-storage **Repository Path**: changeden/compatible-storage ## Basic Information - **Project Name**: compatible-storage - **Description**: Support all browser, Wechat mini program and Alipay mini program. Quick to set, get and remove the storage. - **Primary Language**: NodeJS - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-02-15 - **Last Updated**: 2022-05-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## Compatible-Storage > Support all browser, Wechat mini program and Alipay mini program. > 支持所有浏览器的硬盘缓存,同时支持微信小程序和支付宝小程序。 > Quick to set, get and remove the storage. > 简易的get、set操作。 ### NPM [ ![NPM version](https://img.shields.io/npm/v/compatible-storage.svg) ![NPM download](https://img.shields.io/npm/dm/compatible-storage.svg) ![NPM download](https://img.shields.io/npm/dw/compatible-storage.svg) ](https://www.npmjs.com/package/compatible-storage) ### Install 安装 ``` npm i -save compatible-storage // or yarn add compatible-storage ``` ### Import 导入 ``` import StorageUtils from 'compatible-storage'; ``` ### Usage 使用 #### Storage 硬盘缓存 ``` // Set by default expire time. Default: 30 days. StorageUtils.set('storage-key', { ...data...}); // Custom expire time. eg: 2 hours. StorageUtils.set('storage-key', { ...data...}, new StorageUtils.Expire(2, StorageUtils.EXPIRE_UNIT.HOUR)); // Get const value = StorageUtils.get('storage-key'); // Remove StorageUtils.remove('storage-key'); ``` #### Memory 内存缓存 > It will be reset when your shut down the mini-program or close the web window. > 当退出小程序或关闭网页,数据会重置 ``` // Set by default expire time. Default: 30 days. StorageUtils.setMemory('storage-key', { ...data...}); // Custom expire time. eg: 2 hours. StorageUtils.setMemory('storage-key', { ...data...}, new StorageUtils.Expire(2, StorageUtils.EXPIRE_UNIT.HOUR)); // Get const value = StorageUtils.getMemory('storage-key'); // Remove StorageUtils.removeMemory('storage-key'); // Reference object 获取引用对象 // Wechat Mini-Program 微信小程序 const memory = wx[StorageUtils.KEY_MEMORY] // Alipay Mini-Program 支付宝小程序 const memory = my[StorageUtils.KEY_MEMORY] // Web Page 网页 const memory = window[StorageUtils.KEY_MEMORY] // Get value for reference object 通过引用对象获取指定键的值 const value = memory[key] ```