Skip to content
Open
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
47 changes: 23 additions & 24 deletions docs/product/libraries/ts/cache/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,42 +71,41 @@ bun install @unkey/cache
</CodeGroup>

<CodeGroup>
```ts Hello World

```ts Hello World
import { createCache, DefaultStatefulContext, Namespace } from "@unkey/cache";
import { MemoryStore } from "@unkey/cache/stores";

/\*\*

- Define the type of your data,
- or perhaps generate the types from your database
\*/
type User = {
/**
* Define the type of your data,
* or perhaps generate the types from your database
*/
type User = {
id: string;
email: string;
};

/\*\*
};

- In serverless you'd get this from the request handler
- See /docs/libraries/ts/cache/overview#context
\*/
const ctx = new DefaultStatefulContext();
/**
* In serverless you'd get this from the request handler
* See /docs/libraries/ts/cache/overview#context
*/
const ctx = new DefaultStatefulContext();

const memory = new MemoryStore({ persistentMap: new Map() });

const cache = createCache({
user: new Namespace<User>(ctx, {
stores: [memory],
fresh: 60_000, // Data is fresh for 60 seconds
stale: 300_000, // Data is stale for 300 seconds
})
user: new Namespace<User>(ctx, {
stores: [memory],
fresh: 60_000, // Data is fresh for 60 seconds
stale: 300_000, // Data is stale for 300 seconds
}),
});

await cache.user.set("userId", { id: "userId", email: "user@email.com" });
const user = await cache.user.get("userId")
console.log(user)
const user = await cache.user.get("userId");
console.log(user);
```

````
```ts Tiered Caches
import { createCache, DefaultStatefulContext, Namespace } from "@unkey/cache";
import { CloudflareStore, MemoryStore } from "@unkey/cache/stores";
Expand Down Expand Up @@ -145,7 +144,7 @@ const cache = createCache({
stores: [memory, cloudflare],
fresh: 60_000, // Data is fresh for 60 seconds
stale: 300_000, // Data is stale for 300 seconds
});
}),
});

async function main() {
Expand All @@ -157,7 +156,7 @@ async function main() {
}

main();
````
```

```ts Multiple Namespaces
import { createCache, DefaultStatefulContext, Namespace } from "@unkey/cache";
Expand Down
Loading