Skip to content

Commit

Permalink
fix emailable cache and improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeBu committed Dec 17, 2024
1 parent 94e6fc6 commit 175ba5e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class HttpAddressGateway implements AddressGateway {
minLength: queryMinLength,
});

return this.#withCache({
return this.#withCache<string>({
overrideCacheDurationInHours: 24,
getCacheKey: (query) => `geosearch_${query}`,
logParams: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("createRedisWithCache implementation", () => {
redisClient,
});
calls.length = 0;
cachedCallToPartner = withCache({
cachedCallToPartner = withCache<string>({
cb: someCallToAPartner,
getCacheKey: (query) => query,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export const makeRedisWithCache: MakeWithCache<RedisWithCacheConfig> =
({ defaultCacheDurationInHours, redisClient }) =>
({ overrideCacheDurationInHours, getCacheKey, cb, logParams }) => {
type Callback = typeof cb;
return (async (...params: Parameters<Callback>) => {
return (async (param: Parameters<Callback>[0]) => {
const start = Date.now();
if (!redisClient.isOpen) return cb(...params);
const cacheKey = getCacheKey(...params);
if (!redisClient.isOpen) return cb(param);
const cacheKey = getCacheKey(param);

const cachedValue = await redisClient.get(cacheKey);
if (cachedValue) {
Expand Down Expand Up @@ -50,7 +50,7 @@ export const makeRedisWithCache: MakeWithCache<RedisWithCacheConfig> =
const durationInSecond =
(overrideCacheDurationInHours ?? defaultCacheDurationInHours) * 3600;

const result = await cb(...params);
const result = await cb(param);

try {
await redisClient.setEx(
Expand Down
7 changes: 5 additions & 2 deletions back/src/domains/core/caching-gateway/port/WithCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ export type DefaultCacheConfig = {

export type MakeWithCache<
Config extends DefaultCacheConfig = DefaultCacheConfig,
> = (config: Config) => <Cb extends (...params: any[]) => Promise<any>>(args: {
> = (config: Config) => <
T,
Cb extends (param: T) => Promise<any> = (param: T) => Promise<any>,
>(args: {
overrideCacheDurationInHours?: number;
getCacheKey: (...params: Parameters<Cb>) => string;
getCacheKey: (param: Parameters<Cb>[0]) => string;
cb: Cb;
logParams?: { route: UnknownSharedRoute; partner: PartnerKey };
}) => Cb;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Bottleneck from "bottleneck";
import { ValidateEmailFeedback } from "shared";
import { Email, ValidateEmailFeedback } from "shared";
import { HttpClient } from "shared-routes";
import { createLogger } from "../../../../utils/logger";
import { WithCache } from "../../caching-gateway/port/WithCache";
Expand Down Expand Up @@ -34,13 +34,13 @@ export class EmailableEmailValidationGateway implements EmailValidationGetaway {
): Promise<ValidateEmailFeedback> {
const sanitizedEmail = emailInParams.toLowerCase().trim();

return this.withCache({
return this.withCache<Email>({
logParams: {
partner: "emailable",
route: emailableValidationRoutes.validateEmail,
},
overrideCacheDurationInHours: thirtyDaysInHours,
getCacheKey: ({ email }) => `emailable_${email}`,
getCacheKey: (email) => `emailable_${email}`,
cb: (email) =>
this.#limiter.schedule(() =>
this.httpClient
Expand Down

0 comments on commit 175ba5e

Please sign in to comment.