alepha@docs:~/docs/reference/primitives$
cat $cache.md
1 min read

#$cache

#Import

typescript
1import { $cache } from "alepha/cache";

#Overview

Creates a cache primitive for caching with automatic management.

Middleware mode (no handler) — usable in use arrays AND as a store:

ts
 1class UserService { 2  userCache = $cache({ name: "users", ttl: [10, "minutes"] }); 3  4  fetchUser = $pipeline({ 5    use: [this.userCache], 6    handler: async (userId: string) => this.repo.getById(userId), 7  }); 8  9  async invalidateUser(userId: string) {10    await this.userCache.invalidate(userId);11  }12}

Primitive mode (with handler) — standalone callable:

ts
1getUserData = $cache({2  name: "user-data",3  ttl: [10, "minutes"],4  handler: async (userId: string) => {5    return await database.users.findById(userId);6  }7});

#Options

Option Type Required Description
name string No The cache name
handler Object No Function which returns cached data.
key Object No The key generator for the cache
provider InstantiableClass<CacheProvider> | "memory" No The store provider for the cache
ttl DurationLike No The time-to-live for the cache in seconds
disabled boolean No If the cache is disabled.
compress boolean No Enable gzip compression for cached values