Skip to content

Commit

Permalink
feat(2981): fix review comments from Max.
Browse files Browse the repository at this point in the history
  • Loading branch information
koekiebox committed Nov 22, 2024
1 parent 436e2fc commit d55d93e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
16 changes: 8 additions & 8 deletions packages/backend/src/asset/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ export interface AssetService {

interface ServiceDependencies extends BaseService {
accountingService: AccountingService
cacheDataStore: CacheDataStore<Asset>
assetCache: CacheDataStore<Asset>
}

export async function createAssetService({
logger,
knex,
accountingService,
cacheDataStore
assetCache
}: ServiceDependencies): Promise<AssetService> {
const log = logger.child({
service: 'AssetService'
Expand All @@ -63,7 +63,7 @@ export async function createAssetService({
logger: log,
knex,
accountingService,
cacheDataStore
assetCache
}

return {
Expand Down Expand Up @@ -97,7 +97,7 @@ async function createAsset(
const reActivated = await Asset.query(deps.knex)
.patchAndFetchById(deletedAsset.id, { deletedAt: null })
.throwIfNotFound()
await deps.cacheDataStore.set(reActivated.id, reActivated)
await deps.assetCache.set(reActivated.id, reActivated)
return reActivated
}

Expand Down Expand Up @@ -141,7 +141,7 @@ async function updateAsset(
.patchAndFetchById(id, { withdrawalThreshold, liquidityThreshold })
.throwIfNotFound()

await deps.cacheDataStore.set(id, asset)
await deps.assetCache.set(id, asset)
return asset
} catch (err) {
if (err instanceof NotFoundError) {
Expand All @@ -160,7 +160,7 @@ async function deleteAsset(
throw new Error('Knex undefined')
}

await deps.cacheDataStore.delete(id)
await deps.assetCache.delete(id)
try {
// return error in case there is a peer or wallet address using the asset
const peer = await Peer.query(deps.knex).where('assetId', id).first()
Expand Down Expand Up @@ -189,11 +189,11 @@ async function getAsset(
deps: ServiceDependencies,
id: string
): Promise<void | Asset> {
const inMem = (await deps.cacheDataStore.get(id)) as Asset
const inMem = (await deps.assetCache.get(id)) as Asset
if (inMem) return inMem

const asset = await Asset.query(deps.knex).whereNull('deletedAt').findById(id)
if (asset) await deps.cacheDataStore.set(asset.id, asset)
if (asset) await deps.assetCache.set(asset.id, asset)

return asset
}
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export const Config = {
'MAX_OUTGOING_PAYMENT_RETRY_ATTEMPTS',
5
),
localCacheDuration: envInt('LOCAL_CACHE_DURATION', 5_000)
localCacheDuration: envInt('LOCAL_CACHE_DURATION_MS', 15_000)
}

function parseRedisTlsConfig(
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export function initIocContainer(
logger: logger,
knex: knex,
accountingService: await deps.use('accountingService'),
cacheDataStore: createInMemoryDataStore(config.localCacheDuration)
assetCache: createInMemoryDataStore(config.localCacheDuration)
})
})

Expand Down Expand Up @@ -289,7 +289,7 @@ export function initIocContainer(
accountingService: await deps.use('accountingService'),
webhookService: await deps.use('webhookService'),
assetService: await deps.use('assetService'),
cacheDataStore: createInMemoryDataStore(config.localCacheDuration)
walletAddressCache: createInMemoryDataStore(config.localCacheDuration)
})
})
container.singleton('spspRoutes', async (deps) => {
Expand Down
17 changes: 10 additions & 7 deletions packages/backend/src/open_payments/wallet_address/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ interface ServiceDependencies extends BaseService {
accountingService: AccountingService
webhookService: WebhookService
assetService: AssetService
cacheDataStore: CacheDataStore<WalletAddress>
walletAddressCache: CacheDataStore<WalletAddress>
}

export async function createWalletAddressService({
Expand All @@ -96,7 +96,7 @@ export async function createWalletAddressService({
accountingService,
webhookService,
assetService,
cacheDataStore
walletAddressCache
}: ServiceDependencies): Promise<WalletAddressService> {
const log = logger.child({
service: 'WalletAddressService'
Expand All @@ -108,7 +108,7 @@ export async function createWalletAddressService({
accountingService,
webhookService,
assetService,
cacheDataStore
walletAddressCache
}
return {
create: (options) => createWalletAddress(deps, options),
Expand Down Expand Up @@ -188,7 +188,7 @@ async function createWalletAddress(
additionalProperties: additionalProperties
})
await deps.assetService.setOn(walletAddress)
await deps.cacheDataStore.set(walletAddress.id, walletAddress)
await deps.walletAddressCache.set(walletAddress.id, walletAddress)
return walletAddress
} catch (err) {
if (err instanceof ForeignKeyViolationError) {
Expand Down Expand Up @@ -247,7 +247,10 @@ async function updateWalletAddress(
}
await trx.commit()

await deps.cacheDataStore.set(updatedWalletAddress.id, updatedWalletAddress)
await deps.walletAddressCache.set(
updatedWalletAddress.id,
updatedWalletAddress
)
return updatedWalletAddress
} catch (err) {
await trx.rollback()
Expand All @@ -262,13 +265,13 @@ async function getWalletAddress(
deps: ServiceDependencies,
id: string
): Promise<WalletAddress | undefined> {
const walletAdd = await deps.cacheDataStore.get(id)
const walletAdd = await deps.walletAddressCache.get(id)
if (walletAdd) return walletAdd as WalletAddress

const walletAddress = await WalletAddress.query(deps.knex).findById(id)
if (walletAddress) {
await deps.assetService.setOn(walletAddress)
await deps.cacheDataStore.set(id, walletAddress)
await deps.walletAddressCache.set(id, walletAddress)
}
return walletAddress
}
Expand Down

0 comments on commit d55d93e

Please sign in to comment.