docs

Performance

To improve our application performance, we can use some strategies like cache, redis and compression.

Inside the app/config/performance.ts file, you can active redis, cache strategy and define cache life time in seconds. If cache and redis are active, we’ll use redis to store the cache.

We can cache our query results like:

  // app/services/UserService.ts

  static async index(currentPage: number = 1) {
    const perPage = 10;
    const page = currentPage ? currentPage : 1;
    const skip = (page - 1) * perPage;

    // Cache
    const key = `users.list.params=${skip}_${perPage}`;
    const cached = await useCache.get(key);

    if (cached) {
      return JSON.parse(cached);
    }

    // Queries
    const totalUsers = await User.count();
    const query = await User.findMany({ take: perPage, skip });
    const users = ResponseUtils.excludeFromList(query, ["password"]);

    const response = ResponseUtils.paginate({
      data: users,
      totalData: totalUsers,
      page,
      perPage,
    });

    await useCache.set(key, JSON.stringify(response));

    return response;
  }

From 100kb the response will be compressed with gzip. If you don’t want to compress some page, you can pass x-no-compressionheader.

Summary