Cache Strategy

Learn how Vectra Guard's intelligent caching makes sandboxed commands 10x faster

How Caching Works

Vectra Guard uses intelligent caching to dramatically speed up repeated command executions. When a command runs in the sandbox, its inputs and outputs are cached, allowing subsequent identical commands to return instantly.

Cache Flow

  1. Command is received with inputs and environment
  2. Cache key is generated from command hash + input hash
  3. Cache is checked for matching entry
  4. If found: return cached result instantly (0.25s)
  5. If not found: execute in sandbox, cache result, return (2.5s)

Cache Key Generation

Cache keys are generated from multiple factors to ensure accuracy and prevent stale results.

Cache Key Components
# Cache key includes:
# 1. Command string (exact match required)
# 2. Command arguments
# 3. Working directory
# 4. Environment variables (selected)
# 5. Input file hashes (if any)
# 6. Sandbox image version

# Example cache key components:
Command: "npm install"
Args: ["express"]
CWD: "/project"
Env: {"NODE_ENV": "production"}
Input files: ["package.json" (hash: abc123...)]

# Combined hash: sha256(command + args + cwd + env + files)

Note: Even a small change in command, arguments, or inputs will generate a different cache key and trigger a fresh execution.

Cache Strategies

Choose a caching strategy that matches your workflow and performance needs.

Smart (Default)

Intelligent caching that balances performance and freshness. Caches most commands but invalidates on dependency changes.

sandbox:
  enable_cache: true
  cache_strategy: smart
  cache_ttl: 3600  # 1 hour

Best for: Most development workflows. Automatically invalidates when package files change.

Aggressive

Maximum caching. Caches everything possible with longer TTL. Use when commands are deterministic.

sandbox:
  enable_cache: true
  cache_strategy: aggressive
  cache_ttl: 86400  # 24 hours
  cache_invalidation: manual

Best for: CI/CD pipelines, deterministic builds, testing.

Conservative

Minimal caching. Only caches safe, read-only commands. Shorter TTL.

sandbox:
  enable_cache: true
  cache_strategy: conservative
  cache_ttl: 300  # 5 minutes
  cache_readonly_only: true

Best for: When you need fresh results, dynamic content, or are unsure about command determinism.

Cache Management

Manage your cache with built-in commands for inspection, invalidation, and cleanup.

Cache Management Commands
# View cache statistics
vg cache stats --path .

# Output:
# Cache location: .vectra-guard/cache/
# Total entries: 1,234
# Cache size: 45.2 MB
# Hit rate: 87.3%
# Oldest entry: 2 days ago
# Newest entry: 5 minutes ago

# Clear all cache
vg cache clear --path .

# Clear cache for specific command
vg cache clear --path . --command "npm install"

# Invalidate cache (mark as stale)
vg cache invalidate --path . --pattern "npm:*"

# Prune old entries
vg cache prune --path . --max-age 7d

Performance Impact

Caching provides significant performance improvements for repeated commands.

Without Cache

First run:2.5s
Subsequent runs:2.5s
100 runs:250s

With Cache

First run:2.5s
Subsequent runs:0.25s
100 runs:27.5s
10x faster!