评论缓存是一种很好的做法吗?我应该如何正确地使用它?

问题描述

我尝试过基于 Ben Awad 课程的类似 reddit 的网络应用程序。我在单个帖子页面添加评论部分。我对帖子和评论都有光标分页。这就是它的样子:

const cursorCommentsPagination = (): Resolver => {
  return (_parent,fieldArgs,cache,info) => {
    const { parentKey: entityKey,fieldName } = info;
    const allFields = cache.inspectFields(entityKey);
    const fieldInfos = allFields.filter(
      (info) =>
        info.fieldName === fieldName &&
        info.arguments!.postId ===
          parseInt(stringifyVariables(fieldArgs.postId)) // taking only posts with this postId
    );
    const size = fieldInfos.length;
    if (size === 0) {
      return undefined;
    }
    const fieldKey = `${fieldName}(${stringifyVariables(fieldArgs)})`;
    const isItInTheCache = cache.resolve(entityKey,fieldKey) as string;
    info.partial = !isItInTheCache;
    let hasMore = true;
    const results: string[] = [];

    fieldInfos.forEach((fi) => {
      const key = cache.resolve(entityKey,fi.fieldKey) as string;
      const data = cache.resolve(key,"comments") as string[];
      const _hasMore = cache.resolve(key,"hasMore");
      if (!_hasMore) {
        hasMore = _hasMore as boolean;
      }
      results.push(...data);
    });

    return {
      __typename: "PaginatedComments",hasMore,comments: results,};
  };
};

由于某种原因,它运行起来很奇怪 - 它缓存了我访问的每个帖子页面中的所有评论,并在我访问的下一个帖子中显示所有缓存的评论 所以我决定添加这个:

...
parseInt(stringifyVariables(fieldArgs.postId)) // taking only posts with this postId
...

而且它工作正常。但它现在只缓存一个帖子中的评论,所以如果我转到另一篇文章,缓存会重置,如果我返回并转到同一篇文章,缓存工作正常。 现在我不确定,我是否需要缓存评论,或者有什么方法可以缓存所有评论并只在页面显示需要的评论

这是我的仓库:https://github.com/tmelent/NodejsFullstackTutorial

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)