Cache class provides a unified interface for working with different cache drivers including Memcached, Redis, and local file-based caching.
Namespace
Methods
setConnection
Sets or picks a cache connection based on the configuration.The name of the cache connection to use. If not provided, uses the default connection from configuration.
\Exception if the specified connection is not found in configuration
Supported Drivers
- memcached - Uses PHP Memcached extension with compression enabled
- redis - Uses Predis client with configurable timeouts
- local - Uses file-based caching
Usage Example
Configuration
The cache connection settings are defined inconfig/cache.php:
Environment Variables
For Memcached:MEMCACHED_HOST- Memcached server hostMEMCACHED_PORT- Memcached server port
REDIS_PROTOCOL- Redis protocol (tcp/unix)REDIS_HOST- Redis server hostREDIS_PORT- Redis server portREDIS_PASSWORD- Redis authentication password
Helper Function
The framework provides a globalcache() helper function for convenient access:
Notes
- Redis connections for queue operations use infinite timeout (0) to support blocking operations like
BLPOP - Regular Redis cache connections use a 1-second timeout
- Memcached connections have compression enabled by default
- The class uses the
Proxyabletrait for method delegation to the underlying driver