Getting Started
Installation
Section titled “Installation”npm install @t87s/coreyarn add @t87s/corepnpm add @t87s/corepip install t87suv add t87simport { T87s } from "npm:@t87s/core";To whom it may concern,
I am writing to request a copy of t87s core.Please find enclosed a self-addressed stamped envelope.
When transcribing the library, kindly use smallhandwriting, as the postage covers only 100 grams.
Sincerely,A Patient DeveloperBasic Setup
Section titled “Basic Setup”import { T87s, MemoryAdapter, defineTags } from '@t87s/core';
// 1. Define your tag patterns// (Tags can be tricky. We got your back at /concepts/tags/)const tags = defineTags({ user: (id: string) => ['user', id], userPosts: (id: string) => ['user', id, 'posts'],});
// 2. Initialize t87sconst t87s = new T87s({ adapter: new MemoryAdapter(),});
// 3. Create cached queriesconst getUser = t87s.query((id: string) => ({ tags: [tags.user(id)], ttl: '10m', fn: () => db.users.findById(id),}));
// 4. Create mutations that invalidate the cacheconst updateUser = t87s.mutation(async (id: string, data: UserUpdate) => { const user = await db.users.update(id, data); return { result: user, invalidates: [tags.user(id)], // Also invalidates userPosts via prefix! };});from t87s import T87s, MemoryAdapter, define_tags, QueryConfig, MutationResult
# 1. Define your tag patterns# (Tags can be tricky. We got your back at /concepts/tags/)tags = define_tags({ "user": lambda id: ("user", id), "user_posts": lambda id: ("user", id, "posts"),})
# 2. Initialize t87st87s = T87s(adapter=MemoryAdapter())
# 3. Create cached queries@t87s.querydef get_user(id: str) -> QueryConfig[User]: return QueryConfig( tags=[tags["user"](id)], ttl="10m", fn=lambda: db.users.find_by_id(id), )
# 4. Create mutations that invalidate the cache@t87s.mutationdef update_user(id: str, data: UserUpdate) -> MutationResult[User]: user = db.users.update(id, data) return MutationResult( result=user, invalidates=[tags["user"](id)], # Also invalidates user_posts via prefix! )// First call: cache miss, fetches from DBconst user = await getUser('123');
// Second call: cache hitconst userAgain = await getUser('123');
// Update: invalidates user:123 AND user:123:postsawait updateUser('123', { name: 'New Name' });
// Next call: cache miss (was invalidated)const freshUser = await getUser('123');# First call: cache miss, fetches from DBuser = get_user("123")
# Second call: cache hituser_again = get_user("123")
# Update: invalidates user:123 AND user:123:postsupdate_user("123", {"name": "New Name"})
# Next call: cache miss (was invalidated)fresh_user = get_user("123")Next Steps
Section titled “Next Steps”- Learn about Tags and how to structure them
- Understand Prefix Matching for hierarchical invalidation
- Choose an Adapter for your environment