FilesystemCache
TheFilesystemCache is the default implementation provided by Apivalk. It is suitable for most applications, especially in environments where a shared filesystem is available or on single-server setups.
Characteristics
- Persistence: Data is saved as JSON-encoded files.
- Organization: Each cache item is stored in its own file, named using a SHA-256 hash of its key.
- Safety: Automatically creates the cache directory if it doesn’t exist.
Usage Example
Generic Nature & Custom Adapters
Apivalk’s cache system is strictly interface-based. This means you can easily swap theFilesystemCache for an implementation that fits your infrastructure better.
Implementing CacheInterface
Feel free to orientate on the existing FilesystemCache implementation for inspiration.
To create your own cache provider (e.g., for Redis, Memcached, or Database), you only need to implement the CacheInterface:
Why use a Custom Cache?
- Distributed Environments: If your API runs on multiple nodes, using a centralized cache like Redis ensures all nodes share the same routing index.
- Extreme Performance: Memory-based caches (Redis, APCu) are significantly faster than disk-based caches for high-traffic APIs.
- Infrastructure Alignment: Use the tools your team already manages and monitors.
Using CacheItem
When implementing your own cache, you’ll interact with the CacheItem class. It provides a convenient toJson() method for serialization and a static byJson() method for restoration, though you are free to store the data in any format as long as you can reconstruct the CacheItem object.