diff --git a/packages/plugin-infra/src/web3/useSocialAddressListAll.ts b/packages/plugin-infra/src/web3/useSocialAddressListAll.ts index 0fa5e1930d00..98986eeddd4e 100644 --- a/packages/plugin-infra/src/web3/useSocialAddressListAll.ts +++ b/packages/plugin-infra/src/web3/useSocialAddressListAll.ts @@ -1,8 +1,17 @@ -import { useAsyncRetry } from 'react-use' import { EMPTY_LIST } from '@masknet/shared-base' import { NetworkPluginID, SocialAddress, SocialIdentity } from '@masknet/web3-shared-base' +import LRUCache from 'lru-cache' +import { useAsyncRetry } from 'react-use' import { useWeb3State } from './useWeb3State' +type AddressList = Array> +type CacheValue = Promise>> + +const addressCache = new LRUCache({ + max: 500, + ttl: 2 * 60 * 1000, +}) + /** * Get all social addresses under of all networks. */ @@ -18,9 +27,14 @@ export function useSocialAddressListAll( const userId = identity?.identifier?.userId if (!userId || userId === '$unknown') return EMPTY_LIST - const allSettled = await Promise.allSettled>>( - [EVM_IdentityService, SolanaIdentityService].map((x) => x?.lookup(identity) ?? []), - ) + let cached = addressCache.get(userId) + if (!cached) { + cached = Promise.allSettled( + [EVM_IdentityService, SolanaIdentityService].map((x) => x?.lookup(identity) ?? []), + ) + addressCache.set(userId, cached) + } + const allSettled = await cached const listOfAddress = allSettled.flatMap((x) => (x.status === 'fulfilled' ? x.value : [])) return sorter && listOfAddress.length ? listOfAddress.sort(sorter) : listOfAddress }, [identity, sorter, EVM_IdentityService?.lookup, SolanaIdentityService?.lookup])