当前位置:网站首页>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>

边栏推荐
- Oracle control file loss recovery archive mode method
- Basic knowledge sorting of mongodb database
- 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
- Getting started with webgl (2)
- 【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
- 15. Using the text editing tool VIM
- 2022 all open source enterprise card issuing network repair short website and other bugs_ 2022 enterprise level multi merchant card issuing platform source code
- What are the safest securities trading apps
- 【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
- Mesh merging under ue4/ue5 runtime
猜你喜欢

20th anniversary of agile: a failed uprising

Write a ten thousand word long article "CAS spin lock" to send Jay's new album to the top of the hot list

Async and await

UE4 exports the picture + text combination diagram through ucanvasrendertarget2d

Cut ffmpeg as needed, and use emscripten to compile and run

webgl_ Graphic transformation (rotation, translation, zoom)
![[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)](/img/a3/7f08f189c608d6086b368dfa3831f7.png)
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
Implementation of crawling web pages and saving them to MySQL using the scrapy framework

How to create Apple Developer personal account P8 certificate

LeetCode3_ Longest substring without duplicate characters
随机推荐
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
2.Golang基础知识
【原创】一切不谈考核的管理都是扯淡!
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
How to create Apple Developer personal account P8 certificate
After UE4 is packaged, mesh has no material problem
Pat grade a 1103 integer factorizatio
Create lib Library in keil and use lib Library
HPDC smart base Talent Development Summit essay
[quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
有钱人买房就是不一样
Configure mongodb database in window environment
Tkinter after how to refresh data and cancel refreshing
Monthly observation of internet medical field in May 2022
[quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)
webgl_ Graphic transformation (rotation, translation, zoom)
HW初级流量监控,到底该怎么做
Whole process analysis of unity3d rendering pipeline
./ Functions of configure, make and make install
【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码