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 fluent configure() method.
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 fluent configuration API to construct the manager.
use Fyennyi\AsyncCache\AsyncCacheManager;
$manager = new AsyncCacheManager(
AsyncCacheManager::configure($cache)
->build()
);
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
);
$promise = $manager->wrap(
'my_cache_key',
function () {
// Your async operation here
// Should return a value or a Promise
return perform_heavy_calculation();
},
$options
);
// Handle the result asynchronously
$promise->then(function ($result) {
echo $result;
});
// Or wait synchronously with React\Async\await()
$result = \React\Async\await($promise);
Next Steps¶
- Explore Caching Strategies to optimize performance.
- Learn about Atomic Operations for counters.
- Configure Rate Limiting for external APIs.