- How do I install and use FastSet in a Laravel project?
- Install via Composer with `composer require toflar/fast-set`, then create a set using `FastSet::make([1, 2, 3])`. For Laravel’s service container, bind it in `config/app.php` under `providers` or use a singleton in a service class. Example: `app()->singleton(FastSet::class, fn() => FastSet::make($yourData));`.
- What Laravel versions and PHP versions does FastSet support?
- FastSet requires **PHP 8.1+** for performance optimizations like typed properties. It’s compatible with **Laravel 9+**, leveraging its service container and caching systems. Tested up to Laravel 11; older versions may need minor adjustments for dependency injection.
- Can FastSet handle dynamic datasets, or is it only for static data?
- FastSet is **immutable after compilation**, so it’s best for static or infrequently updated data (e.g., country codes, blacklists). For dynamic datasets, rebuild the set periodically using Laravel’s scheduler (`Artisan::schedule()`) or queue jobs (`dispatch()`). Avoid real-time updates unless using a hybrid approach with Redis or a database.
- How does FastSet compare to Laravel’s native cache drivers for membership checks?
- FastSet excels for **in-memory, high-frequency lookups** (e.g., API input validation) with **O(1) complexity**, while Laravel’s cache (Redis, APCu) adds persistence and distributed scaling but with higher latency (~1–10ms vs. FastSet’s ~0.1ms). Use FastSet for speed-critical paths and cache for persistence.
- What’s the memory overhead of FastSet for large datasets (e.g., 1M+ elements)?
- FastSet uses **bitwise operations** for efficiency but consumes **~1.5x–2x the RAM of native arrays** for large sets. Monitor with `memory_get_usage()` in Laravel’s Tinker or a test script. For >1M elements, consider **chunking** (split into smaller sets) or offloading to Redis. Benchmark with `FastSet::benchmark()` before production.
- How do I integrate FastSet with Laravel’s caching system (e.g., Redis, APCu)?
- Store **serialized FastSet data** in Laravel’s cache (e.g., `Cache::put('fast_set_key', serialize($fastSet))`). Rebuild the set on cache misses or via Laravel’s `Cache::tags()` for invalidation. For APCu, use `Cache::store('apcu')`—FastSet’s precompiled structure avoids disk I/O but requires manual deserialization.
- Are there performance pitfalls when using FastSet in production?
- Watch for **cold starts** (precompilation adds ~50–100ms overhead) and **thread safety** if rebuilding sets in queues. Mitigate by lazy-loading sets or using `dispatchSync()` for critical paths. Also, **hash collisions** (using `spl_object_hash`) are rare but test edge cases with custom data. Enable **OPcache** in `php.ini` for optimal performance.
- Can FastSet replace Redis Sets for my Laravel application?
- FastSet is **faster for in-memory lookups** but lacks Redis’s persistence, clustering, or atomic operations. Use FastSet for **low-latency, single-server** needs (e.g., fraud detection) and Redis for **distributed, persistent** sets. Hybrid approach: Use FastSet for hot data and Redis for backup or sync.
- How do I test FastSet in Laravel unit tests (e.g., with Pest or PHPUnit)?
- Mock FastSet using **Mockery** or Laravel’s `partialMock`. Example: `FastSet::shouldReceive('contains')->andReturn(true);`. For integration tests, use a real instance but reset it between tests: `$fastSet = FastSet::make([]);`. Test edge cases like empty sets, collisions, and serialization/deserialization.
- What’s the best way to rebuild FastSet dynamically when data changes (e.g., from a database)?
- Use **Laravel’s Eloquent observers** or **model events** (e.g., `Model::saved()`) to trigger set rebuilds. For large datasets, queue the rebuild with `dispatch(new RebuildFastSetJob($data))` and use Laravel’s `dispatchSync()` for critical paths. Sync with a database via `Model::observers` or a scheduled job (`Artisan::call('fastset:rebuild')`).