Skip to content

Commit 45afb15

Browse files
authored
feat: move adapters to separate npm library (#87)
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.
1 parent b4831fe commit 45afb15

File tree

9 files changed

+25
-988
lines changed

9 files changed

+25
-988
lines changed

README.md

Lines changed: 22 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,26 @@ npm install @epic-web/cachified
5252

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

5757
/* lru cache is not part of this package but a simple non-persistent cache */
58-
const lru = new LRUCache<string, CacheEntry>({ max: 1000 });
58+
const lruInstance = new LRUCache<string, CacheEntry>({ max: 1000 });
59+
60+
const lru: Cache = {
61+
set(key, value) {
62+
const ttl = totalTtl(value?.metadata);
63+
return lruInstance.set(key, value, {
64+
ttl: ttl === Infinity ? undefined : ttl,
65+
start: value?.metadata?.createdTime,
66+
});
67+
},
68+
get(key) {
69+
return lruInstance.get(key);
70+
},
71+
delete(key) {
72+
return lruInstance.delete(key);
73+
},
74+
};
5975

6076
function getUserById(userId: number) {
6177
return cachified({
@@ -218,129 +234,11 @@ interface CachifiedOptions<Value> {
218234

219235
## Adapters
220236

221-
There are some build-in adapters for common caches, using them makes sure
222-
the used caches cleanup outdated values themselves.
223-
224-
### Adapter for [lru-cache](https://www.npmjs.com/package/lru-cache)
225-
226-
<!-- lru-adapter -->
227-
228-
```ts
229-
import { LRUCache } from 'lru-cache';
230-
import { cachified, lruCacheAdapter, CacheEntry } from '@epic-web/cachified';
231-
232-
const lru = new LRUCache<string, CacheEntry>({ max: 1000 });
233-
const cache = lruCacheAdapter(lru);
234-
235-
await cachified({
236-
cache,
237-
key: 'user-1',
238-
getFreshValue() {
239-
240-
},
241-
});
242-
```
243-
244-
### Adapter for [redis](https://www.npmjs.com/package/redis)
245-
246-
<!-- redis-adapter -->
247-
248-
```ts
249-
import { createClient } from 'redis';
250-
import { cachified, redisCacheAdapter } from '@epic-web/cachified';
251-
252-
const redis = createClient({
253-
/* ...opts */
254-
});
255-
const cache = redisCacheAdapter(redis);
256-
257-
await cachified({
258-
cache,
259-
key: 'user-1',
260-
getFreshValue() {
261-
262-
},
263-
});
264-
```
265-
266-
### Adapter for [redis@3](https://www.npmjs.com/package/redis/v/3.1.2)
267-
268-
<!-- redis-3-adapter -->
269-
270-
```ts
271-
import { createClient } from 'redis';
272-
import { cachified, redis3CacheAdapter } from '@epic-web/cachified';
273-
274-
const redis = createClient({
275-
/* ...opts */
276-
});
277-
const cache = redis3CacheAdapter(redis);
278-
279-
const data = await cachified({
280-
cache,
281-
key: 'user-1',
282-
getFreshValue() {
283-
284-
},
285-
});
286-
```
287-
288-
### Adapter for [Cloudflare KV](https://developers.cloudflare.com/kv/)
289-
290-
For additional information or to report issues, please visit the [cachified-adapter-cloudflare-kv repository](https://github.com/AdiRishi/cachified-adapter-cloudflare-kv).
291-
292-
```ts
293-
import { cachified, Cache } from '@epic-web/cachified';
294-
import { cloudflareKvCacheAdapter } from 'cachified-adapter-cloudflare-kv';
295-
296-
export interface Env {
297-
KV: KVNamespace;
298-
CACHIFIED_KV_CACHE: Cache;
299-
}
300-
301-
export async function getUserById(
302-
userId: number,
303-
env: Env,
304-
): Promise<Record<string, unknown>> {
305-
return cachified({
306-
key: `user-${userId}`,
307-
cache: env.CACHIFIED_KV_CACHE,
308-
async getFreshValue() {
309-
const response = await fetch(
310-
`https://jsonplaceholder.typicode.com/users/${userId}`,
311-
);
312-
return response.json();
313-
},
314-
ttl: 60_000, // 1 minute
315-
staleWhileRevalidate: 300_000, // 5 minutes
316-
});
317-
}
318-
319-
export default {
320-
async fetch(
321-
request: Request,
322-
env: Env,
323-
ctx: ExecutionContext,
324-
): Promise<Response> {
325-
env.CACHIFIED_KV_CACHE = cloudflareKvCacheAdapter({
326-
kv: env.KV,
327-
keyPrefix: 'mycache', // optional
328-
name: 'CloudflareKV', // optional
329-
});
330-
const userId = Math.floor(Math.random() * 10) + 1;
331-
const user = await getUserById(userId, env);
332-
return new Response(JSON.stringify(user), {
333-
headers: {
334-
'Content-Type': 'application/json',
335-
},
336-
});
337-
},
338-
};
339-
```
340-
341-
### Adapter for [redis-json](https://www.npmjs.com/package/@redis/json)
237+
There are some adapters available for common caches. Using them makes sure the used caches cleanup outdated values themselves.
342238

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

345243
## Advanced Usage
346244

extractExamples.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)