Skip to content

Commit

Permalink
fix: args key removed from view method; auto cache clean-up.
Browse files Browse the repository at this point in the history
  • Loading branch information
wpdas committed Feb 6, 2024
1 parent b537a38 commit 56f37d6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/cache/MemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ class MemoryCache implements CacheI {
resolve()
})
}

cleanUp() {
return new Promise<void>((resolve) => {
Object.keys(_memoryCache).forEach((key) => {
const keyValueData = _memoryCache[key] as Data<any>

// If expired, remove item from _memoryCache
if (Date.now() > keyValueData.expiresAt) {
delete _memoryCache[key]
}
})

resolve()
})
}
}

export default MemoryCache
26 changes: 26 additions & 0 deletions src/cache/StorageCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ class StorageCache implements CacheI {
resolve()
})
}

cleanUp() {
return new Promise<void>((resolve) => {
if (!isLocalStorageAccessible) {
resolve()
return
}

const lsSize = localStorage.length
for (let i = 0; i < lsSize; i++) {
const key = localStorage.key(i)
if (key?.includes('naxios::')) {
// Get data and parse it
const cachedDataStr = localStorage.getItem(key)
const cachedData = cachedDataStr ? (JSON.parse(cachedDataStr) as Data<any>) : null

// If expired, remove item from local storage
if (cachedData && Date.now() > cachedData.expiresAt) {
localStorage.removeItem(key)
}
}
}

resolve()
})
}
}

export default StorageCache
5 changes: 5 additions & 0 deletions src/cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ export interface CacheI {
setItem: <T>(key: string, value: T) => Promise<void>
getItem: <T>(key: string) => Promise<T | null>
removeItem: (key: string) => Promise<void>
/**
* Clean up cache: remove all expired items from cache (in memory or local storage)
* @returns
*/
cleanUp: () => Promise<void>
}

export interface Data<T> {
Expand Down
30 changes: 26 additions & 4 deletions src/managers/contract-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,36 @@ class ContractManager {
}
}

/**
* Create a key name based on contractId + method + args
* @param method
* @param args
* @returns
*/
private getCacheKey(method: string, args: Record<string, any>) {
let keysValues = ''
Object.keys(args || {}).forEach((key) => {
keysValues += `:${key}-${args[key]}`
})

const key = `naxios::${this.walletManager.network}:${this.walletManager.contractId}:${method}${keysValues}`
return key
}

// Build View Method Interface
private async buildViewInterface<R>(props: BuildViewInterfaceProps) {
// Clean up cache: remove all expired items from cache (in memory or local storage)
if (this.cache) {
await this.cache.cleanUp()
}

const { method = '', args = {}, config } = props
const cacheKey = this.getCacheKey(method, args)

// Check if there's cached information, if so, returns it
// item name is composed of: contractAddress:method
// item name is composed of: naxios::testnet:contractAddress:method:arg0-arg0value:arg1-arg1value...
if (config?.useCache && this.cache) {
const cachedData = await this.cache.getItem<R>(`${this.walletManager.contractId}:${method}`)
const cachedData = await this.cache.getItem<R>(cacheKey)

if (cachedData) {
return cachedData
Expand All @@ -71,7 +93,7 @@ class ContractManager {

// If cache is avaiable, store data on it
if (config?.useCache && this.cache) {
await this.cache.setItem<R>(`${this.walletManager.contractId}:${method}`, outcome)
await this.cache.setItem<R>(cacheKey, outcome)
}

return outcome
Expand Down Expand Up @@ -166,7 +188,7 @@ class ContractManager {
* @returns
*/
async view<A extends {}, R>(method: string, props?: ViewMethodArgs<A>, config?: BuildViewInterfaceConfig) {
return this.buildViewInterface<R>({ method, args: { ...props }, config })
return this.buildViewInterface<R>({ method, args: props?.args || {}, config })
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/managers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type ContractManagerConfig = {

export type BuildViewInterfaceProps = {
method: string
args: {}
args: any
config?: BuildViewInterfaceConfig
}

Expand Down

0 comments on commit 56f37d6

Please sign in to comment.