BJS-kr / nestjs-omacache

flexible, powerful and simple caching strategy using decorators for NestJS
72 stars 4 forks source link

Set url as default key #24

Closed moioo91120 closed 2 months ago

moioo91120 commented 3 months ago

As of now, we have to set key. It's good for a lot of case but is it possible to set "url" as default key ?

In my case, I have "getTicker24h" endpoints from multiple provider. Then, I have multiple controllers and a generic super controller where I set my logic. Endpoints are going to look like "/provider/{provider name}/{endpoints}", for example "/provider/binance/getTicker24h"

Standard nestjs cache-manager is going to set my key to "/provider/binance/getTicker24h" because it caches url. Your library is asking for a key, so i'm going to set getTicker24h because my controller logic is defined in super.

If i have in my controller

 @Get('getTicker24h')
  @CacheStorage({
    key: `getTicker24h`,
    kind: 'temporal',
    ttl: 60,
    paramIndex: [0],
  })
  @UseInterceptors(CacheInterceptor)
  async getTicker24h(@Query() params: ZTickersParamsDto): Promise<ITickerResponse[]> {
    return this.exchangeService.execPublic(this.provider, 'fetchTickers', params);
  }

I get in redis :

image

Because decorator needs static string, I can't set my provider name, meaning I can't prefixe the cache depending on the full endpoint that was called. Do you think we could set logic so that it takes the url (before params) by default if no key are defined ?

Thanks

BJS-kr commented 3 months ago

can you provide your implementation details? like, how do you create controllers using 'super'?

if you can tell me more about your case, I will add your case in the test

thanks

moioo91120 commented 3 months ago

So, I have a generic exchange controller

export class GenericExchangeController implements IGenericExchangeController {
  protected logger = new Logger(this.constructor.name);
  constructor(
    protected readonly exchangeService: ExchangeService,
    protected readonly bankSyncService: BankSyncService,
    protected readonly provider?: SourceProvider
  ) {
    if (!provider) this.provider = null;
  }

  @Get('getTicker24h')
  @CacheTTL(5 * MINUTES)
  @UseInterceptors(CacheInterceptor)
  async getTicker24h(@Query() params: ZTickersParamsDto): Promise<ITickerResponse[]> {
    return this.exchangeService.execPublic(this.provider, 'fetchTickers', params);
  }
...
}

and then multiple controllers for each provider using the generic controller

@Controller(`provider/binance`)
export class BinanceController extends GenericExchangeController implements IBinanceController {
  constructor(
    protected readonly exchangeService: ExchangeService,
    protected readonly bankSyncService: BankSyncService
  ) {
    super(exchangeService, bankSyncService, 'binance');
  }
....
}

@Controller(`provider/coinbase`)
export class CoinbaseController extends GenericExchangeController implements ICoinbaseController {
  constructor(
    protected readonly exchangeService: ExchangeService,
    protected readonly bankSyncService: BankSyncService
  ) {
    super(exchangeService, bankSyncService, 'coinbase');
  }
}

Then, endpoints calls are like GET /provider/binance/getTicker24h GET /provider/coinbase/getTicker24h

and so on.

BJS-kr commented 3 months ago

ah, now I get it clearly.

I have few ideas, but I'm trying not to be too hacky, which is dangerous for future nest's changes. I'll share when I find out some good ways.

and of course, I'm always open, so tell me your ideas anytime

thank you for using this package

moioo91120 commented 3 months ago

I think a good way to handle it would be to use something like that

And construct the cache key name from a variable. That way, we can control it in controller as well as service and the user can chose how he wants to construct his cache key name (or prefix maybe ?) directly in the class constructor.

BJS-kr commented 3 months ago

thanks! I'll check it out

BJS-kr commented 3 months ago

how about using custom param decorator and use index of it? for example,

export const FullUrl = createParamDecorator(
  (data: unknown, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    const fullUrl = ...

    return fullUrl
  },
);

and use it in parent class like

export class GenericExchangeController implements IGenericExchangeController {
  protected logger = new Logger(this.constructor.name);
  constructor(
    protected readonly exchangeService: ExchangeService,
    protected readonly bankSyncService: BankSyncService,
    protected readonly provider?: SourceProvider
  ) {
    if (!provider) this.provider = null;
  }

  @Get('getTicker24h')
  @CacheStorage({
    key: `getTicker24h`,
    kind: 'temporal',
    ttl: 60,
    paramIndex: [0,1],
  })
  async getTicker24h(@Query() params: ZTickersParamsDto, @FullUrl() fullUrl: string): Promise<ITickerResponse[]> {
    return this.exchangeService.execPublic(this.provider, 'fetchTickers', params);
  }
...
}

I think this would do the job

BJS-kr commented 2 months ago

Should I close this issue? Let me know if you need anything