The Story Behind NitroCache
As a PHP developer working on Windows, I've always struggled with the overhead of Redis and Memcached in local environments. Running Docker or WSL2 just to have a fast key-value store felt like overkill. I wanted something native, lightweight, and incredibly fast.
So, I decided to build NitroCache.
π¦ Why Rust?
I chose Rust for the core engine because I needed:
- Memory Safety: Handling shared memory segments can be dangerous; Rust makes it predictable.
- Performance: I wanted to achieve near-zero latency.
- FFI Compatibility: Rust makes it easy to export C-compatible functions that PHP can call via the FFI extension.
π The Architecture: Bypassing the Network Stack
Standard caching solutions use TCP/IP sockets. Even on localhost, this introduces overhead (handshakes, packet processing).
NitroCache uses Shared Memory (shm).
- The Rust Server manages a dedicated memory segment and handles TTL/eviction.
- The PHP Client maps that same memory segment into its own process.
The result? We bypass the network stack entirely. Accessing data takes about 16-20 microseconds.
π Benchmarks (Windows 10, PHP 8.4)
In my local tests with 500,000 keys, I achieved:
- SET: ~61,500 ops/s
- GET: ~57,400 ops/s (Note: Peak performance in optimized environments reaches much higher).
π οΈ How to use it
It's as simple as:
$cache = new NitroCache(maxMemoryMb: 512);
$cache->set('key', 'value', 3600);
echo $cache->get('key');
π Open Source & Future
NitroCache is completely open-source (MIT). I'm currently in the alpha stage and looking for feedback on:
FFI stability in long-running processes.
Memory management edge cases.
Linux support (it's next on the roadmap!).
Check out the code here:
https://github.com/mamontil/nitro-cache
I'd love to hear your thoughts! Have you ever used FFI in your PHP projects?

Top comments (0)