fofx/api-cache

Laravel-based API response caching library


README

A Laravel-based PHP library for caching API responses from various services (OpenAI, DataForSEO, Pixabay, YouTube, and more). Provides intelligent caching, rate limiting, compression, and response management through a unified interface.

Requirements

  • PHP 8.3+
  • Laravel 11.38+
  • Redis (for distributed rate limiting)

Development Setup

  1. Clone the repository
  2. Install dependencies:
composer install
  1. Copy .env.example to .env
  2. For testing the demo API:
php -S 0.0.0.0:8000 -t public

Documentation

Please see the docs folder for:

Diagrams

Features

  • API response caching
  • Rate limiting with Redis
  • Compression support
  • Multiple API client support

Caching Control

The sendCachedRequest() method respects the caching settings of the API client. You can control caching behavior using:

// Create a new client instance
$client = new ScraperApiClient();

// Disable caching for a specific request
$client->setUseCache(false);

// Check current caching status
$isCachingEnabled = $client->getUseCache();
echo 'Is caching enabled: ' . ($isCachingEnabled ? 'true' : 'false') . PHP_EOL;

$response = $client->scrape('https://httpbin.org/headers');
echo format_api_response($response, true);

// Re-enable caching
$client->setUseCache(true);

Rate Limiting

Redis-based distributed rate limiting is implemented to ensure consitent rate limiting across multiple application instances.

See Rate Limiting Documentation for details.

Database Migrations

This library takes an unconventional approach to database migrations in order to follow the DRY principle and simplify maintenance across multiple clients, while maintaining consistency between tables.

For more details, see Database Migrations Documentation.

Laravel Integration

To use this library in a full Laravel project, see the Laravel Integration Guide for step-by-step setup instructions including installation, configuration, and usage examples.

Usage

Getting Started

API Clients

DataForSEO API

Utilities

License

MIT