New: K8sGPT Auto Remediation!

Back to Documentation

Caching

Learn about K8sGPT's caching mechanisms and how to configure them

Overview

K8sGPT implements caching to improve performance and reduce API calls to AI providers. This page explains the caching mechanisms and how to configure them for optimal performance.

Cache Types

In-Memory Cache

The default caching mechanism stores results in memory for quick access:

# Enable in-memory caching
k8sgpt analyze --cache=true

# Configure cache duration (in minutes)
k8sgpt analyze --cache-duration=30

Persistent Cache

Store cache results on disk for persistence across restarts:

# Enable persistent caching
k8sgpt analyze --cache=true --cache-type=persistent

# Specify cache directory
k8sgpt analyze --cache-dir=/path/to/cache

Redis Cache

Use Redis for distributed caching in multi-node environments:

# Enable Redis caching
k8sgpt analyze --cache=true --cache-type=redis

# Configure Redis connection
k8sgpt analyze --redis-url=redis://localhost:6379
k8sgpt analyze --redis-password=your-password

Cache Configuration

Environment Variables

# Enable caching
export K8SGPT_CACHE=true

# Set cache duration (minutes)
export K8SGPT_CACHE_DURATION=30

# Configure cache type
export K8SGPT_CACHE_TYPE=persistent

# Set cache directory
export K8SGPT_CACHE_DIR=/path/to/cache

# Redis configuration
export K8SGPT_REDIS_URL=redis://localhost:6379
export K8SGPT_REDIS_PASSWORD=your-password

Kubernetes Configuration

apiVersion: v1
kind: ConfigMap
metadata:
  name: k8sgpt-cache-config
data:
  K8SGPT_CACHE: "true"
  K8SGPT_CACHE_TYPE: "persistent"
  K8SGPT_CACHE_DURATION: "30"
  K8SGPT_CACHE_DIR: "/cache"

Cache Management

Cache Operations

# Clear the cache
k8sgpt cache clear

# View cache statistics
k8sgpt cache stats

# List cached items
k8sgpt cache list

Cache Invalidation

The cache is automatically invalidated when:

  • Cache duration expires
  • Kubernetes resources change
  • Analysis parameters change
  • Manual cache clear is performed

Performance Considerations

Optimize caching for your use case:

  • Set appropriate cache duration based on cluster change frequency
  • Use persistent cache for production environments
  • Consider Redis for high-availability setups
  • Monitor cache hit rates and adjust settings accordingly
  • Regularly clean up old cache entries

Troubleshooting

Common caching issues and solutions:

  • Check cache permissions and storage space
  • Verify Redis connectivity for distributed caching
  • Monitor cache performance metrics
  • Clear cache if stale results are observed
  • Review cache configuration for optimal settings