From ec6fcb5e0f618f9053f2c41640f40c77e7bc8579 Mon Sep 17 00:00:00 2001 From: unclebill Date: Wed, 15 Jun 2022 22:42:02 +0800 Subject: [PATCH 1/2] fix: cache result of useSocialAddressListAll --- .../src/web3/useSocialAddressListAll.ts | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/plugin-infra/src/web3/useSocialAddressListAll.ts b/packages/plugin-infra/src/web3/useSocialAddressListAll.ts index 0fa5e1930d00..ded1b7634e5f 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,10 +27,15 @@ 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]) + }, [identity?.identifier?.toText(), sorter, EVM_IdentityService?.lookup, SolanaIdentityService?.lookup]) } From 64055505842c85849812e2f8d29bb5f56b02b269 Mon Sep 17 00:00:00 2001 From: unclebill Date: Wed, 15 Jun 2022 22:48:13 +0800 Subject: [PATCH 2/2] fixup! fix: cache result of useSocialAddressListAll --- packages/plugin-infra/src/web3/useSocialAddressListAll.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-infra/src/web3/useSocialAddressListAll.ts b/packages/plugin-infra/src/web3/useSocialAddressListAll.ts index ded1b7634e5f..98986eeddd4e 100644 --- a/packages/plugin-infra/src/web3/useSocialAddressListAll.ts +++ b/packages/plugin-infra/src/web3/useSocialAddressListAll.ts @@ -37,5 +37,5 @@ export function useSocialAddressListAll( const allSettled = await cached const listOfAddress = allSettled.flatMap((x) => (x.status === 'fulfilled' ? x.value : [])) return sorter && listOfAddress.length ? listOfAddress.sort(sorter) : listOfAddress - }, [identity?.identifier?.toText(), sorter, EVM_IdentityService?.lookup, SolanaIdentityService?.lookup]) + }, [identity, sorter, EVM_IdentityService?.lookup, SolanaIdentityService?.lookup]) }