Skip to content

Commit

Permalink
feat: move adapters to separate npm library (#87)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this removes all built-in adapters in favor of separate packages. You'll need to use those packages for adapters going forward. Please see the updated docs for more info.
  • Loading branch information
mannyv123 authored Feb 9, 2024
1 parent b4831fe commit 45afb15
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 988 deletions.
146 changes: 22 additions & 124 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,26 @@ npm install @epic-web/cachified

```ts
import { LRUCache } from 'lru-cache';
import { cachified, CacheEntry } from '@epic-web/cachified';
import { cachified, CacheEntry, Cache, totalTtl } from '@epic-web/cachified';

/* lru cache is not part of this package but a simple non-persistent cache */
const lru = new LRUCache<string, CacheEntry>({ max: 1000 });
const lruInstance = new LRUCache<string, CacheEntry>({ max: 1000 });

const lru: Cache = {
set(key, value) {
const ttl = totalTtl(value?.metadata);
return lruInstance.set(key, value, {
ttl: ttl === Infinity ? undefined : ttl,
start: value?.metadata?.createdTime,
});
},
get(key) {
return lruInstance.get(key);
},
delete(key) {
return lruInstance.delete(key);
},
};

function getUserById(userId: number) {
return cachified({
Expand Down Expand Up @@ -218,129 +234,11 @@ interface CachifiedOptions<Value> {

## Adapters

There are some build-in adapters for common caches, using them makes sure
the used caches cleanup outdated values themselves.

### Adapter for [lru-cache](https://www.npmjs.com/package/lru-cache)

<!-- lru-adapter -->

```ts
import { LRUCache } from 'lru-cache';
import { cachified, lruCacheAdapter, CacheEntry } from '@epic-web/cachified';

const lru = new LRUCache<string, CacheEntry>({ max: 1000 });
const cache = lruCacheAdapter(lru);

await cachified({
cache,
key: 'user-1',
getFreshValue() {
return '[email protected]';
},
});
```

### Adapter for [redis](https://www.npmjs.com/package/redis)

<!-- redis-adapter -->

```ts
import { createClient } from 'redis';
import { cachified, redisCacheAdapter } from '@epic-web/cachified';

const redis = createClient({
/* ...opts */
});
const cache = redisCacheAdapter(redis);

await cachified({
cache,
key: 'user-1',
getFreshValue() {
return '[email protected]';
},
});
```

### Adapter for [redis@3](https://www.npmjs.com/package/redis/v/3.1.2)

<!-- redis-3-adapter -->

```ts
import { createClient } from 'redis';
import { cachified, redis3CacheAdapter } from '@epic-web/cachified';

const redis = createClient({
/* ...opts */
});
const cache = redis3CacheAdapter(redis);

const data = await cachified({
cache,
key: 'user-1',
getFreshValue() {
return '[email protected]';
},
});
```

### Adapter for [Cloudflare KV](https://developers.cloudflare.com/kv/)

For additional information or to report issues, please visit the [cachified-adapter-cloudflare-kv repository](https://github.com/AdiRishi/cachified-adapter-cloudflare-kv).

```ts
import { cachified, Cache } from '@epic-web/cachified';
import { cloudflareKvCacheAdapter } from 'cachified-adapter-cloudflare-kv';

export interface Env {
KV: KVNamespace;
CACHIFIED_KV_CACHE: Cache;
}

export async function getUserById(
userId: number,
env: Env,
): Promise<Record<string, unknown>> {
return cachified({
key: `user-${userId}`,
cache: env.CACHIFIED_KV_CACHE,
async getFreshValue() {
const response = await fetch(
`https://jsonplaceholder.typicode.com/users/${userId}`,
);
return response.json();
},
ttl: 60_000, // 1 minute
staleWhileRevalidate: 300_000, // 5 minutes
});
}

export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext,
): Promise<Response> {
env.CACHIFIED_KV_CACHE = cloudflareKvCacheAdapter({
kv: env.KV,
keyPrefix: 'mycache', // optional
name: 'CloudflareKV', // optional
});
const userId = Math.floor(Math.random() * 10) + 1;
const user = await getUserById(userId, env);
return new Response(JSON.stringify(user), {
headers: {
'Content-Type': 'application/json',
},
});
},
};
```

### Adapter for [redis-json](https://www.npmjs.com/package/@redis/json)
There are some adapters available for common caches. Using them makes sure the used caches cleanup outdated values themselves.

[cachified-redis-json-adapter](https://www.npmjs.com/package/cachified-redis-json-adapter)
- Adapter for [redis](https://www.npmjs.com/package/redis) : [cachified-redis-adapter](https://www.npmjs.com/package/cachified-redis-adapter)
- Adapter for [redis-json](https://www.npmjs.com/package/@redis/json) : [cachified-redis-json-adapter](https://www.npmjs.com/package/cachified-redis-json-adapter)
- Adapter for [Cloudflare KV](https://developers.cloudflare.com/kv/) : [cachified-adapter-cloudflare-kv repository](https://github.com/AdiRishi/cachified-adapter-cloudflare-kv)

## Advanced Usage

Expand Down
68 changes: 0 additions & 68 deletions extractExamples.js

This file was deleted.

Loading

0 comments on commit 45afb15

Please sign in to comment.