当前位置:网站首页>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>
边栏推荐
- Whether runnable can be interrupted
- 20th anniversary of agile: a failed uprising
- Async and await
- Use cpolar to build a business website (2)
- 如何在opensea批量发布NFT(Rinkeby测试网)
- After UE4 is packaged, mesh has no material problem
- How to build your own super signature system (yunxiaoduo)?
- What is Base64?
- webgl_ Enter the three-dimensional world (1)
- MySQL bit类型解析
猜你喜欢
UE4 exports the picture + text combination diagram through ucanvasrendertarget2d
Ue4/ue5 multi thread development attachment plug-in download address
After UE4 is packaged, mesh has no material problem
Do you know the relationship between the most important indicators of two strong wind control and the quality of the customer base
HW primary flow monitoring, what should we do
2. Heap sort "hard to understand sort"
LeetCode1_ Sum of two numbers
Streaming end, server end, player end
AB package details in unity (super detail, features, packaging, loading, manager)
What is Base64?
随机推荐
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
写一篇万字长文《CAS自旋锁》送杰伦的新专辑登顶热榜
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
【OBS】RTMPSockBuf_ Fill, remote host closed connection.
Virtual memory, physical memory /ram what
How to build your own super signature system (yunxiaoduo)?
Please supervise the 2022 plan
unnamed prototyped parameters not allowed when body is present
【原创】一切不谈考核的管理都是扯淡!
Do not use memset to clear floating-point numbers
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
How to create Apple Developer personal account P8 certificate
Clang compile link ffmpeg FAQ
Jacobo code coverage
Database exception resolution caused by large table delete data deletion
【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
How to understand that binary complement represents negative numbers
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
【搞船日记】【Shapr3D的STL格式转Gcode】
OpenGL's distinction and understanding of VAO, VBO and EBO