Skip to content

Commit

Permalink
refactor: adjust naming slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
Drodevbar committed Jan 10, 2025
1 parent 7858855 commit 68266a6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export interface MessageDeduplicationKeyGenerator<MessageType extends object = o
}

export interface MessageDeduplicationStore {
storeCacheKey(key: string, value: string, ttlSeconds: number): Promise<void>
retrieveCacheKey(key: string): Promise<string | null>
storeKey(key: string, value: string, ttlSeconds: number): Promise<void>
retrieveKey(key: string): Promise<string | null>
}

export type MessageDeduplicationConfig = {
Expand Down
20 changes: 11 additions & 9 deletions packages/core/lib/queues/AbstractQueueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,29 +522,31 @@ export abstract class AbstractQueueService<
}
}

/** Retrieves cache key from deduplication store and checks if it exists */
/** Checks for an existence of deduplication key in deduplication store */
protected async isMessageDuplicated(message: MessagePayloadSchemas): Promise<boolean> {
if (!this.messageDeduplicationConfig) {
return false
}

const cacheKey = this.messageDeduplicationConfig.deduplicationKeyGenerator.generate(message)
const retrievedCacheKey =
await this.messageDeduplicationConfig.deduplicationStore.retrieveCacheKey(cacheKey)
const deduplicationKey =
this.messageDeduplicationConfig.deduplicationKeyGenerator.generate(message)
const deduplicationValue =
await this.messageDeduplicationConfig.deduplicationStore.retrieveKey(deduplicationKey)

return retrievedCacheKey !== null
return deduplicationValue !== null
}

/** Stores cache key in deduplication store */
/** Stores deduplication key in deduplication store */
protected async deduplicateMessage(message: MessagePayloadSchemas): Promise<void> {
if (!this.messageDeduplicationConfig) {
return
}

const cacheKey = this.messageDeduplicationConfig.deduplicationKeyGenerator.generate(message)
const deduplicationKey =
this.messageDeduplicationConfig.deduplicationKeyGenerator.generate(message)

await this.messageDeduplicationConfig.deduplicationStore.storeCacheKey(
cacheKey,
await this.messageDeduplicationConfig.deduplicationStore.storeKey(
deduplicationKey,
new Date().toISOString(),
this.messageDeduplicationConfig.deduplicationWindowSeconds,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ export class RedisMessageDeduplicationStore implements MessageDeduplicationStore
this.config = config
}

async storeCacheKey(key: string, value: string, ttlSeconds: number): Promise<void> {
const cacheKey = this.getCacheKeyWithOptionalPrefix(key)
async storeKey(key: string, value: string, ttlSeconds: number): Promise<void> {
const keyWithPrefix = this.getKeyWithOptionalPrefix(key)

await this.redis.set(cacheKey, value, 'EX', ttlSeconds)
await this.redis.set(keyWithPrefix, value, 'EX', ttlSeconds)
}

retrieveCacheKey(key: string): Promise<string | null> {
const cacheKey = this.getCacheKeyWithOptionalPrefix(key)
retrieveKey(key: string): Promise<string | null> {
const keyWithPrefix = this.getKeyWithOptionalPrefix(key)

return this.redis.get(cacheKey)
return this.redis.get(keyWithPrefix)
}

private getCacheKeyWithOptionalPrefix(key: string): string {
private getKeyWithOptionalPrefix(key: string): string {
return this.config?.keyPrefix?.length ? `${this.config.keyPrefix}:${key}` : key
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ describe('RedisMessageDeduplicationStore', () => {
await cleanRedis(redis)
})

describe('storeCacheKey', () => {
it('stores a cache key in Redis with provided value and ttl', async () => {
describe('storeKey', () => {
it('stores a key in Redis with provided value and ttl', async () => {
const key = 'test_key'
const value = 'test_value'
const ttlSeconds = 60

await store.storeCacheKey(key, value, ttlSeconds)
await store.storeKey(key, value, ttlSeconds)

const storedValue = await redis.get(`${KEY_PREFIX}:${key}`)
expect(storedValue).toBe(value)
Expand All @@ -49,15 +49,15 @@ describe('RedisMessageDeduplicationStore', () => {
})
})

describe('retrieveCacheKey', () => {
it('retrieves a cache key from Redis', async () => {
describe('retrieveKey', () => {
it('retrieves a key from Redis', async () => {
const key = 'test_key'
const value = 'test_value'
const ttlSeconds = 60

await redis.set(`${KEY_PREFIX}:${key}`, value, 'EX', ttlSeconds)

const retrievedValue = await store.retrieveCacheKey(key)
const retrievedValue = await store.retrieveKey(key)

expect(retrievedValue).toBe(value)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('SqsPermissionPublisher', () => {
await diContainer.dispose()
})

it('writes cache key to redis using provided deduplication function and publishes message', async () => {
it('writes deduplication key to store using provided deduplication function and publishes message', async () => {
const message = {
id: '1',
messageType: 'add',
Expand All @@ -69,13 +69,13 @@ describe('SqsPermissionPublisher', () => {
expect(spy.message).toEqual(message)
expect(spy.processingResult).toBe('published')

const cacheKey = await messageDeduplicationStore.retrieveCacheKey(
const deduplicationKey = await messageDeduplicationStore.retrieveKey(
messageDeduplicationKeyGenerator.generate(message),
)
expect(cacheKey).not.toBeNull()
expect(deduplicationKey).not.toBeNull()
})

it('does not publish the same message if cache key already exists', async () => {
it('does not publish the same message if deduplication key already exists', async () => {
const message = {
id: '1',
messageType: 'add',
Expand All @@ -101,7 +101,7 @@ describe('SqsPermissionPublisher', () => {
expect(spySecondCall).toBeUndefined()
})

it('publishing messages that produce different cache keys does not affect each other', async () => {
it('publishing messages that produce different deduplication keys does not affect each other', async () => {
const message1 = {
id: '1',
messageType: 'add',
Expand Down

0 comments on commit 68266a6

Please sign in to comment.