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: indicating to alternative persistence whether it is trying to get before first semaphore #29

Merged
merged 1 commit into from
Jun 17, 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
2 changes: 1 addition & 1 deletion src/remembered-redis-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface AlternativePersistence {
* Get the content for the informed key
* @param key the key for the content
*/
get(key: string): Promise<Buffer | string | undefined>;
get(key: string, firstCheck: boolean): Promise<Buffer | string | undefined>;

maxSavingDelay?: number;
maxResultsPerSave?: number;
Expand Down
6 changes: 4 additions & 2 deletions src/remembered-redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class RememberedRedis extends Remembered {
await this.tryTo(semaphore.acquire.bind(semaphore));
}
try {
return await this.getFromCacheInternal(key);
return await this.getFromCacheInternal(key, false);
} finally {
if (!noSemaphore) {
this.tryTo(semaphore.release.bind(semaphore));
Expand Down Expand Up @@ -316,7 +316,7 @@ export class RememberedRedis extends Remembered {
}

private async tryCache<T>(key: string, callback: () => PromiseLike<T>) {
const result = await this.getFromCacheInternal<T>(key);
const result = await this.getFromCacheInternal<T>(key, true);
if (result !== EMPTY) {
this.onCache?.(key);
return result;
Expand All @@ -326,6 +326,7 @@ export class RememberedRedis extends Remembered {

private async getFromCacheInternal<T>(
key: string,
firstCheck: boolean,
): Promise<T | typeof EMPTY> {
const redisKey = this.getRedisKey(key);
const cached = await this.try(redisKey, () =>
Expand All @@ -338,6 +339,7 @@ export class RememberedRedis extends Remembered {
) {
const alternativeCached = await this.alternativePersistence.get(
cached.toString(),
firstCheck,
);
if (alternativeCached) {
const deserialized = await this.serializer.deserialize(
Expand Down
6 changes: 3 additions & 3 deletions test/unit/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ describe('index.ts', () => {

expect(target['getSemaphore']).toHaveCallsLike(['my key']);
expect(acquire).toHaveCallsLike([semaphore]);
expect(target['getFromCacheInternal']).toHaveCallsLike(['my key']);
expect(target['getFromCacheInternal']).toHaveCallsLike(['my key', false]);
expect(release).toHaveCallsLike([semaphore]);
expect(target['tryTo']).toHaveCallsLike(
['acquire result'],
Expand All @@ -322,7 +322,7 @@ describe('index.ts', () => {

expect(target['getSemaphore']).toHaveCallsLike(['my key']);
expect(acquire).toHaveCallsLike([semaphore]);
expect(target['getFromCacheInternal']).toHaveCallsLike(['my key']);
expect(target['getFromCacheInternal']).toHaveCallsLike(['my key', false]);
expect(release).toHaveCallsLike([semaphore]);
expect(target['tryTo']).toHaveCallsLike(
['acquire result'],
Expand All @@ -336,7 +336,7 @@ describe('index.ts', () => {

expect(target['getSemaphore']).toHaveCallsLike(['my key']);
expect(acquire).toHaveCallsLike();
expect(target['getFromCacheInternal']).toHaveCallsLike(['my key']);
expect(target['getFromCacheInternal']).toHaveCallsLike(['my key', false]);
expect(release).toHaveCallsLike();
expect(target['tryTo']).toHaveCallsLike();
expect(result).toBe('expected result');
Expand Down