Skip to content

Commit

Permalink
Fix issue with cache
Browse files Browse the repository at this point in the history
- Call the invalidation onStart of the mutation
- Add 5 mins window for accounts data api to not use cache (like profiles caching works)
  • Loading branch information
teodorus-nathaniel committed Aug 29, 2024
1 parent 91ff410 commit 8b4f592
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
27 changes: 22 additions & 5 deletions src/pages/api/accounts-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApiAccountDataResponse>
Expand Down Expand Up @@ -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
)
})
})
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type ResponseData = {
export type ApiProfilesResponse = ApiResponse<ResponseData>
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}`
}
Expand Down
6 changes: 4 additions & 2 deletions src/services/subsocial/evmAddresses/mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions src/services/subsocial/profiles/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}
Expand All @@ -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)
},
},
}
)
Expand Down

0 comments on commit 8b4f592

Please sign in to comment.