当前位置:网站首页>Secondary encapsulation of storage (sessionstorage/localstorage) using TS
Secondary encapsulation of storage (sessionstorage/localstorage) using TS
2022-06-29 01:53:00 【maolovecoding】
combination ts encapsulation storeage
In the project , If you use native, uncapsulated localStorage It will be a little troublesome .emm, Second encapsulation of session and local storage scheme . Easy to use for the project . It is more convenient to access objects , And you can get objects , Get the type of object .
export class LocalCache {
setCache<T = any>(key: string, value: T): boolean;
setCache<T = any>(
key: string,
value: T,
localOrSessionStorage: boolean
): boolean;
/** * Set the cache * @param key * @param value * @param localOrSessionStorage true => localStorage The default is true If needed sessionStorage This parameter needs to be passed false * @returns */
setCache<T = any>(
key: string,
value: T,
localOrSessionStorage = true
): boolean {
try {
const val = JSON.stringify(value);
if (localOrSessionStorage) {
window.localStorage.setItem(key, val);
} else {
window.sessionStorage.setItem(key, val);
}
} catch (error) {
return false;
}
return true;
}
getCache<T = any>(key: string): T;
getCache<T = any>(key: string, localOrSessionStorage: boolean): T;
/** * * @param key Access to the cache * @param localOrSessionStorage * @returns */
getCache<T>(key: string, localOrSessionStorage = true): T {
let res: any;
if (localOrSessionStorage) {
const val = window.localStorage.getItem(key);
if (val) {
res = JSON.parse(val);
}
}
const val = window.sessionStorage.getItem(key);
if (val) {
res = JSON.parse(val);
}
return res as T;
}
deleteCache(key: string): void;
deleteCache(key: string, localOrSessionStorage: boolean): void;
deleteCache(key: string, localOrSessionStorage = true): void {
if (localOrSessionStorage) window.localStorage.removeItem(key);
else window.sessionStorage.removeItem(key);
}
clearCache(): void;
clearCache(localOrSessionStorage: boolean): void;
clearCache(localOrSessionStorage = true): void {
if (localOrSessionStorage) window.localStorage.clear();
else window.sessionStorage.clear();
}
}
// Cache object
export default new LocalCache();
边栏推荐
- 什么叫股票线上开户?网上开户安全么?
- Scala 基礎 (三):運算符和流程控制
- IPFS简述
- 4276. good at C
- 基于 FPGA 的 RISC CPU 设计(4)关于项目的 36 个问题及其答案
- Written examination questions, answers and analysis of Digital IC design and FPGA design (2) 2021 Huawei Hisilicon (Part 1)
- Basic use of flask Sqlalchemy
- Who do you want to know when opening a stock account? Is it safe to open an account online now?
- 如何成为一名高级数字 IC 设计工程师(5-1)理论篇:时钟技术、复位技术
- TypeScript(7)泛型
猜你喜欢

独家分析 | 软件测试关于简历和面试的真实情况

栈的增删查改---动态内存

Business system anti-virus
![[image detection] recognition of the front and back of a coin based on texture features with matlab code attached](/img/61/1fb15e9defa1fc471c4d2d34cc1ed4.jpg)
[image detection] recognition of the front and back of a coin based on texture features with matlab code attached

Analysis of sending principle of OData metadata request for SAP ui5 application

IPFS简述

SAP ui5 beginner tutorial Part 23 - sorting sort and grouping Group trial version of list control

TiFlash 面向编译器的自动向量化加速
Scala Foundation (3): Operators and Process Control

Typescript (5) class, inheritance, polymorphism
随机推荐
Near's storage stacking
如何成为一名高级数字 IC 设计工程师(6-7)数字 IC 验证篇:DEBUG 技巧
Introduction to super dongle scheme
Share the code technology points and software usage of socket multi client communication
4276. 擅长C
Oculusrifts and unity UI interaction (1) - Overview
Callback function of unity after importing resources
TypeScript(5)类、继承、多态
I want to buy stocks today, OK? Is it safe to open an account online now?
Digital IC design, FPGA design written examination questions, answers and analysis of autumn move (1) 2022 Ziguang zhanrui (Part 1)
I have summarized some experiences from the whole process of R & D platform splitting
How to encrypt the device
Teach you how to understand the test environment project deployment
How to become a senior digital IC Design Engineer (1-1) Verilog coding Grammar: Introduction
Installing Oracle database in docker
Configurable FFT system design based on risc-v SOC (1) Introduction
C语言课程设计------食品仓库管理系统
How to become a senior digital IC Design Engineer (4-2) script: file read / write operation realized by Verilog HDL code
Niuke.com Huawei question bank (41~50)
How does flush open an account? Is it safe to open an account online?