Getting Started¶
This guide will help you set up Async Cache PHP and start caching your asynchronous operations.
Prerequisites¶
Ensure you have installed the package via Composer:
Basic Setup¶
To start using the library, you need to create an instance of AsyncCacheManager. The recommended way is to use the AsyncCacheBuilder.
1. Initialize a Cache Adapter¶
The library supports any PSR-16 compliant cache adapter or ReactPHP Cache implementation.
Suitable for testing or single-process applications.
2. Create the Manager¶
Use the builder to construct the manager.
Wrapping Operations¶
The core functionality is provided by the wrap method. It takes a cache key, a factory function (which returns a value or a promise), and configuration options.
use Fyennyi\AsyncCache\CacheOptions;
use Fyennyi\AsyncCache\Enum\CacheStrategy;
$options = new CacheOptions(
ttl: 60, // Data is fresh for 60 seconds
strategy: CacheStrategy::Strict // Default strategy
);
$future = $manager->wrap(
'my_cache_key',
function () {
// Your async operation here
// Should return a value or a Promise/Future
return perform_heavy_calculation();
},
$options
);
// Wait for the result
$result = $future->wait();
Next Steps¶
- Explore Caching Strategies to optimize performance.
- Learn about Atomic Operations for counters.
- Configure Rate Limiting for external APIs.