typescript 即使未在代码中执行set,也会在get之后立即调用DataloaderMapset

mzsu5hc0  于 2023-03-09  发布在  TypeScript
关注(0)|答案(1)|浏览(142)

有人能告诉我为什么
当调用get时,set函数被调用,即使它不在代码中?
代码如下:

  • 主文件.ts*
console.log('1.New CustomDataloader');
    const core = new CustomDataloader();
    const response = await core.getLoader.load('myKey');
    console.log('3.Response:',response);
  • 自定义数据加载程序.ts*
const getCacheMap = new cacheMap<any>({});

private getResolver = async (keys: readonly string[]): Promise<any[]> => {
    // Just for test purposes return -> null

    return await Promise.all(
      keys.map(async (randomKey) => {
        return null;
      })
    );
  };

public getLoader = new DataLoader<string, any>(this.getResolver, {
    cache: true,
    cacheMap: getCacheMap,
    maxBatchSize: 100,
    batchScheduleFn: (callback: any) => setTimeout(callback, 50),
  });
  • 缓存Map.ts*
import { CacheMap } from 'dataloader';
    import NodeCache, { Options } from 'node-cache';
    //import LRUCache from "lru-cache";
    
  
    export default class cacheMap<V> implements CacheMap<string, Promise<V>> {
      private readonly cache: any;
    
      constructor(options: Options) {
        // Ignore options atm
        this.cache = new NodeCache({
          stdTTL: 5,
          checkperiod: 10,
          errorOnMissing: false,
          useClones: false,
          deleteOnExpire: true,
        });
      }
    
      public get(key: string): Promise<V> | void {
        console.log('2.Get key function is called, key:',key);
        if(this.cache.has(key)){
          return this.cache.get(key);
        }
      }
      public set(key: string, value: Promise<V>): void {
          console.log('[!] Set function is called -but why?');
      }
    
      public delete(key: string): void {
        this.cache.del(key);
      }
    
      public clear(): void {
        this.cache.flushAll();
      }
    }

其控制台日志为:

1.New CustomDataloader                                                                                                                                                                                                
2.Get key function is called, key: myKey                                                                                                                                                                             
[!] Set function is called -but why?                                                                                                                                                                                        
3.Response: null

为什么只调用get的时候还要设置一些东西?
尝试了很多方法,但无法理解为什么会发生这种情况,以及如何解决它。我唯一想到的是在cacheMap中添加标记,在get函数中设置ignoreSet= true,并在set中使用if语句将其 Package ,但这不是一个快速解决方案。
有什么主意吗?-谢谢。

  • 不要介意任何类型,我只是玩了几天的代码,并在测试时添加它们。*
6kkfgxo0

6kkfgxo01#

我做了一些研究,发现在GraphQL/Dataloader git中.load同时使用了函数**.get.set**
他们git里的代码片段

// If caching and there is a cache-hit, return cached Promise.
if (cacheMap) {
  var cachedPromise = cacheMap.get(cacheKey);
  if (cachedPromise) {
    var cacheHits = batch.cacheHits || (batch.cacheHits = []);
    return new Promise(resolve => {
      cacheHits.push(() => {
        resolve(cachedPromise);
      });
    });
  }
}

// Otherwise, produce a new Promise for this key, and enqueue it to be
// dispatched along with the current batch.
batch.keys.push(key);
var promise = new Promise((resolve, reject) => {
  batch.callbacks.push({ resolve, reject });
});

// If caching, cache this promise.
if (cacheMap) {
  cacheMap.set(cacheKey, promise);
}

这回答了我的问题

相关问题