Skip to content

v1.4.0

Compare
Choose a tag to compare
@wpdas wpdas released this 06 Feb 14:57
· 30 commits to main since this release
  • feature: Cache System

Cache System

There are two kinds of cache systems to be used. They are Memory Cache and Storage Cache.

Memory Cache: will be cleared when the app refreshes, as its data lives in memory only.
Storage Cache: The data will remain even when the browser tab is refreshed. Data is persisted using Local Storage.

When instantiating a cache, you need to provide the expirationTime (in seconds). This is used to know when the cache should be returned instead of making a real contract call. When the cache expires, a real call to the contract is made. Each contract's method has its own time of expiration.

// web3Api.ts with cache
import naxios, { StorageCache } from '@wpdas/naxios'

const naxiosInstance = new naxios({
  contractId: CONTRACT_ID,
  network: 'testnet',
  cache: new StorageCache({ expirationTime: 60 }),
})

export const contractApi = naxiosInstance.contractApi()

Then, to use cached view, you can just pass the configuration object saying you want to use cached data.

import { contractApi } from './web3Api'

const args: {}
const config: { useCache: true }

contractApi.view('get_greeting', args, config).then((response) => console.log(response))