Skip to main content

Intelligent caching proxy for Rentcast API with cost management and rate limiting

Project description

RentCache - Intelligent Rentcast API Proxy

๐Ÿ  Reduce your Rentcast API costs by 70-90% with intelligent caching, rate limiting, and usage management.

RentCache is a production-ready FastAPI proxy server that sits between your applications and the Rentcast API, providing intelligent caching, cost optimization, and usage analytics. Perfect for real estate applications that need frequent property data access without breaking the budget.

Python 3.10+ FastAPI SQLAlchemy License: MIT Repository

๐Ÿ’ฐ Cost Savings

Dramatic API Cost Reduction:

  • ๐ŸŽฏ 70-90% cost savings through intelligent caching
  • ๐Ÿ“Š Cache hit ratios typically 80-95% after warm-up
  • ๐Ÿ’ธ Property records: $1.00 โ†’ $0.10 per request (90% savings)
  • ๐Ÿ’ธ Value estimates: $2.00 โ†’ $0.20 per request (90% savings)
  • ๐Ÿ’ธ Market data: $5.00 โ†’ $0.50 per request (90% savings)

Real-world example: A property management company reduced their monthly Rentcast bill from $2,500 to $300 using RentCache.

โœจ Features

๐Ÿš€ Performance & Caching

  • Intelligent Multi-Level Caching: SQLite for persistence + optional Redis for speed
  • Configurable TTL: Different cache durations for different endpoint types
  • Stale-While-Revalidate: Serve cached data during upstream failures
  • Cache Warming: Pre-populate cache for better performance
  • Soft Deletion: Mark entries invalid instead of deleting for analytics

๐Ÿ’ฐ Cost Management

  • Usage Tracking: Monitor API costs and savings from cache hits
  • Rate Limiting: Prevent expensive API overuse with per-endpoint limits
  • Cost Estimation: Track estimated costs for each endpoint type
  • Budget Alerts: Monitor spending against configured limits

๐Ÿ” Security & Access Control

  • API Key Management: Create, update, and revoke access keys
  • Role-Based Access: Different limits per API key
  • Rate Limiting: Global and per-endpoint request limits
  • CORS Support: Configurable cross-origin resource sharing
  • Request Validation: Comprehensive input validation with Pydantic

๐Ÿ“Š Analytics & Monitoring

  • Real-time Metrics: Cache hit ratios, response times, error rates
  • Usage Statistics: Track usage patterns and popular endpoints
  • Health Checks: Monitor system and dependency health
  • Structured Logging: JSON logs for easy parsing and analysis

๐Ÿ”ง Developer Experience

  • OpenAPI Docs: Auto-generated API documentation
  • CLI Administration: Command-line tools for management
  • Type Safety: Full type annotations with Pydantic models
  • Comprehensive Tests: Unit and integration test coverage

๐Ÿš€ Quick Start

Docker Deployment (Recommended)

# Clone the repository
git clone https://git.supported.systems/MCP/rentcache.git
cd rentcache

# Start with Docker (includes reverse proxy)
make setup

# Or manually:
docker network create caddy 2>/dev/null || true
docker compose up --build -d

Local Development

# Install dependencies
uv sync

# Start development server
make local-dev

# Or manually:
uv run rentcache server --reload

Basic Usage

  1. Create an API key:

    # Using Docker
    make create-key NAME=my_app KEY=your_rentcast_api_key
    
    # Or locally
    uv run rentcache create-key my_app your_rentcast_api_key
    
  2. Make API calls:

    # Make API call
    curl -H "Authorization: Bearer your_rentcast_api_key" \
         "http://localhost:8000/api/v1/properties?city=Austin&state=TX"
    
  3. Monitor performance:

    # View metrics
    curl "http://localhost:8000/metrics"
    
    # Health check
    curl "http://localhost:8000/health"
    

๐Ÿ“š Documentation

๐Ÿ“– API Documentation

Core Endpoints

All Rentcast API endpoints are proxied with intelligent caching:

๐Ÿ˜๏ธ Property Records

GET /api/v1/properties
GET /api/v1/properties/{property_id}

Cache TTL: 24 hours (expensive endpoints)

๐Ÿ’ฒ Value & Rent Estimates

GET /api/v1/estimates/value
GET /api/v1/estimates/rent
POST /api/v1/estimates/value/bulk
POST /api/v1/estimates/rent/bulk

Cache TTL: 1 hour (dynamic pricing)

๐Ÿ  Listings

GET /api/v1/listings/sale
GET /api/v1/listings/rental
GET /api/v1/listings/{listing_id}

Cache TTL: 30 minutes (frequently updated)

๐Ÿ“ˆ Market Data

GET /api/v1/markets/stats
GET /api/v1/comparables

Cache TTL: 2 hours (market statistics)

