SDK Documentation
Welcome to the Cachee.ai SDK documentation. Cachee.ai is an AI-powered enterprise caching platform that automatically optimizes your application's cache performance, reducing latency and infrastructure costs by up to 42%.
Installation
Install the Cachee.ai SDK using your language's package manager:
# Install via pip
pip install cachee-sdk
# Or with Poetry
poetry add cachee-sdk
# Or with pipenv
pipenv install cachee-sdk
# Install via npm
npm install @cachee/sdk
# Or with Yarn
yarn add @cachee/sdk
# Or with pnpm
pnpm add @cachee/sdk
# Install via go get
go get github.com/cachee/cachee-go
# Import in your code
import "github.com/cachee/cachee-go"
<!-- Maven -->
<dependency>
<groupId>com.cachee</groupId>
<artifactId>cachee-sdk</artifactId>
<version>1.0.0</version>
</dependency>
// Gradle
implementation 'com.cachee:cachee-sdk:1.0.0'
Quick Start
Get up and running with Cachee.ai in under 5 minutes:
from cachee_sdk import cachee
import os
# Initialize Cachee with your API key
cachee.init(api_key=os.getenv('CACHEE_API_KEY'))
# Add caching to any function with a decorator
@cachee.cache(
key="user:{user_id}",
ttl_strategy="adaptive" # AI handles TTL automatically
)
def get_user(user_id):
# Your expensive operation (database query, API call, etc.)
return db.query("SELECT * FROM users WHERE id = ?", user_id)
# Use it like normal - Cachee handles the rest
user = get_user(12345)
print(f"User: {user['name']}")
# That's it! You now have AI-powered caching 🚀
import { cachee } from '@cachee/sdk';
// Initialize Cachee with your API key
cachee.init({
apiKey: process.env.CACHEE_API_KEY
});
// Add caching to any function
const getUser = cachee.cache(
async (userId) => {
// Your expensive operation
return await db.query('SELECT * FROM users WHERE id = ?', userId);
},
{
key: 'user:{userId}',
ttlStrategy: 'adaptive' // AI handles TTL automatically
}
);
// Use it like normal - Cachee handles the rest
const user = await getUser(12345);
console.log(`User: ${user.name}`);
// That's it! You now have AI-powered caching 🚀
package main
import (
"context"
"fmt"
"os"
"github.com/cachee/cachee-go"
)
func main() {
// Initialize Cachee with your API key
c := cachee.New(cachee.Config{
APIKey: os.Getenv("CACHEE_API_KEY"),
})
// Create a cached function
getUser := c.Cache(
"user:{userId}",
cachee.Options{TTLStrategy: "adaptive"},
func(ctx context.Context, userId int) (User, error) {
// Your expensive operation
return db.QueryUser(userId)
},
)
// Use it like normal - Cachee handles the rest
user, err := getUser(context.Background(), 12345)
if err != nil {
panic(err)
}
fmt.Printf("User: %s\n", user.Name)
// That's it! You now have AI-powered caching 🚀
}
import com.cachee.cachee.Cachee;
import com.cachee.cachee.annotations.Cache;
public class Application {
public static void main(String[] args) {
// Initialize Cachee with your API key
Cachee.init(System.getenv("CACHEE_API_KEY"));
}
// Add caching with annotation
@Cache(
key = "user:{userId}",
ttlStrategy = "adaptive" // AI handles TTL automatically
)
public User getUser(int userId) {
// Your expensive operation
return db.query("SELECT * FROM users WHERE id = ?", userId);
}
// Use it like normal - Cachee handles the rest
User user = getUser(12345);
System.out.println("User: " + user.getName());
// That's it! You now have AI-powered caching 🚀
}
- Monitoring access patterns for your function
- Using AI to predict when data will be requested
- Automatically adjusting TTL based on usage
- Pre-fetching related data
- Tracking performance metrics
Authentication
Cachee.ai uses API keys for authentication. You can pass your API key in multiple ways:
# Method 1: Environment variable (recommended)
import os
from cachee_sdk import cachee
cachee.init(api_key=os.getenv('CACHEE_API_KEY'))
# Method 2: Direct initialization
cachee.init(api_key='YOUR_API_KEY')
# Method 3: Configuration file
cachee.init(config_file='~/.cachee/config.yaml')
# Method 4: AWS Secrets Manager
import boto3
secrets = boto3.client('secretsmanager')
api_key = secrets.get_secret_value(SecretId='cachee-api-key')['SecretString']
cachee.init(api_key=api_key)
// Method 1: Environment variable (recommended)
import { cachee } from '@cachee/sdk';
cachee.init({
apiKey: process.env.CACHEE_API_KEY
});
// Method 2: Direct initialization
cachee.init({
apiKey: 'YOUR_API_KEY'
});
// Method 3: Configuration file
cachee.init({
configFile: '~/.cachee/config.json'
});
// Method 4: AWS Secrets Manager
import { SecretsManager } from 'aws-sdk';
const secrets = new SecretsManager();
const { SecretString } = await secrets.getSecretValue({
SecretId: 'cachee-api-key'
}).promise();
cachee.init({ apiKey: SecretString });
package main
import (
"os"
"github.com/cachee/cachee-go"
)
// Method 1: Environment variable (recommended)
c := cachee.New(cachee.Config{
APIKey: os.Getenv("CACHEE_API_KEY"),
})
// Method 2: Direct initialization
c := cachee.New(cachee.Config{
APIKey: "YOUR_API_KEY",
})
// Method 3: Configuration file
c, err := cachee.NewFromConfig("~/.cachee/config.yaml")
// Method 4: AWS Secrets Manager
import "github.com/aws/aws-sdk-go/service/secretsmanager"
svc := secretsmanager.New(session.New())
secret, _ := svc.GetSecretValue(&secretsmanager.GetSecretValueInput{
SecretId: aws.String("cachee-api-key"),
})
c := cachee.New(cachee.Config{
APIKey: *secret.SecretString,
})
import com.cachee.cachee.Cachee;
// Method 1: Environment variable (recommended)
Cachee.init(System.getenv("CACHEE_API_KEY"));
// Method 2: Direct initialization
Cachee.init("YOUR_API_KEY");
// Method 3: Configuration file
Cachee.initFromConfig("~/.cachee/config.properties");
// Method 4: AWS Secrets Manager
AWSSecretsManager client = AWSSecretsManagerClientBuilder.standard().build();
GetSecretValueRequest request = new GetSecretValueRequest()
.withSecretId("cachee-api-key");
String apiKey = client.getSecretValue(request).getSecretString();
Cachee.init(apiKey);
- Never commit API keys to version control
- Use environment variables or secret managers in production
- Rotate API keys regularly (every 90 days recommended)
- Use separate keys for development, staging, and production
- Restrict key permissions to only what's needed
Caching Basics
Understand the fundamentals of how Cachee.ai caches your data:
Cache Keys
Cache keys uniquely identify cached data. Use dynamic parameters in your keys:
# Simple key
@cachee.cache(key="users_list")
def get_all_users():
return db.query("SELECT * FROM users")
# Dynamic key with parameter
@cachee.cache(key="user:{user_id}")
def get_user(user_id):
return db.query("SELECT * FROM users WHERE id = ?", user_id)
# Composite key with multiple parameters
@cachee.cache(key="products:{category}:{page}")
def get_products(category, page):
return db.query("SELECT * FROM products WHERE category = ? LIMIT ? OFFSET ?",
category, 20, page * 20)
# Complex key with custom serialization
@cachee.cache(key="search:{query}:{filters_hash}")
def search_products(query, filters):
filters_hash = hashlib.md5(json.dumps(filters).encode()).hexdigest()
return db.search(query, filters)
TTL (Time To Live)
TTL determines how long data stays in cache. Cachee.ai offers multiple strategies:
| Strategy | Description | Best For |
|---|---|---|
adaptive |
AI automatically adjusts TTL based on access patterns | Most use cases (default recommendation) |
fixed |
Set a specific TTL in seconds | Data with predictable freshness requirements |
stale-while-revalidate |
Serve stale data while refreshing in background | High-traffic endpoints where stale data is acceptable |
time-based |
Different TTL based on time of day | Data with predictable update schedules |
no-ttl |
Cache indefinitely until manually invalidated | Immutable data (user IDs, product SKUs) |
@cachee.cache() Decorator
Complete reference for the main caching decorator:
@cachee.cache(
key: str, # Cache key with parameter interpolation
ttl_strategy: str = "adaptive", # TTL strategy: adaptive, fixed, stale-while-revalidate, time-based, no-ttl
ttl: int = None, # TTL in seconds (required if ttl_strategy="fixed")
namespace: str = None, # Logical grouping for related cache entries
version: str = "1", # Cache version for schema changes
warm_cache: bool = False, # Pre-compute during off-peak hours
fallback_on_error: bool = False, # Serve stale data if function raises error
compress: bool = True, # Compress large payloads (>10KB)
serialize: str = "json", # Serialization: json, pickle, msgpack
tags: List[str] = None, # Tags for bulk invalidation
conditions: Dict = None, # Conditional caching rules
ai_enabled: bool = True, # Enable AI features
metrics: bool = True, # Track performance metrics
local_cache: bool = True, # Enable in-memory L1 cache
distributed: bool = True, # Use distributed cache (Redis/Memcached)
)
Parameter Reference
| Parameter | Type | Description |
|---|---|---|
key REQUIRED |
string | Cache key template. Use {param} for dynamic values. |
ttl_strategy |
string | TTL strategy. Default: "adaptive" |
ttl |
int | TTL in seconds (required if ttl_strategy="fixed") |
namespace |
string | Logical grouping for cache entries. Example: "users" |
version |
string | Cache version for schema changes. Default: "1" |
warm_cache |
bool | Pre-compute during off-peak hours. Default: False |
fallback_on_error |
bool | Serve stale data if function fails. Default: False |
compress |
bool | Compress payloads >10KB. Default: True |
serialize |
string | Serialization format: "json", "pickle", "msgpack". Default: "json" |
tags |
List[str] | Tags for bulk invalidation. Example: ["users", "auth"] |
ai_enabled |
bool | Enable AI optimization. Default: True |
local_cache |
bool | Enable in-memory L1 cache. Default: True |
Advanced Examples
# Example 1: API calls with fallback
@cachee.cache(
key="github:user:{username}",
ttl_strategy="fixed",
ttl=300, # 5 minutes
fallback_on_error=True, # Serve stale if GitHub is down
tags=["external_api", "github"]
)
def get_github_user(username):
response = httpx.get(f"https://api.github.com/users/{username}")
return response.json()
# Example 2: Expensive aggregation with cache warming
@cachee.cache(
key="analytics:dashboard:{org_id}",
ttl_strategy="adaptive",
warm_cache=True, # Pre-compute at 2 AM daily
compress=True,
namespace="analytics"
)
def get_dashboard_analytics(org_id):
# Expensive multi-table aggregation
return db.run_analytics_query(org_id)
# Example 3: User-specific data with namespace
@cachee.cache(
key="user:{user_id}:preferences",
namespace="users",
version="2", # Increment when schema changes
tags=["users", "preferences"],
serialize="json"
)
def get_user_preferences(user_id):
return db.query("SELECT * FROM user_preferences WHERE user_id = ?", user_id)
# Example 4: Conditional caching based on request size
@cachee.cache(
key="search:{query}",
conditions={
"cache_if": lambda result: len(result) > 0, # Only cache non-empty results
"skip_if": lambda query: len(query) < 3 # Don't cache short queries
}
)
def search(query):
return db.full_text_search(query)
AI Features
Cachee.ai's AI engine continuously learns from your application's behavior to optimize caching automatically:
- Data Collection: Tracks every cache hit, miss, and access pattern
- Pattern Recognition: Identifies temporal, user, and behavioral patterns
- Prediction: Uses proprietary ML models to predict future access
- Optimization: Automatically adjusts TTL, pre-fetches data, and warms cache
- Feedback Loop: Continuously learns from outcomes to improve accuracy
All of this happens transparently. You don't need to configure anything. Just use the @cachee.cache() decorator and the AI does the rest.
Cache Warming
Pre-compute expensive operations during off-peak hours to ensure cache is always hot:
# Enable automatic cache warming
@cachee.cache(
key="analytics:{org_id}",
warm_cache=True # AI will determine best time to warm
)
def get_analytics(org_id):
return expensive_aggregation(org_id)
# Manual cache warming (useful for migrations)
cachee.warm_cache(
function=get_analytics,
params=[{"org_id": id} for id in active_org_ids],
schedule="0 2 * * *" # Every day at 2 AM
)
# Warm cache for specific keys
cachee.warm_keys([
"analytics:org_123",
"analytics:org_456",
"analytics:org_789"
])
# Warm entire namespace
cachee.warm_namespace("analytics", concurrency=10)
Monitoring & Observability
Track cache performance and AI predictions in real-time:
# Get real-time metrics
metrics = cachee.get_metrics(
namespace="users",
time_range="last_24h"
)
print(f"Cache Hit Rate: {metrics['hit_rate']}%")
print(f"Avg Latency: {metrics['latency_p50']}ms")
print(f"Cost Savings: ${metrics['cost_savings']}")
# Get AI predictions
predictions = cachee.get_ai_predictions(
key="user:{user_id}"
)
print(f"Predicted next access: {predictions['next_access_time']}")
print(f"Recommended TTL: {predictions['recommended_ttl']}s")
print(f"Confidence: {predictions['confidence']}%")
# Export metrics to Prometheus
from prometheus_client import start_http_server
cachee.enable_prometheus_exporter(port=9090)
# Send metrics to CloudWatch
cachee.enable_cloudwatch_metrics(
namespace="Cachee",
region="us-west-2"
)
Best Practices
Follow these guidelines to get the most out of Cachee.ai:
- Start with adaptive TTL: Let the AI learn first, then optimize if needed
- Use descriptive cache keys:
user:{user_id}notu:{id} - Namespace related data: Group users, products, etc. for easier management
- Enable compression for large payloads: >10KB should be compressed
- Use tags for bulk invalidation: Tag related cache entries
- Monitor metrics regularly: Check dashboard weekly to identify optimization opportunities
- Version your cache: Increment version when data schema changes
- Don't cache user-specific sensitive data without encryption: Use
encrypt=Trueparameter - Don't use dynamic keys without parameters:
key=f"user:{user_id}"should bekey="user:{user_id}" - Don't cache frequently changing data: If data changes >1x per second, caching may not help
- Don't ignore cache invalidation: Stale data can cause bugs
- Don't over-cache: Not everything needs caching. Focus on expensive operations
- Don't use pickle serialization for untrusted data: Stick with JSON for external APIs
Performance Optimization Tips
# 1. Use local L1 cache for hot data
@cachee.cache(
key="config:app_settings",
local_cache=True, # In-memory cache
local_ttl=60, # 60 seconds in memory
distributed_ttl=300 # 5 minutes in Redis
)
def get_app_config():
return db.query("SELECT * FROM config")
# 2. Batch operations when possible
@cachee.cache_batch(
key="user:{user_id}",
batch_size=100
)
def get_users_batch(user_ids):
return db.query("SELECT * FROM users WHERE id IN (?)", user_ids)
# 3. Use stale-while-revalidate for high-traffic endpoints
@cachee.cache(
key="homepage:feed",
ttl_strategy="stale-while-revalidate",
ttl=300, # Serve stale after 5 minutes
grace_period=3600 # But keep serving stale for up to 1 hour
)
def get_homepage_feed():
return expensive_feed_query()
# 4. Conditional caching for variable-cost operations
@cachee.cache(
key="search:{query}",
conditions={
"cache_if": lambda result: len(result) > 10, # Only cache if many results
"ttl_if": lambda result: 3600 if len(result) > 100 else 300 # Dynamic TTL
}
)
def search(query):
return search_engine.query(query)
AWS Integration
Deploy Cachee.ai in your AWS VPC with full control:
- Managed SaaS: Fastest setup (5 minutes), Cachee manages infrastructure
- AWS Marketplace: One-click CloudFormation deployment (15 minutes)
- Self-Hosted: Full control, deploy via Docker/ECS/Terraform (30 minutes)
See our Onboarding Guide for detailed deployment instructions.
# Deploy via AWS Marketplace CloudFormation
# Contact support for CloudFormation templates
aws cloudformation create-stack \
--stack-name cachee-ai-production \
--template-url <provided-by-support> \
--parameters \
ParameterKey=VPC,ParameterValue=vpc-12345 \
ParameterKey=Subnets,ParameterValue=subnet-abc\\,subnet-def \
ParameterKey=CacheSize,ParameterValue=100 \
ParameterKey=InstanceType,ParameterValue=<recommended-instance-type> \
--capabilities CAPABILITY_IAM
# Or use our CLI
cachee-cli deploy aws \
--region us-west-2 \
--vpc vpc-12345 \
--subnets subnet-abc,subnet-def \
--cache-size 100GB
Managed Cloud — Provisioning Walkthrough
The fastest path from zero to a working cache. Cachee provisions and runs the infrastructure — you get an API key and point the SDK at it.
Step 1: Create your account
Go to cachee.ai/start. Enter your email and phone number. No credit card required for the free tier (1M requests/month).
Step 2: Copy your API key
After verifying your phone via OTP, your API key is generated and displayed. Copy it to your environment:
export CACHEE_API_KEY="ck_live_xxxxxxxxxxxxxxxxxxxx"
Step 3: Install the SDK
# Node.js
npm install @cachee/sdk
# Python
pip install cachee
Step 4: Initialize and make your first call
import { CacheeClient } from '@cachee/sdk'
const cache = new CacheeClient({
apiKey: process.env.CACHEE_API_KEY,
region: 'auto', // routes to nearest edge
fallback: 'local', // in-memory LRU if server unreachable
timeout: 2000 // ms
})
// Your first cache operation
await cache.set('user:1234', { name: 'Alice', plan: 'scale' }, { ttl: 300 })
const user = await cache.get('user:1234')
console.log(user) // { name: 'Alice', plan: 'scale' }
Step 5: Verify in the dashboard
Within 60 seconds of your first call, the Cachee dashboard shows your hit rate, latency percentiles, and request volume. You're live.
Core Operations Reference
// GET — retrieve a cached value
const val = await cache.get('key')
// SET — store with optional TTL (seconds)
await cache.set('key', value, { ttl: 300 })
// DEL — remove a key
await cache.del('key')
// EXISTS — check if a key exists
const exists = await cache.exists('key') // boolean
// MSET — batch set multiple keys
await cache.mset([
{ key: 'a', value: 1, ttl: 60 },
{ key: 'b', value: 2, ttl: 60 },
{ key: 'c', value: 3, ttl: 60 }
])
Connection Options
const cache = new CacheeClient({
apiKey: process.env.CACHEE_API_KEY,
// Region: 'auto' (nearest), or explicit: 'us-east-1', 'eu-west-1', 'ap-southeast-1'
region: 'auto',
// Fallback: 'local' (in-memory LRU), 'none' (throw on disconnect), or 'stale' (serve expired)
fallback: 'local',
// Timeout in ms for each operation
timeout: 2000,
// Max keys in local fallback LRU (default: 10000)
fallbackMaxKeys: 10000,
// Retry: exponential backoff on transient failures
retries: 3,
retryDelay: 200 // ms, doubles each retry
})
Sidecar Container
Deploy a Cachee container alongside your application. It speaks the Redis protocol on port 6379 — your existing Redis client code works without any changes. Cache calls happen over localhost with zero network hop.
Environment Variables
| Variable | Required | Default | Description |
|---|---|---|---|
CACHEE_API_KEY | Yes | — | Your Cachee API key |
CACHEE_MODE | No | sidecar | Must be "sidecar" |
RESP_PORT | No | 6379 | Redis protocol port |
PORT | No | 3000 | HTTP health/metrics port |
L1_MAX_KEYS | No | 1000000 | Max keys in L1 cache |
L1_DEFAULT_TTL | No | 600 | Default TTL in seconds |
CONTROL_PLANE_URL | No | https://api.cachee.ai | Control plane endpoint |
Docker Compose
services:
your-app:
image: your-app:latest
environment:
# Point your existing Redis client here:
REDIS_HOST: cachee-sidecar
REDIS_PORT: "6379"
depends_on:
- cachee-sidecar
cachee-sidecar:
image: cacheeai/sidecar:latest
environment:
CACHEE_API_KEY: ${CACHEE_API_KEY}
CACHEE_MODE: sidecar
RESP_PORT: "6379"
L1_MAX_KEYS: "1000000"
L1_DEFAULT_TTL: "600"
deploy:
resources:
limits:
memory: 256M
cpus: '0.5'
healthcheck:
test: ["CMD", "redis-cli", "-p", "6379", "PING"]
interval: 10s
timeout: 3s
retries: 3
Kubernetes Pod Spec
apiVersion: v1
kind: Pod
metadata:
name: app-with-cachee
spec:
containers:
- name: your-app
image: your-app:latest
env:
- name: REDIS_URL
value: "redis://localhost:6379"
- name: cachee-sidecar
image: cacheeai/sidecar:latest
env:
- name: CACHEE_API_KEY
valueFrom:
secretKeyRef:
name: cachee-secrets
key: api-key
- name: CACHEE_MODE
value: "sidecar"
- name: RESP_PORT
value: "6379"
ports:
- containerPort: 6379
name: redis
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "500m"
livenessProbe:
tcpSocket:
port: 6379
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
tcpSocket:
port: 6379
initialDelaySeconds: 3
periodSeconds: 5
Supported Redis Commands
The sidecar speaks the Redis RESP protocol. These commands are fully supported:
Any unsupported command returns a clear error response — never a silent hang or timeout.
Self-Hosted Deployment
Run the Cachee agent on your own infrastructure. All cache data stays on your hardware — the control plane receives only operational metrics (hit rate, latency, memory utilization). No keys or values ever leave your environment.
Step 1: Generate a connection token
From the Cachee dashboard, go to Settings → Self-Hosted → Generate Token. The token is single-use and expires in 24 hours.
Step 2: Run the agent
# Pull and run the Cachee agent
docker run -d \
--name cachee-agent \
-e CACHEE_CONNECT_TOKEN="ct_live_xxxxxxxxxxxx" \
-e CACHEE_REGION="us-east-1" \
-p 6379:6379 \
-p 3000:3000 \
cacheeai/agent:latest
# The agent will:
# 1. Authenticate with the control plane using your token
# 2. Download your configuration and AI models
# 3. Start serving Redis protocol on port 6379
# 4. Begin sending operational metrics to the control plane
Step 3: Verify the connection
# Test Redis protocol
redis-cli -h localhost -p 6379 PING
# → PONG
# Test a SET/GET cycle
redis-cli -h localhost -p 6379 SET hello world
# → OK
redis-cli -h localhost -p 6379 GET hello
# → "world"
# Check agent health
curl http://localhost:3000/health
# → {"status":"ok","mode":"self-hosted","connected":true}
What stays where
- → All cache keys and values
- → All TTL and expiration data
- → The Cachee agent binary
- → Local AI model weights (downloaded once)
- → Hit rate and miss rate
- → Latency percentiles (p50, p95, p99)
- → Memory utilization percentage
- → Request count per interval
The control plane uses these metrics to push AI-optimized eviction and pre-warming decisions back to your agent. No raw cache data is ever transmitted.
Air-gapped environments
The agent supports restricted-egress networks. Configure an outbound proxy for control plane sync:
docker run -d \
--name cachee-agent \
-e CACHEE_CONNECT_TOKEN="ct_live_xxxxxxxxxxxx" \
-e HTTPS_PROXY="http://your-proxy:8080" \
-e CONTROL_PLANE_URL="https://api.cachee.ai" \
-p 6379:6379 \
cacheeai/agent:latest