Cache
Loco provides a cache layer to improve application performance by storing frequently accessed data.
Supported Cache Drivers
Loco supports several cache drivers out of the box:
- Null Cache: A no-op cache that doesn't actually store anything (default)
- In-Memory Cache: A local in-memory cache using the
mokacrate - Redis Cache: A distributed cache using Redis
Default Behavior
By default, Loco initializes a Null cache driver. The Null driver implements the cache interface but doesn't actually store any data:
get()operations always returnNone- Other operations like
insert(),remove(), etc. return errors with a message indicating the operation is not supported
If you use the cache functionality without configuring a proper cache driver, many operations will result in errors. It's recommended to configure a real cache driver for production use.
Configuring Cache Drivers
You can configure your preferred cache driver in your application's configuration files (e.g., config/development.yaml).
Configuration Examples
Null Cache (Default)
cache:
kind: Null
In-Memory Cache
feature cache_inmem enable by default
cache:
kind: InMem
max_capacity: 33554432 # 32MiB (default if not specified)
Redis Cache
feature cache_redis should be enabled
cache:
kind: Redis
uri: "redis://localhost:6379"
max_size: 10 # Maximum number of connections in the pool
If no cache configuration is provided, the Null cache will be used by default.
Using the Cache
All items are cached as serialized values with string keys.
use Duration;
use cache;
use ;
async
See the Cache API docs for more examples.