Cache Control Parameters

All endpoints support these parameters:

  • force_refresh=true: Bypass cache and fetch fresh data
  • ttl_override=3600: Override default TTL (in seconds)

Response Headers

Every response includes cache information:

X-Cache-Hit: true|false
X-Response-Time-MS: 45.2
X-Estimated-Cost: 2.0  (only on cache misses)

๐Ÿ› ๏ธ Administration

CLI Commands

# Server management
rentcache server --host 0.0.0.0 --port 8000 --reload

# API key management
rentcache create-key <name> <rentcast_key> [options]
rentcache list-keys
rentcache update-key <name> [options]
rentcache delete-key <name>

# Cache management
rentcache clear-cache [--endpoint=properties] [--older-than=24]

# Monitoring
rentcache stats [--endpoint=properties] [--days=7]
rentcache health

API Key Management

# Create key with custom limits
rentcache create-key production_app YOUR_KEY \
  --daily-limit 5000 \
  --monthly-limit 100000 \
  --expires 2024-12-31

# Update existing key
rentcache update-key production_app --daily-limit 10000 --active

# List all keys with usage stats
rentcache list-keys

Cache Management

# Clear specific endpoint cache
rentcache clear-cache --endpoint properties

# Clear old cache entries
rentcache clear-cache --older-than 24

# Clear all cache (careful!)
rentcache clear-cache

HTTP Admin Endpoints

# API key management
POST /admin/api-keys          # Create API key
GET  /admin/api-keys          # List API keys
PUT  /admin/api-keys/{id}     # Update API key
DELETE /admin/api-keys/{id}   # Delete API key

# Cache management  
POST /admin/cache/clear       # Clear cache entries
GET  /admin/cache/stats       # Cache statistics

# System monitoring
GET /health                   # Health check
GET /metrics                  # System metrics

โš™๏ธ Configuration

Environment Variables

# Server
HOST=0.0.0.0
PORT=8000
DEBUG=false

# Database
DATABASE_URL=sqlite+aiosqlite:///./rentcache.db
DATABASE_ECHO=false

# Redis (optional)
REDIS_URL=redis://localhost:6379
REDIS_ENABLED=false

# Rentcast API
RENTCAST_BASE_URL=https://api.rentcast.io
RENTCAST_TIMEOUT=30
RENTCAST_MAX_RETRIES=3

# Cache settings
DEFAULT_CACHE_TTL=3600
EXPENSIVE_ENDPOINTS_TTL=86400
ENABLE_STALE_WHILE_REVALIDATE=true

# Rate limiting
ENABLE_RATE_LIMITING=true
GLOBAL_RATE_LIMIT=1000/hour
PER_ENDPOINT_RATE_LIMIT=100/minute

# Security
ALLOWED_HOSTS=*
CORS_ORIGINS=*

# Logging
LOG_LEVEL=INFO
LOG_FORMAT=json

Configuration File

Create a .env file in your project root:

# Basic configuration
DEBUG=true
LOG_LEVEL=DEBUG

# Database
DATABASE_URL=sqlite+aiosqlite:///./rentcache.db

# Optional Redis for better performance
# REDIS_URL=redis://localhost:6379
# REDIS_ENABLED=true

# Custom cache settings
DEFAULT_CACHE_TTL=3600
EXPENSIVE_ENDPOINTS_TTL=86400

# Rate limiting
GLOBAL_RATE_LIMIT=2000/hour
PER_ENDPOINT_RATE_LIMIT=200/minute

๐Ÿ—๏ธ Architecture

System Components

graph TD
    A[Client Applications] --> B[FastAPI Server]
    B --> C[Authentication Layer]
    C --> D[Rate Limiting]
    D --> E[Cache Manager]
    E --> F{Cache Hit?}
    F -->|Yes| G[Return Cached Data]
    F -->|No| H[Rentcast API]
    H --> I[Store in Cache]
    I --> G
    
    E --> J[(SQLite/PostgreSQL)]
    E --> K[(Redis - Optional)]
    
    B --> L[Usage Analytics]
    L --> J
    
    B --> M[Health Monitoring]
    B --> N[Metrics Collection]

Cache Strategy

  1. L1 Cache (Redis): Fast in-memory cache for frequently accessed data
  2. L2 Cache (SQLite/PostgreSQL): Persistent cache with analytics and soft deletion
  3. Cache Keys: MD5 hash of endpoint + method + parameters
  4. TTL Management: Different expiration times based on data volatility
  5. Stale-While-Revalidate: Serve expired data during upstream failures

Rate Limiting Strategy

  1. Global Limits: Per API key across all endpoints
  2. Per-Endpoint Limits: Specific limits for expensive operations
  3. Exponential Backoff: Automatically slow down aggressive clients
  4. Usage Tracking: Monitor and alert on approaching limits

