Skip to main content

Cache

Kit provides a Laravel-inspired Cache facade for storing and retrieving data with optional TTL (time-to-live) expiration. The cache automatically uses Redis when available, falling back to an in-memory cache when Redis is unavailable.

Overview

The cache system is automatically initialized when your server starts - no manual setup required. It tries to connect to Redis first, and if Redis isn’t available, it seamlessly falls back to an in-memory cache.

Quick Start

Configuration

The cache uses environment variables for configuration. All are optional with sensible defaults.

Example .env

Basic Operations

Storing Items

Retrieving Items

Removing Items

The Remember Pattern

The remember method retrieves an item from the cache, or stores a default value if it doesn’t exist:
This is perfect for expensive operations like database queries or API calls.

Atomic Counters

Increment and decrement numeric values atomically:

Testing

In tests, you can use the in-memory cache implementation directly:

Type Safety

The cache works with any type that implements Serialize and Deserialize:

Redis vs In-Memory

The framework automatically selects the appropriate backend:
  • Redis: When REDIS_URL environment variable is set and Redis is accessible
  • In-Memory: When Redis is not available (development, testing, or Redis failure)

Best Practices

Key Naming

Use consistent, hierarchical key names:

TTL Strategy

Choose TTL based on data volatility:

Cache Invalidation

Invalidate related cache entries when data changes: