From 8b4f5929f6af85a65e1f3638a402419438babb68 Mon Sep 17 00:00:00 2001 From: teodorus-nathaniel Date: Thu, 29 Aug 2024 19:29:37 +0700 Subject: [PATCH] Fix issue with cache - Call the invalidation onStart of the mutation - Add 5 mins window for accounts data api to not use cache (like profiles caching works) --- src/pages/api/accounts-data.ts | 27 +++++++++++++++---- src/pages/api/profiles.ts | 2 +- .../subsocial/evmAddresses/mutation.tsx | 6 +++-- src/services/subsocial/profiles/mutation.ts | 6 +---- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/pages/api/accounts-data.ts b/src/pages/api/accounts-data.ts index 23255896d..e326089c1 100644 --- a/src/pages/api/accounts-data.ts +++ b/src/pages/api/accounts-data.ts @@ -37,11 +37,16 @@ const GET_ENS_NAMES = gql(` } `) -const MAX_AGE = 60 * 60 // 1 hour +const MAX_AGE = 15 * 60 // 15 minutes const getRedisKey = (address: string) => { return `accounts-data:${address}` } +const INVALIDATED_MAX_AGE = 5 * 60 // 5 minutes +const getInvalidatedAccountsDataRedisKey = (id: string) => { + return `accounts-data-invalidated:${id}` +} + export default async function handler( req: NextApiRequest, res: NextApiResponse @@ -78,7 +83,15 @@ export default async function handler( function invalidateCache(addresses: string[]) { addresses.forEach((address) => { - redisCallWrapper((redis) => redis?.del(getRedisKey(address))) + redisCallWrapper(async (redis) => { + await redis?.del(getRedisKey(address)) + return redis?.set( + getInvalidatedAccountsDataRedisKey(address), + address, + 'EX', + INVALIDATED_MAX_AGE + ) + }) }) } @@ -183,9 +196,13 @@ export async function getAccountsDataFromCache( let newlyFetchedData: AccountData[] = [] const promises = addresses.map(async (address) => { - const cachedData = await redisCallWrapper((redis) => - redis?.get(getRedisKey(address)) - ) + const cachedData = await redisCallWrapper(async (redis) => { + const isInvalidated = await redis?.get( + getInvalidatedAccountsDataRedisKey(address) + ) + if (!isInvalidated) return redis?.get(getRedisKey(address)) + return null + }) if (cachedData) { const parsedData = JSON.parse(cachedData) as AccountData diff --git a/src/pages/api/profiles.ts b/src/pages/api/profiles.ts index 442b64eeb..e531e5a94 100644 --- a/src/pages/api/profiles.ts +++ b/src/pages/api/profiles.ts @@ -25,7 +25,7 @@ type ResponseData = { export type ApiProfilesResponse = ApiResponse export type ApiProfilesInvalidationResponse = ApiResponse<{}> -const INVALIDATED_MAX_AGE = 1 * 60 // 1 minute +const INVALIDATED_MAX_AGE = 2 * 60 // 2 minutes const getInvalidatedProfileRedisKey = (id: string) => { return `profiles-invalidated:${id}` } diff --git a/src/services/subsocial/evmAddresses/mutation.tsx b/src/services/subsocial/evmAddresses/mutation.tsx index 0bbfb7b65..13cb77a75 100644 --- a/src/services/subsocial/evmAddresses/mutation.tsx +++ b/src/services/subsocial/evmAddresses/mutation.tsx @@ -69,9 +69,11 @@ export function useLinkEvmAddress({ config, { txCallbacks: { - onStart: () => setOnCallbackLoading(true), + onStart: ({ address }) => { + setOnCallbackLoading(true) + mutateAccountsDataCache(address) + }, onSuccess: async ({ address }) => { - await mutateAccountsDataCache(address) getAccountDataQuery.invalidate(client, address) setOnCallbackLoading(false) diff --git a/src/services/subsocial/profiles/mutation.ts b/src/services/subsocial/profiles/mutation.ts index cc01c844e..ce01a6ffb 100644 --- a/src/services/subsocial/profiles/mutation.ts +++ b/src/services/subsocial/profiles/mutation.ts @@ -76,6 +76,7 @@ export function useUpsertProfile( txCallbacks: { onStart: ({ data, address }) => { preventWindowUnload() + invalidateProfileServerCache(address) getProfileQuery.setQueryData(client, address, (oldData) => { const oldProfileSpaceId = oldData?.profileSpace?.id const oldProfileContent = oldData?.profileSpace?.content || {} @@ -100,11 +101,6 @@ export function useUpsertProfile( onError: async ({ address }) => { getProfileQuery.invalidate(client, address) }, - onSuccess: async ({ address }) => { - await invalidateProfileServerCache(address) - // Remove invalidation because the data will be same, and sometimes IPFS errors out, making the profile gone - // getProfileQuery.invalidate(client, address) - }, }, } )