# @prairielearn/cache

Latest version **2.1.19** (published 2026-10-06) · 0 weekly downloads

## Install

```sh
npm install @prairielearn/cache
pnpm add @prairielearn/cache
yarn add @prairielearn/cache
bun add @prairielearn/cache
```

## Health

**Score 65/100 (B)** — status: active.

Positive: esm support; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 2.1.19 |
| Published | 2026-10-06 |
| First published | 2024-02-09 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 66.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 534 |
| Maintainers | nwalters512, mwest1066 |

## Links

- npm: https://www.npmjs.com/package/@prairielearn/cache
- Repository: https://github.com/PrairieLearn/PrairieLearn
- npm.io page: https://npm.io/package/@prairielearn/cache

## Dependencies (4)

- [ioredis](https://npm.io/package/ioredis.md) ^6.0.0
- [lru-cache](https://npm.io/package/lru-cache.md) ^11.5.3
- [@prairielearn/logger](https://npm.io/package/@prairielearn/logger.md) ^4.0.2
- [@prairielearn/sentry](https://npm.io/package/@prairielearn/sentry.md) ^6.0.0

## Recent versions

- 2.1.19 (latest) — 2026-10-06
- 2.1.18 — 2026-10-01
- 2.1.17 — 2026-09-18
- 2.1.16 — 2026-08-17
- 2.1.15 — 2026-08-11
- 2.1.14 — 2026-07-10
- 2.1.13 — 2026-06-05
- 2.1.12 — 2026-06-01
- 2.1.11 — 2026-05-16
- 2.1.10 — 2026-05-07
- 2.1.9 — 2026-04-29
- 2.1.8 — 2026-03-04
- 2.1.7 — 2026-01-31
- 2.1.6 — 2026-01-16
- 2.1.5 — 2026-01-15
- … 34 more at https://npm.io/package/@prairielearn/cache/versions

## README

# `@prairielearn/cache`

Utilities to help connect to and store information in a cache. This package _does not_ load configurations directly. Instead, configs should be passed in when the package is initialized upon loading the application. Then, the package can be used in the throughout the application to interact with the cache.

## Usage

First, you will need to initialize the library with the cache type that you are intending to use, a prefix that will be the start of all of your cache keys, and optionally, a Redis server URL if that is the cache type being used:

```ts
import { cache } from '@prairielearn/cache';

await cache.init({
  type: 'redis',
  keyPrefix: 'my-app:',
  redisUrl: 'redis://localhost:6379/',
});
```

Alternatively, you can create new instances of the `Cache` class instead of using the global `cache` object:

```ts
import { Cache } from '@prairielearn/cache';

const myCache = new Cache();

await myCache.init({
  type: 'redis',
  keyPrefix: 'my-app:',
  redisUrl: 'redis://localhost:6379/',
});
```

After initializing, you can use `set`, `get`, `del`, `reset` or `close` functions to interact with the cache. Note that `set`, `get`, and `del` have required arguments. Calling `set` will require the intended KEY, VALUE, and length of time to store the data (in milliseconds). Calling `get` or `del` will require the KEY for the intended result.

The following example will store `foo: bar` for 10 minutes:

```ts
await cache.set('foo', 'bar', 600000);
```

The following example will use the key `foo` to retrieve the value `bar`:

```ts
await cache.get('foo');
// returns bar
```

The following example will use the key `foo` to delete the key value pair `foo: bar`:

```ts
await cache.del('foo');
```

Using `reset()` will clear the currently stored data in the cache. Using `close()` will disable the currently used cache and, if using Redis, close the connection.

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