Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with cache #701

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading