# remember-cache

> Small browser cache helpers inspired by Laravel Cache::remember

Latest version **0.3.0** (published 2026-06-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install remember-cache
pnpm add remember-cache
yarn add remember-cache
bun add remember-cache
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2026-06-29 |
| First published | 2020-11-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 60.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Skrupel |
| Maintainers | skrupel |
| Keywords | cache, remember, laravel, javascript |

## Links

- npm: https://www.npmjs.com/package/remember-cache
- Repository: https://github.com/skrupelgit/cache-remember.js
- Homepage: https://github.com/skrupelgit/cache-remember.js#readme
- Issues: https://github.com/skrupelgit/cache-remember.js/issues
- npm.io page: https://npm.io/package/remember-cache

## Dependencies (1)

- [localit](https://npm.io/package/localit.md) ^6.1.0

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 0.3.0 (latest) — 2026-06-29
- 0.2.0 — 2021-04-25
- 0.1.0 — 2020-12-03
- 0.0.4 — 2020-12-01
- 0.0.3 — 2020-12-01
- 0.0.2 — 2020-11-24
- 0.0.1 — 2020-11-24

## README

# remember-cache

Small browser cache helpers inspired by Laravel's `Cache::remember()`.

`remember-cache` stores values in `localStorage` through [`localit`](https://www.npmjs.com/package/localit). It is intended for browser apps and test environments that provide `localStorage`.

## Installation

```bash
npm i remember-cache
```

## API

```js
import cache, { createCache } from "remember-cache";
```

### Basic storage

Use `get`, `set`, `has`, and `forget` when you want direct cache access.

```js
cache.set("current-user", user, { seconds: 300 });

cache.get("current-user");
cache.get("current-user", null);
cache.has("current-user");
cache.forget("current-user");
```

### `remember(key, seconds, handler, options?)`

Returns the cached value for `key` when it exists. Otherwise, it resolves `handler`, stores the result, and returns it.

`handler` can be a plain value, a function, or a promise.

```js
const user = await cache.remember("current-user", 60, async () => {
    const response = await fetch("/api/me");
    return response.json();
});
```

Pass `0` as the lifetime to store the value without an expiration date.

### `refresh(key, handler, options?)`

Ignores the previous cached value, resolves `handler`, stores the new value, notifies subscribers, and returns it.

```js
await cache.refresh("current-user", fetchUser, { seconds: 300 });
```

### `rememberMany(entries, seconds, options?)`

Resolves several `remember` calls in parallel and returns an object with the same keys.

```js
const data = await cache.rememberMany({
    user: fetchUser,
    settings: fetchSettings,
}, 60);
```

### `autoUpdate(key, handler, options?)`

Returns the previous cached value immediately when one exists, while updating the cache in the background.

If there is no cached value yet, it returns a promise for the current handler result.

```js
const firstResult = await cache.autoUpdate("news", fetchNews);

const staleResult = cache.autoUpdate("news", fetchLatestNews);
```

Use `onUpdate` and `onError` to connect the refresh to a reactive store, ref, signal, or state setter without depending on a specific framework.

```js
const state = { value: null };

state.value = await cache.autoUpdate("news", fetchNews, {
    staleTime: 30,
    maxAge: 300,
    onUpdate(value) {
        state.value = value;
    },
    onError(error) {
        console.error(error);
    },
});
```

`staleTime` skips the background refresh while the cached value is still fresh. `maxAge` sets the cache expiration. Both values are in seconds.

### `subscribe(key, callback, options?)`

Subscribes to cache changes made through `remember-cache` methods. It returns an unsubscribe function.

```js
const unsubscribe = cache.subscribe("news", value => {
    console.log(value);
}, { family: "cache-autoupdate" });

unsubscribe();
```

### `resource(key, handler, options?)`

Creates a small framework-agnostic state object for UI code.

```js
const user = cache.resource("current-user", fetchUser);

user.value;
user.loading;
user.error;

user.subscribe(resource => {
    render(resource.value);
});

await user.refresh();
```

### `autoClear(key, handler, options?)`

Stores the value like `remember`, but keeps only the most recent entries in the `cache-autoclear` family.

```js
cache.config.autoClearEntries = 20;

const page = await cache.autoClear("products-page-1", async () => {
    const response = await fetch("/api/products?page=1");
    return response.json();
});
```

By default, `autoClear` keeps 10 entries and each entry expires after `cache.config.autoClearMaxTime` seconds.

## Families

Most methods accept a custom `family` option. Families let you group entries and clear them together.

```js
await cache.remember("profile", 60, fetchProfile, { family: "users" });
cache.clearFamily("users");
```

The built-in cache families are:

- `cache-remember`
- `cache-autoupdate`
- `cache-autoclear`

## Custom cache instances

Use `createCache` when you want a different default family or storage.

```js
const sessionCache = createCache({
    family: "session",
    storage: sessionStorage,
});

sessionCache.set("token-preview", preview);
```

## Helpers

```js
cache.clearFamily("cache-remember");
cache.getFamilyKeys("cache-autoclear");
cache.getDomainExpirationDates("cache-autoclear");
```

## Notes

This package depends on `localStorage` by default, so server-side rendering environments need to call it only in the browser or provide a compatible storage implementation.

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