当前位置:网站首页>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>
边栏推荐
- [quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)
- AB package details in unity (super detail, features, packaging, loading, manager)
- MongoDB数据库基础知识整理
- MySQL bit type resolution
- LeetCode3_ Longest substring without duplicate characters
- postman生成时间戳,未来时间戳
- Whether runnable can be interrupted
- 15. Using the text editing tool VIM
- Getting started with webgl (2)
- VS2005 strange breakpoint is invalid or member variable value cannot be viewed
猜你喜欢
Whole process analysis of unity3d rendering pipeline
Webgl texture
有钱人买房就是不一样
Annexb and avcc are two methods of data segmentation in decoding
Super simple and fully automated generation super signature system (cloud Xiaoduo minclouds.com cloud service instance), free application in-house test app distribution and hosting platform, maintenan
居然从408改考自命题!211华北电力大学(北京)
AB package details in unity (super detail, features, packaging, loading, manager)
Getting started with webgl (1)
HW初级流量监控,到底该怎么做
银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
随机推荐
使用Scrapy框架爬取网页并保存到Mysql的实现
【OBS】RTMPSockBuf_ Fill, remote host closed connection.
Whether runnable can be interrupted
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
20th anniversary of agile: a failed uprising
Use of SVN
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
UE4 exports the picture + text combination diagram through ucanvasrendertarget2d
Cocos creator collision and collision callback do not take effect
[Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering
Connecting FTP server tutorial
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
Monthly observation of internet medical field in May 2022
A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation
Keil5 does not support online simulation of STM32 F0 series
【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
./ Functions of configure, make and make install
When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address
Mesh merging under ue4/ue5 runtime