# kitkache

> A basic, configurable client side cache

Latest version **0.4.0** (published 2023-01-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install kitkache
pnpm add kitkache
yarn add kitkache
bun add kitkache
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2023-01-15 |
| First published | 2022-12-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Arjen Bloemsma |
| Maintainers | arjenbloemsma |
| Keywords | basic, browser, cache, client, conditional, configurable, front-end, local, simple, storage, util |

## Links

- npm: https://www.npmjs.com/package/kitkache
- Repository: https://github.com/arjenbloemsma/kitkache
- Homepage: https://github.com/arjenbloemsma/kitkache#readme
- Issues: https://github.com/arjenbloemsma/kitkache/issues
- npm.io page: https://npm.io/package/kitkache

## 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.4.0 (latest) — 2023-01-15
- 0.3.4 — 2023-01-15
- 0.3.3 — 2022-12-29
- 0.3.1 — 2022-12-26
- 0.3.0 — 2022-12-26

## README

# Kitkache

Kitkache is a basic client side cache for in the browser.

Kitkache will wrap the value to be stored in an object with a timestamp. A config object defines how long the object in the [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) will remain valid and when that period has passed it will retrieve a new value by calling the provided `objectLoader` callback.

Besides the period (in seconds) after which an object becomes invalid the config
can also hold a function `storeCondition` that returns a boolean, so that the
value will only be stored if the function evaluates to `true`. The value
retrieved by the `objectLoader` callback is passed as an argument to
`storeCondition` function.

## Install

Using npm:
```shell
$ npm install kitkache
```

Using yarn:
```shell
$ yarn add kitkache
```

Using pnpm:
```shell
$ pnpm add kitkache
```

## Usage

Store value of type `YourCustomType` for 2 hours

```typescript
import {kitKache} from 'kitkache'

await kitKache<YourCustomType>(
  'key',
  {expireAfterHours: 2},
  () => {
    // logic to retrieve value
    return value
  },
)
```

Store `string` value for 5 minutes, unless the value equals 'tokyo'.

```typescript
import {kitKache} from 'kitkache'

await kitKache<string>(
  'key',
  {
    expireAfterMins: 5,
    storeCondition: value => value !== 'tokyo',
  },
  () => {
    // logic to retrieve value
    return value
  },
)
```

## The config object

The configuration to be passed to the kitKache function is simple:

```typescript
{
  expireAfterSecs?: number
  expireAfterMins?: number
  expireAfterHours?: number
  storeCondition?: (value: T) => boolean
}
```

### Setting the expiration period
You can define the period after which the cache becomes invalid (and thus a new value will be retrieved) in either seconds, minutes or hours. kitKache will always choose the most specific value. So if seconds, minutes and hours have been provided, only the value for seconds will be used. If both minutes and hours have been provided, the value for minutes will be used as the expiration period.

### Providing a condition
You can also provide a condition wether a value should be stored in cache or not. The condition is a simple function that takes in a value (kitKache will automatically provide the value that is to be stored) and returns a boolean.

If the function return false, then the value will not be stored in the cache.

Imagine that the `objectLoader` retrieves an object from an external system, but that object should not be stored if the property 'fresh' in that object is set to 'true', then the condition should be something like the following:

```typescript
(value: externalObject) => !value.fresh
```

## The object loader
The `objectLoader` is an async function that returns a value. In it simplest form it could be for instance:

```typescript
() => Promise.resolve('foo')
```

In more realistic scenarios it will most likely be a function that retrieves a value from an external system.

[Project site](https://kitkache.bloemium.io)

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