๐Ÿงช Testing

Run Tests

# Run all tests with coverage
uv run pytest

# Run specific test categories
uv run pytest -m unit
uv run pytest -m integration
uv run pytest -m api

# Run with coverage report
uv run pytest --cov=src/rentcache --cov-report=html

Test Structure

tests/
โ”œโ”€โ”€ conftest.py           # Test configuration
โ”œโ”€โ”€ test_models.py        # Model tests
โ”œโ”€โ”€ test_cache.py         # Cache system tests  
โ”œโ”€โ”€ test_server.py        # API endpoint tests
โ”œโ”€โ”€ test_cli.py           # CLI command tests
โ””โ”€โ”€ test_integration.py   # End-to-end tests

๐Ÿ“Š Monitoring & Analytics

Key Metrics

  • Cache Hit Ratio: Percentage of requests served from cache
  • Response Times: Average response time by endpoint
  • Error Rates: 4xx/5xx error percentages
  • Cost Tracking: Estimated Rentcast API costs and savings
  • Usage Patterns: Popular endpoints and request volumes

Health Checks

GET /health

Response includes:

  • Database connectivity
  • Cache backend status
  • Active API keys count
  • Recent error rates
  • System uptime

Metrics Endpoint

GET /metrics

Provides detailed system metrics including:

  • Request volumes and cache performance
  • Per-endpoint statistics
  • Cost analysis and savings
  • System resource utilization

๐Ÿณ Docker Deployment

Quick Start with Make

# Complete setup with reverse proxy
make setup

# Development mode with hot reload
make dev

# Production mode
make prod

# View logs
make logs

# Health check
make health

# Management commands
make create-key NAME=my_app KEY=your_rentcast_key
make list-keys
make stats

Manual Docker Compose

# Create external network (if not exists)
docker network create caddy

# Development with hot reload
docker compose up --build -d

# Production mode
MODE=production DEBUG=false docker compose up --build -d

# With Redis for better performance
docker compose --profile redis up --build -d

Production Features

  • ๐Ÿ”„ Multi-stage Docker builds for optimized images
  • ๐ŸŒ Caddy reverse proxy with automatic HTTPS
  • ๐Ÿ“Š Health checks and graceful shutdown
  • ๐Ÿ”’ Non-root user for security
  • ๐Ÿ“ฆ Efficient caching with multi-layer cache mounts
  • ๐Ÿš€ Hot reload in development mode
  • ๐Ÿ“ˆ Production monitoring with metrics and logging

๐Ÿ“ˆ Performance Tips

Optimization Strategies

  1. Cache Warming: Pre-populate cache for popular endpoints
  2. Bulk Operations: Use bulk endpoints when available
  3. Connection Pooling: Configure appropriate database connection pools
  4. Response Compression: Enable gzip compression for large responses
  5. CDN Integration: Use CDN for static content and common API responses

Monitoring Performance

# Check cache hit ratios
rentcache stats --days 7

# Monitor response times
curl -s http://localhost:8000/metrics | jq '.avg_response_time_ms'

# Check system health
rentcache health

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass (uv run pytest)
  6. Run code formatting (uv run black src tests)
  7. Submit a pull request

Development Setup

# Install development dependencies
uv sync --all-extras

# Install pre-commit hooks
pre-commit install

# Run tests
uv run pytest

# Format code
uv run black src tests
uv run ruff check src tests --fix

๐Ÿ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ”— Links

๐Ÿ™ Acknowledgments

  • Rentcast for providing the real estate data API
  • FastAPI for the excellent web framework
  • SQLAlchemy for powerful ORM capabilities
  • Pydantic for data validation and serialization

Built with โค๏ธ for the real estate technology community

Reduce your API costs today - every cache hit saves you money!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rentcache-0.1.1.tar.gz (25.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rentcache-0.1.1-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file rentcache-0.1.1.tar.gz.

File metadata

  • Download URL: rentcache-0.1.1.tar.gz
  • Upload date:
  • Size: 25.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for rentcache-0.1.1.tar.gz
Algorithm Hash digest
SHA256 98f8882a479d0631f8c2dae7ed15c6691bbd6c309a5d429f838c01d3f766a7bf
MD5 075ef768376baa7347346f9ff58b9d75
BLAKE2b-256 8856e4ec29e4f164fd09913d5f3adeb917b5a355c9ea921bd3778e3544fb2a2b

See more details on using hashes here.

File details

Details for the file rentcache-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: rentcache-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for rentcache-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dd2c1059ba76660d8d408de4bfaa25de23d08764e408dafca0e3d98002b1b02e
MD5 2d0b949ad76e655d66d79f7cd8a594e1
BLAKE2b-256 561e6814bdba15b29ca621fae7793375f02e361a4702b840d9403c662287aa17

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page