# @tramvai/module-server-response-cache

> Caches the responses from the server for the requests in order to respond quickly

Latest version **1.110.2** (published 2022-06-23) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @tramvai/module-server-response-cache
pnpm add @tramvai/module-server-response-cache
yarn add @tramvai/module-server-response-cache
bun add @tramvai/module-server-response-cache
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.110.2 |
| Published | 2022-06-23 |
| First published | 2022-03-16 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 24.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 233 |
| Maintainers | super_oleg, tinkoffbank |

## Links

- npm: https://www.npmjs.com/package/@tramvai/module-server-response-cache
- Repository: https://github.com/Tinkoff/tramvai
- Homepage: https://github.com/Tinkoff/tramvai#readme
- Issues: https://github.com/Tinkoff/tramvai/issues
- npm.io page: https://npm.io/package/@tramvai/module-server-response-cache

## Dependencies (3)

- [@tinkoff/errors](https://npm.io/package/@tinkoff/errors.md) 0.2.21
- [@tramvai/tokens-common](https://npm.io/package/@tramvai/tokens-common.md) 1.110.2
- [@tramvai/tokens-server-private](https://npm.io/package/@tramvai/tokens-server-private.md) 1.110.2

## Recent versions

- 1.110.2 (latest) — 2022-06-23
- 2.32.0 (prerelease) — 2022-10-06
- 2.31.0 — 2022-10-05
- 2.29.0 — 2022-09-30
- 2.28.0 — 2022-09-28
- 2.27.0 — 2022-09-19
- 2.26.2 — 2022-09-16
- 2.26.0 — 2022-09-15
- 2.25.1 — 2022-09-14
- 2.24.3 — 2022-09-09
- 2.24.1 — 2022-09-09
- 2.24.0 — 2022-09-09
- 2.22.0 — 2022-09-02
- 2.21.1 — 2022-09-01
- 2.21.0 — 2022-09-01
- … 65 more at https://npm.io/package/@tramvai/module-server-response-cache/versions

## README

# Server response cache

Caches the responses from the server for the requests in order to respond quickly

## Installation

You need to install `@tramvai/module-server-response-cache`

```bash npm2yarn
npm i --save @tramvai/module-server-response-cache
```

And connect to the project

```tsx
import { createApp } from '@tramvai/core';
import { ServerResponseCacheModule } from '@tramvai/module-server-response-cache';

createApp({
  name: 'tincoin',
  modules: [ServerResponseCacheModule],
});
```

## Explanation

Module mostly focused on providing high RPS for the server by disabling part of the functionality on ssr and caching non-personalized requests.

:::warning

This type of caching implies no personalization for the individual requests. That means a lot of requests will be served as anonymous pages without most of the ssr preparations for the client. Be sure your app can successfully dehydrate on the client in this case and can load any additional data required to the end user in browser.

:::

### Getting from cache

Conditions when value is returned from cache:

- if there any value in cache
- call of the [`RESPONSE_CACHE_SHOULD_USE_CACHE`](#response_cache_should_use_cache) returns true

### Running full ssr to fill cache

- In case there is no value cache
- In case there is value in cache, but its ttl is outdated. In this case the cached value will still be send to client, but it will be updated in the background.

### Setting to cache

- We have served response
- After response is sent call [`RESPONSE_CACHE_SHOULD_SET_TO_CACHE`](#response_cache_should_set_to_cache) to check if response should be cached

### Options

- `ttl` - time to live of the cache entry. 1 minute by default.
- `maxSize` - max number of entries in cache. 50 entries by default
- `line` - specify the commandLine to execute checking cache. Might be used in case you need some additional data from di that is filled on next lines. By default cache check is executed on `customerStart`

## How to

## Exported tokens

### RESPONSE_CACHE_SHOULD_USE_CACHE

Function that returns flag that current request should be handled by cache.

Default: always true.

### RESPONSE_CACHE_GET_CACHE_KEY

Function that return string that is used as cache key.

Default: request pathname + deviceType (mobile or desktop)

### RESPONSE_CACHE_SHOULD_SET_TO_CACHE

Function that return flag that current response should be cache. As some requests may contain personal info for the client not every response can be cache.

Default: true if there is no cookies for the request

### RESPONSE_CACHE_OPTIONS

Specify [response cache options](#options)

---
_Source: https://npm.io/package/@tramvai/module-server-response-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
