Hyrex provides a built-in key-value store for sharing state between tasks and workers. All methods are static and can be called from anywhere in your application.

Basic Usage

from hyrex import HyrexKV

# Set a value
HyrexKV.set("user:123:status", "active")

# Get a value
status = HyrexKV.get("user:123:status")  # Returns "active"

# Get with default value
status = HyrexKV.get("missing-key", default="unknown")  # Returns "unknown"

# Delete a value
HyrexKV.delete("user:123:status")

Limitations

  • String only: Both keys and values must be strings
  • 1MB max: Values are limited to 1MB (UTF-8 encoded)
  • No expiration: Values persist until explicitly deleted
  • No atomic operations: No increment, decrement, or compare-and-swap

Common Patterns

Task Coordination

@hy.task
def process_batch(batch_id: str):
    # Mark as processing
    HyrexKV.set(f"batch:{batch_id}:status", "processing")
    
    # Do work...
    
    # Mark complete
    HyrexKV.set(f"batch:{batch_id}:status", "complete")

Simple Caching

@hy.task
def get_user_data(user_id: str):
    # Check cache
    cached = HyrexKV.get(f"cache:user:{user_id}")
    if cached:
        return json.loads(cached)
    
    # Fetch from DB
    user = fetch_from_db(user_id)
    
    # Cache result
    HyrexKV.set(f"cache:user:{user_id}", json.dumps(user))
    return user

Progress Tracking

@hy.task
def long_task(job_id: str, total: int):
    for i in range(total):
        # Update progress
        HyrexKV.set(f"progress:{job_id}", str(i + 1))
        process_item(i)
    
    HyrexKV.delete(f"progress:{job_id}")