andreafspeziale / nestjs-memcached

Memcached module for Nest framework (node.js) 🐈
https://nestjs.com/
MIT License
17 stars 0 forks source link

v4 #319

Open andreafspeziale opened 2 weeks ago

andreafspeziale commented 2 weeks ago

I want to move the package to the v4 introducing or at least consider the following:

andreafspeziale commented 5 days ago

💡 I found myself having different TTLs in my applications and in order to retrieve the correct one I've always relied on the injected ConfigService. Can I set those TTLs in the Memcached service instead?

andreafspeziale commented 5 days ago

💡 What if every context not only adds a different TTL to the actual "set" operation but also adds a Version (which I should overall revise as a string instead of a number) and adds some kind of "postfix" to the caching key?

andreafspeziale commented 5 days ago

💡 This is what I would like to avoid speaking of TTLs contexts:

constructor(
    private readonly searchService: SearchService,
    private readonly memcachedService: MemcachedService,
    private readonly configService: ConfigService<Config, true>,
  ) {}

  async handleSearch(index: string, query: UnknownQuery, opts: Opts): Promise<Search> {
    const hash = createHash('md5').update(JSON.stringify(query)).digest('hex');

    if (opts.cache) {
      const cached = await this.memcachedService.get<Cached<Search>>(`${index}::${hash}::search`);

      if (cached) {
        return cached.content;
      }
    }

    const search = await this.searchService.search(index, query);
    await this.memcachedService.setWithMeta<Search, Cached<Search>>(`${hash}::search`, search, {
      ttl: this.configService.get<Config['memcached']>('memcached').contexts[
        MemcachedContexts.Search
      ].ttl,
      ttr: this.configService.get<Config['memcached']>('memcached').contexts[
        MemcachedContexts.Search
      ].ttr,
    });

    return search;
  }