-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: cache system for contract s view
- Loading branch information
Showing
11 changed files
with
279 additions
and
19 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { CacheConstructor, CacheI, Data } from './types' | ||
|
||
let _memoryCache: Record<string, any> = {} | ||
|
||
/** | ||
* Memory Cache - It get cleared when the app refreshes | ||
*/ | ||
class MemoryCache implements CacheI { | ||
private expirationTime = 60 // Default is 1 minute | ||
|
||
constructor({ expirationTime }: CacheConstructor) { | ||
this.expirationTime = expirationTime || this.expirationTime | ||
} | ||
|
||
private prepareData(data: any) { | ||
return { | ||
expiresAt: Date.now() + this.expirationTime * 1000, | ||
data, | ||
} | ||
} | ||
|
||
setItem<T>(key: string, value: T) { | ||
return new Promise<void>((resolve) => { | ||
const updatedCache = { ..._memoryCache } | ||
updatedCache[key] = this.prepareData(value) | ||
_memoryCache = updatedCache | ||
resolve() | ||
}) | ||
} | ||
|
||
getItem<T>(key: string) { | ||
return new Promise<T | null>((resolve) => { | ||
const item = (_memoryCache[key] as Data<T>) || null | ||
|
||
if (item && Date.now() < item.expiresAt) { | ||
// Use cached info | ||
resolve(item.data) | ||
return | ||
} | ||
resolve(null) | ||
}) | ||
} | ||
|
||
removeItem(key: string) { | ||
return new Promise<void>((resolve) => { | ||
const updatedCache: Record<string, any> = {} | ||
Object.keys(_memoryCache).forEach((currentKey: any) => { | ||
if (key !== currentKey) { | ||
updatedCache[currentKey] = _memoryCache[currentKey] | ||
} | ||
}) | ||
|
||
_memoryCache = updatedCache | ||
resolve() | ||
}) | ||
} | ||
} | ||
|
||
export default MemoryCache |
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,77 @@ | ||
import { CacheConstructor, CacheI, Data } from './types' | ||
|
||
let isLocalStorageAccessible = true | ||
|
||
try { | ||
// just try to read it | ||
window.localStorage | ||
} catch (error) { | ||
isLocalStorageAccessible = false | ||
} | ||
|
||
/** | ||
* Storage Cache - Data is persisted using Local Storage | ||
*/ | ||
class StorageCache implements CacheI { | ||
private expirationTime = 60 // Default is 1 minute | ||
|
||
constructor({ expirationTime }: CacheConstructor) { | ||
this.expirationTime = expirationTime || this.expirationTime | ||
} | ||
|
||
private prepareData(data: any) { | ||
return { | ||
expiresAt: Date.now() + this.expirationTime * 1000, | ||
data, | ||
} | ||
} | ||
|
||
setItem<T>(key: string, value: T) { | ||
return new Promise<void>((resolve) => { | ||
if (!isLocalStorageAccessible) { | ||
resolve() | ||
return | ||
} | ||
|
||
const data = this.prepareData(value) | ||
|
||
// persist data | ||
localStorage.setItem(key, JSON.stringify(data)) | ||
resolve() | ||
}) | ||
} | ||
|
||
getItem<T>(key: string) { | ||
return new Promise<T | null>((resolve) => { | ||
if (!isLocalStorageAccessible) { | ||
resolve(null) | ||
return | ||
} | ||
|
||
const cachedDataStr = localStorage.getItem(key) | ||
const cachedData = cachedDataStr ? (JSON.parse(cachedDataStr) as Data<T>) : null | ||
|
||
if (cachedData && Date.now() < cachedData.expiresAt) { | ||
// Use cached info | ||
resolve(cachedData.data) | ||
return | ||
} | ||
|
||
resolve(null) | ||
}) | ||
} | ||
|
||
removeItem(key: string) { | ||
return new Promise<void>((resolve) => { | ||
if (!isLocalStorageAccessible) { | ||
resolve() | ||
return | ||
} | ||
|
||
localStorage.removeItem(key) | ||
resolve() | ||
}) | ||
} | ||
} | ||
|
||
export default StorageCache |
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,2 @@ | ||
export { default as MemoryCache } from './MemoryCache' | ||
export { default as StorageCache } from './StorageCache' |
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,17 @@ | ||
export interface CacheI { | ||
setItem: <T>(key: string, value: T) => Promise<void> | ||
getItem: <T>(key: string) => Promise<T | null> | ||
removeItem: (key: string) => Promise<void> | ||
} | ||
|
||
export interface Data<T> { | ||
expiresAt: number | ||
data: T | ||
} | ||
|
||
export interface CacheConstructor { | ||
/** | ||
* Expiration time in seconds. After expiration, this is going to fetch real data instead of using cached data. | ||
*/ | ||
expirationTime?: number | ||
} |
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
Oops, something went wrong.