# apollo-server-caching

> [![npm version](https://badge.fury.io/js/apollo-server-caching.svg)](https://badge.fury.io/js/apollo-server-caching) [![Build Status](https://circleci.com/gh/apollographql/apollo-server/tree/main.svg?style=svg)](https://circleci.com/gh/apollographql/apoll

Latest version **3.3.0** (published 2021-11-05) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install apollo-server-caching
pnpm add apollo-server-caching
yarn add apollo-server-caching
bun add apollo-server-caching
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 3.3.0 |
| Published | 2021-11-05 |
| First published | 2018-06-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12.0 |
| Dependencies | 1 |
| Unpacked size | 22.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13952 |
| Author | Apollo |
| Maintainers | apollo-bot |

## Links

- npm: https://www.npmjs.com/package/apollo-server-caching
- Repository: https://github.com/apollographql/apollo-server
- Homepage: https://github.com/apollographql/apollo-server#readme
- Issues: https://github.com/apollographql/apollo-server/issues
- npm.io page: https://npm.io/package/apollo-server-caching

## Dependencies (1)

- [lru-cache](https://npm.io/package/lru-cache.md) ^6.0.0

## Recent versions

- 3.3.0 (latest) — 2021-11-05
- 3.2.0-alpha.0 (alpha) — 2021-10-09
- 3.1.0 (next) — 2021-08-23
- 3.0.0-rc.0 (rc) — 2021-07-01
- 3.0.0-preview.1 (preview) — 2021-06-16
- 0.6.1-unified2.0 (unified) — 2021-04-22
- 0.300.0-alpha.3 (next-v3) — 2020-08-12
- 3.2.0 — 2021-10-11
- 3.1.0-alpha.0 — 2021-08-20
- 3.0.1 — 2021-07-16
- 3.0.0 — 2021-07-07
- 0.300.0-preview.0 — 2021-06-01
- 0.300.0-alpha.4 — 2021-05-06
- 0.7.0 — 2021-04-30
- 0.7.0-alpha.0 — 2021-04-28
- … 41 more at https://npm.io/package/apollo-server-caching/versions

## README

# apollo-server-caching

[![npm version](https://badge.fury.io/js/apollo-server-caching.svg)](https://badge.fury.io/js/apollo-server-caching)
[![Build Status](https://circleci.com/gh/apollographql/apollo-server/tree/main.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-server)

## Implementing your own Cache

Internally, Apollo Server uses the `KeyValueCache` interface to provide a caching store for the Data Sources. An in-memory LRU cache is used by default, and we provide connectors for [Memcached](../apollo-server-cache-memcached)/[Redis](../apollo-server-cache-redis) backends.

Built with extensibility in mind, you can also implement your own cache to use with Apollo Server, in a way that best suits your application needs. It needs to implement the following interface that can be exported from `apollo-server-caching`:

```typescript
export interface KeyValueCache {
  get(key: string): Promise<string | undefined>;
  set(key: string, value: string, options?: { ttl?: number }): Promise<void>;
}
```

> The `ttl` value for the `set` method's `options` is specified in __seconds__.

## Testing cache implementations

### Test helpers

`apollo-server-caching` exports a function that you can run within a test suite to validate your implementation. It throws on failure. If you want to test expiration, then mock out `Date` and `setTimeout` (probably with `@sinonjs/fake-timers`) and pass a `tick` can be called to advance the fake time. (If you don't pass `tick`, it won't test expiration.) Other than that, it has no dependencies and can work in any test system and shouldn't require any particular build configuration to use from jest. Here's an example of how to use it with jest:

```typescript
// ../__tests__/YourKeyValueCache.test.ts

import YourKeyValueCache from '../src/YourKeyValueCache';
import { runKeyValueCacheTests } from 'apollo-server-caching';
import FakeTimers from '@sinonjs/fake-timers';

describe('YourKeyValueCache', () => {
  it('run apollo-server-caching test suite', async () => {
    const cache = new YourKeyValueCache();
    const clock = FakeTimers.install();
    try {
      await runKeyValueCacheTests(cache, (ms: number) => clock.tick(ms));
    } finally {
      clock.uninstall();
      await cache.close();
    }
  });
});
```

For more details, consult the [source for `apollo-server-caching`](./src/testsuite.ts).

### Running tests

Run tests with `jest --verbose`

---
_Source: https://npm.io/package/apollo-server-caching · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
