fofx / api-cache
Laravel-based API response caching library
Requires
- php: ^8.2
- fofx/helper: ^1.1
- jeremykendall/php-domain-parser: ^6.3
- laravel/framework: ^11.38
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.68
- orchestra/testbench: ^9.9
- phpstan/phpstan: ^2.1
- phpstan/phpstan-mockery: ^2.0
- phpunit/phpunit: ^11.5
- dev-master
- v0.6.0
- v0.5.9
- v0.5.8
- v0.5.7
- v0.5.6
- v0.5.5
- v0.5.4
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.9
- v0.4.8
- v0.4.7
- v0.4.6
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.9
- v0.3.8
- v0.3.7
- v0.3.6
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.9
- v0.2.8
- v0.2.7
- v0.2.6
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
This package is auto-updated.
Last update: 2025-04-10 17:00:08 UTC
README
🚧 Under Construction 🚧
A Laravel-based PHP library for caching API responses. Currently in early development.
Documentation
Please see the docs folder for:
Diagrams
Development Setup
- Clone the repository
- Install dependencies:
composer install
- Copy
.env.example
to.env
- For testing the demo API:
php -S 0.0.0.0:8000 -t public
Features
- API response caching
- Rate limiting with Redis
- Compression support
- Multiple API client support
Rate Limiting with Redis
The package uses Redis for distributed rate limiting, allowing rate limits to be shared across multiple application instances. This ensures consistent rate limiting even when your application is running on multiple servers or processes.
Implementation
Rate limits are stored in Redis using Laravel's cache system. Since the RateLimitService is registered as singleton in this library's service provider, changing the default cache driver would affect all parts of the application. To avoid state cross-contamination between different cache drivers, use named stores:
// Instead of: Config::set('cache.default', 'array'); $arrayService = new RateLimitService(new RateLimiter(app('cache')->driver())); Config::set('cache.default', 'redis'); $redisService = new RateLimitService(new RateLimiter(app('cache')->driver())); // Use: // Create two services with different cache stores $arrayStore = app('cache')->store('array'); $arrayService = new RateLimitService(new RateLimiter($arrayStore)); $redisStore = app('cache')->store('redis'); $redisService = new RateLimitService(new RateLimiter($redisStore)); // Each service maintains its own state $arrayService->incrementAttempts('client1'); // Only affects array store $redisService->incrementAttempts('client1'); // Only affects Redis store // Create another Redis service (simulating a different server/process) $redisService2 = new RateLimitService(new RateLimiter($redisStore)); // Warning: This service shares rate limit state with $redisService
This ensures proper isolation between different cache stores while allowing rate limits to be shared across multiple instances using the same Redis store.
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.
Instead of duplicating table creation logic in each migration file, we use shared helper functions defined in src/functions.php
:
create_responses_table()
: Creates tables for storing API responsescreate_pixabay_images_table()
: Creates tables for storing Pixabay image data
Example migration:
public function up(): void { $schema = Schema::connection($this->getConnection()); create_responses_table($schema, 'api_cache_demo_responses', false); }
License
MIT