alepha@docs:~/docs/packages/alepha/cache$
cat core.md
2 min read
Last commit:

#Alepha - Cache

#Installation

Part of the alepha package. Import from alepha/cache.

npm install alepha

#Overview

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.

#API Reference

#Primitives

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.

#$cache()

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

  • Automatic function result caching based on input parameters
  • Multiple storage backends (in-memory, Redis, custom providers)
  • Intelligent serialization for JSON, strings, and binary data
  • Configurable TTL with automatic expiration
  • Pattern-based cache invalidation with wildcard support
  • Environment controls to enable/disable caching

Storage Backends

  • Memory: Fast in-memory cache (default for development)
  • Redis: Distributed cache for production environments
  • Custom providers: Implement your own storage backend
ts
 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}
On This Page
No headings found...
ready
mainTypeScript
UTF-8packages_alepha_cache_core.md