Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move redis adapter to separate npm library #87

Merged
merged 8 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
},
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation has no benefit over

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

cachified({ cache, /* ... */ });

One of the main reasons for an adapter is to set the TTL on the cached item in a way that the cache will clean it up itself without involving cachified.

Proposing to either not create an on the fly adapter (example above) or do the following:

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

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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would vote for the latter option. In real apps this is quite critical in order to not bloat your cache db.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I agree. Let's update the example to the one which sets the ttl 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh that makes sense! Thank you for the help!

I've updated it now to the above example.


function getUserById(userId: number) {
return cachified({
Expand Down Expand Up @@ -225,129 +241,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 'user@example.org';
},
});
```

### 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 'user@example.org';
},
});
```

### 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 'user@example.org';
},
});
```

### 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