Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

[No Ticket] Consider all parameters when caching idex results #215

Merged
merged 3 commits into from
Oct 18, 2023
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
49 changes: 27 additions & 22 deletions src/idex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ import { IdentityResolutionConfig, State, ResolutionParams, EventBus, RetrievedI
import { WrappedCallHandler } from './handlers/call-handler'
import { DurableCache, NoOpCache } from './cache'

const IDEX_STORAGE_KEY = '__li_idex_cache'

function _cacheKey(rawKey: unknown) {
if (rawKey) {
const suffix = base64UrlEncode(JSON.stringify(rawKey))
return `${IDEX_STORAGE_KEY}_${suffix}`
} else {
return IDEX_STORAGE_KEY
}
}
const IDEX_STORAGE_KEY = '__li_idex_cache2'

export type ResolutionMetadata = {
expiresAt?: Date,
Expand Down Expand Up @@ -94,21 +85,21 @@ export class IdentityResolver {
return IdentityResolver.make(config || {}, NoOpCache, calls, eventBus)
}

private getCached(key: unknown): [unknown, ResolutionMetadata] | null {
const cachedValue = this.cache.get(_cacheKey(key))
private getCached(key: string): [unknown, ResolutionMetadata] | null {
const cachedValue = this.cache.get(cacheKey(key))
if (cachedValue) {
return [JSON.parse(cachedValue.data), { expiresAt: cachedValue.meta.expiresAt, resolvedAt: cachedValue.meta.writtenAt }]
} else {
return null
}
}

private setCached(key: unknown, value: unknown, expiresAt?: Date) {
this.cache.set(_cacheKey(key), JSON.stringify(value), expiresAt || expiresInHours(this.defaultExpirationHours))
private setCached(key: string, value: unknown, expiresAt?: Date) {
this.cache.set(cacheKey(key), JSON.stringify(value), expiresAt || expiresInHours(this.defaultExpirationHours))
}

private responseReceived(
additionalParams: ResolutionParams,
idexPath: string,
successCallback: (result: unknown, meta: ResolutionMetadata) => void
): ((responseText: string, response: unknown) => void) {
return (responseText, response) => {
Expand All @@ -124,29 +115,28 @@ export class IdentityResolver {

const expiresAt = responseExpires(response)
const resolvedAt = new Date()
this.setCached(additionalParams, responseObj, expiresAt)
this.setCached(idexPath, responseObj, expiresAt)
successCallback(responseObj, { expiresAt, resolvedAt })
}
}

unsafeResolve(successCallback: (result: unknown, meta: ResolutionMetadata) => void, errorCallback: (e: unknown) => void, additionalParams: ResolutionParams): void {
const cachedValue = this.getCached(additionalParams)
const idexPath = this.buildPath(additionalParams)
const cachedValue = this.getCached(idexPath)
if (cachedValue) {
successCallback(...cachedValue)
} else {
this.calls.ajaxGet(
this.getUrl(additionalParams),
this.responseReceived(additionalParams, successCallback),
this.buildUrl(idexPath),
this.responseReceived(idexPath, successCallback),
errorCallback,
this.timeout
)
}
}

getUrl(additionalParams: Record<string, string | string[]>): string {
const originalParams = this.tuples.slice().concat(mapAsParams(additionalParams))
const params = toParams(originalParams)
return `${this.url}/${this.source}/${this.publisherId}${params}`
return this.buildUrl(this.buildPath(additionalParams))
}

resolve(successCallback: (result: unknown, meta: ResolutionMetadata) => void, errorCallback?: (e: unknown) => void, additionalParams?: ResolutionParams): void {
Expand All @@ -162,6 +152,16 @@ export class IdentityResolver {
}
}
}

private buildPath(additionalParams: Record<string, string | string[]>): string {
const originalParams = this.tuples.slice().concat(mapAsParams(additionalParams))
const params = toParams(originalParams)
return `${this.source}/${this.publisherId}${params}`
}

private buildUrl(path: string): string {
return `${this.url}/${path}`
}
}

function responseExpires(response: unknown) {
Expand All @@ -172,3 +172,8 @@ function responseExpires(response: unknown) {
}
}
}

function cacheKey(rawKey: string): string {
const suffix = base64UrlEncode(JSON.stringify(rawKey))
return `${IDEX_STORAGE_KEY}_${suffix}`
}
4 changes: 2 additions & 2 deletions test/unit/idex/identity-resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('IdentityResolver', () => {
expect(requestToComplete.url).to.eq('https://idx.liadm.com/idex/unknown/any')
expect(responseAsJson).to.be.eql(response)
expect(callCount).to.be.eql(1)
expect(storageHandler.getCookie('__li_idex_cache_e30')).to.be.eq(JSON.stringify(response))
expect(storageHandler.getCookie('__li_idex_cache2_InVua25vd24vYW55Ig')).to.be.eq(JSON.stringify(response))
done()
}
identityResolver.resolve(successCallback)
Expand Down Expand Up @@ -380,7 +380,7 @@ describe('IdentityResolver', () => {
expect(responseAsJson).to.be.eql(response)
expect(callCount).to.be.eql(1)

expect(storageHandler.getCookie('__li_idex_cache_e30')).to.be.eq(JSON.stringify(response))
expect(storageHandler.getCookie('__li_idex_cache2_InVua25vd24vYW55Ig')).to.be.eq(JSON.stringify(response))
expect(epochSeconds(recordedExpiresAt!)).to.be.eq(epochSeconds(expiresAt))

done()
Expand Down