当前位置:网站首页>TS as a general cache method
TS as a general cache method
2022-07-07 15:47:00 【Sam young】
Create a file tool
export interface IStorageStore {
/** Expiration time */
expiredAt: number;
/** Saved content */
value: any;
}
export interface IGetStorageInfo {
/** Is it overdue */
expired: boolean;
/** Timestamp of cache expiration */
expiredAt: number;
/** Timestamp of cache expiration - Current timestamp */
expiredIn: number;
/** Cached data */
value: any;
}
export default const App = {
/** Write to cache */
setStorage(usage: string, value: any, options: ISetStorageOptions = {
expiresIn: -1 }): void {
const key = storageKey(usage);
const expiresIn = options.expiresIn || -1;
let expiredAt;
function getExpiresAt(): number {
return expiresIn === -1 ? -1 : new Date().valueOf() + expiresIn * 1000;
}
// The default is true: Indicates that the expiration time will be overwritten
if (options.expiresInOverwrite !== false) {
expiredAt = getExpiresAt();
} else {
const stored = window.sessionStorage.getItem(key);
if (typeof stored !== "undefined") {
// No coverage , And it has been saved before , Therefore, the expiration time will not be modified
const reuslt = JSON.parse(stored) as IStorageStore;
expiredAt = reuslt.expiredAt;
} else {
expiredAt = getExpiresAt();
}
}
try {
const data = {
value,
expiredAt,
};
window.sessionStorage.setItem(key, JSON.stringify(data));
} catch (error) {
console.log(" Cache write failed : ", error);
}
},
/** Read cache information */
getStorageInfo(usage: string): IGetStorageInfo | null {
const key = storageKey(usage);
/** If this cache exists, process the data Otherwise it returns null */
if (window.sessionStorage.getItem(key)) {
const storedContent = JSON.parse(window.sessionStorage.getItem(key) || "");
const now = new Date().valueOf();
const expiredAt = Number(Reflect.get(storedContent, "expiredAt") || -1);
const expiredIn = expiredAt - now;
const expired = expiredAt !== -1 && expiredIn <= 0;
let value = null;
if (expired) {
window.sessionStorage.removeItem(key);
} else {
value = Reflect.get(storedContent, "value");
}
return {
expired, expiredAt, expiredIn, value };
} else {
return null;
}
},
/** Remove the cache */
removeStorage(usage: string): any {
window.sessionStorage.removeItem(usage);
},
}
Page using
<template>
<div class="p-index">
Cache class
<input v-model="input" />
<button @click="handleSaveCache"> Save cache </button>
<button @click="handleGetCache"> Read cache </button>
</div>
</template>
<script lang="ts"> import {
Options, Vue } from "vue-class-component"; import Tool from "@/tool/app"; @Options({
}) export default class PageIndex extends Vue {
input = ""; handleSaveCache() {
Tool.setStorage("input", this.input, {
expiresIn: 10 }); } handleGetCache() {
const reuslt = Tool.getStorageInfo("input"); console.log(reuslt); } } </script>
边栏推荐
- 2022 all open source enterprise card issuing network repair short website and other bugs_ 2022 enterprise level multi merchant card issuing platform source code
- Cocos uses custom material to display problems
- Getting started with webgl (4)
- 【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
- [deep learning] semantic segmentation experiment: UNET network /msrc2 dataset
- 有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
- Getting started with webgl (3)
- #HPDC智能基座人才发展峰会随笔
- Pit avoidance: description of null values in in and not in SQL
- There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the
猜你喜欢
居然从408改考自命题!211华北电力大学(北京)
使用cpolar建立一个商业网站(2)
Zhongang Mining: Fluorite continues to lead the growth of new energy market
AB package details in unity (super detail, features, packaging, loading, manager)
The download button and debug button in keil are grayed out
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
[target detection] yolov5 Runtong voc2007 data set
Create lib Library in keil and use lib Library
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
随机推荐
STM32F103C8T6 PWM驱动舵机(SG90)
2. 堆排序『较难理解的排序』
【数字IC验证快速入门】19、SystemVerilog学习之基本语法6(线程内部通信...内含实践练习)
[deep learning] image hyperspectral experiment: srcnn/fsrcnn
[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset
LeetCode2_ Add two numbers
Introduction of mongod management database method
HPDC smart base Talent Development Summit essay
The bank needs to build the middle office capability of the intelligent customer service module to drive the upgrade of the whole scene intelligent customer service
Oracle控制文件丢失恢复归档模式方法
Summary of knowledge points of xlua hot update solution
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
Window环境下配置Mongodb数据库
使用cpolar建立一个商业网站(2)
MySQL bit type resolution
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
How to build your own super signature system (yunxiaoduo)?
如何在opensea批量发布NFT(Rinkeby测试网)
Share the technical details of super signature system construction
unnamed prototyped parameters not allowed when body is present