Part of the alepha package. Import from alepha/cache.
npm install alepha
Provides high-performance caching capabilities for Alepha applications with configurable TTL and multiple storage backends.
The cache module enables declarative caching through the $cache primitive, allowing you to cache method results,
API responses, or computed values with automatic invalidation and type safety. It supports both in-memory and
persistent storage backends for different performance and durability requirements.
Primitives are functions that define and configure various aspects of your application. They follow the convention of starting with $ and return configured primitive instances.
For more details, see the Primitives documentation.
Creates a cache primitive for high-performance data caching with automatic management.
Provides a caching layer that improves application performance by storing frequently accessed data in memory or external stores like Redis, with support for both function result caching and manual cache operations.
Key Features
Storage Backends
1class DataService { 2 // Function result caching 3 getUserData = $cache({ 4 name: "user-data", 5 ttl: [10, "minutes"], 6 handler: async (userId: string) => { 7 return await database.users.findById(userId); 8 } 9 });10 11 // Manual cache operations12 sessionCache = $cache<UserSession>({13 name: "sessions",14 ttl: [1, "hour"]15 });16 17 async storeSession(id: string, session: UserSession) {18 await this.sessionCache.set(id, session);19 }20 21 async invalidateUserSessions(userId: string) {22 await this.sessionCache.invalidate(`user:${userId}:*`);23 }24}