-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
295 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,32 @@ | ||
import { getStorageShortName } from '/@/utils/env'; | ||
import { createStorage as create } from './storageCache'; | ||
import { createStorage as create, CreateStorageParams } from './storageCache'; | ||
import { enableStorageEncryption } from '/@/settings/encryptionSetting'; | ||
import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting'; | ||
|
||
const createOptions = (storage = sessionStorage) => { | ||
export type Options = Partial<CreateStorageParams>; | ||
|
||
const createOptions = (storage: Storage, options: Options = {}): Options => { | ||
return { | ||
// No encryption in debug mode | ||
hasEncrypt: enableStorageEncryption, | ||
storage, | ||
prefixKey: getStorageShortName(), | ||
timeout: DEFAULT_CACHE_TIME, | ||
|
||
...options, | ||
}; | ||
}; | ||
|
||
export const WebStorage = create(createOptions()); | ||
export const WebStorage = create(createOptions(sessionStorage)); | ||
|
||
export const createStorage = (storage: Storage = sessionStorage, options: Options = {}) => { | ||
return create(createOptions(storage, options)); | ||
}; | ||
|
||
export const createStorage = (storage: Storage = sessionStorage) => { | ||
return create(createOptions(storage))!; | ||
export const createPersistentStorage = ( | ||
storage: Storage = sessionStorage, | ||
options: Options = {} | ||
) => { | ||
return createStorage(storage, { ...options, timeout: DEFAULT_CACHE_TIME }); | ||
}; | ||
|
||
export default WebStorage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
export interface Cache<V = any> { | ||
value?: V; | ||
timeoutId?: ReturnType<typeof setTimeout>; | ||
time?: number; | ||
alive?: number; | ||
} | ||
|
||
const NOT_ALIVE = 0; | ||
|
||
export class Memory<T = any, V = any> { | ||
private cache: { [key in keyof T]?: Cache<V> } = {}; | ||
private alive: number; | ||
|
||
constructor(alive = NOT_ALIVE) { | ||
// Unit second | ||
this.alive = alive * 1000; | ||
} | ||
|
||
get getCache() { | ||
return this.cache; | ||
} | ||
|
||
setCache(cache) { | ||
this.cache = cache; | ||
} | ||
|
||
// get<K extends keyof T>(key: K) { | ||
// const item = this.getItem(key); | ||
// const time = item?.time; | ||
// if (!isNullOrUnDef(time) && time < new Date().getTime()) { | ||
// this.remove(key); | ||
// } | ||
// return item?.value ?? undefined; | ||
// } | ||
|
||
get<K extends keyof T>(key: K) { | ||
return this.cache[key]; | ||
} | ||
|
||
set<K extends keyof T>(key: K, value: V, expires?: number) { | ||
let item = this.get(key); | ||
|
||
if (!expires || (expires as number) <= 0) { | ||
expires = this.alive; | ||
} | ||
if (item) { | ||
if (item.timeoutId) { | ||
clearTimeout(item.timeoutId); | ||
item.timeoutId = undefined; | ||
} | ||
item.value = value; | ||
} else { | ||
item = { value, alive: expires }; | ||
this.cache[key] = item; | ||
} | ||
|
||
if (!expires) { | ||
return value; | ||
} | ||
item.time = new Date().getTime() + this.alive * 1000; | ||
item.timeoutId = setTimeout(() => { | ||
this.remove(key); | ||
}, expires); | ||
|
||
return value; | ||
} | ||
|
||
remove<K extends keyof T>(key: K) { | ||
const item = this.get(key); | ||
Reflect.deleteProperty(this.cache, key); | ||
if (item) { | ||
clearTimeout(item.timeoutId!); | ||
return item.value; | ||
} | ||
} | ||
|
||
resetCache(cache: { [K in keyof T]: Cache }) { | ||
Object.keys(cache).forEach((key) => { | ||
const k = (key as any) as keyof T; | ||
const item = cache[k]; | ||
if (item && item.time) { | ||
const now = new Date().getTime(); | ||
const expire = now + item.time * 1000; | ||
if (expire > now) { | ||
this.set(k, item.value, expire); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
clear() { | ||
Object.keys(this.cache).forEach((key) => { | ||
const item = this.cache[key]; | ||
item.timeoutId && clearTimeout(item.timeoutId); | ||
}); | ||
this.cache = {}; | ||
} | ||
} |
Oops, something went wrong.