Overview
PhpSpreadsheet uses approximately 1KB per cell (1.6KB on 64-bit PHP) in your worksheets. Large workbooks can quickly consume available memory. Cell caching provides a mechanism to maintain cell objects in a smaller memory footprint or off-memory storage (disk, APCu, Memcache, or Redis).How Cell Caching Works
By default, PhpSpreadsheet holds all cell objects in memory. You can specify alternatives by providing your own PSR-16 cache implementation.Key Features
- Automatic namespacing: Cache keys are automatically namespaced
- Automatic cleanup: Cache is cleaned up after use
- Shared instances: A single cache instance can be shared across multiple PhpSpreadsheet instances
- Per-worksheet caching: Each worksheet maintains a separate cache
Configuring Cell Caching
Cell caching must be configured before reading a workbook or creating your first worksheet. Configuration cannot be changed once worksheets are instantiated.Basic Setup
Cache Implementations
PhpSpreadsheet does not ship with alternative cache implementations. You must select the most appropriate implementation for your environment using pre-existing PSR-16 libraries.APCu Cache
Ideal for applications with APCu extension enabled. Provides in-memory caching with excellent performance.Installation
Configuration
Redis Cache
Best for distributed applications or when you need persistent caching across requests.Installation
Configuration
Memcache Cache
Suitable for distributed caching environments.Installation
Configuration
Complete Example
Here’s a complete example showing how to configure caching before loading a large spreadsheet:Best Practices
Choose the Right Cache Backend
Choose the Right Cache Backend
- Small to medium workbooks: Use default memory caching
- Large workbooks (single server): Use APCu for best performance
- Large workbooks (distributed): Use Redis or Memcache
- Very large workbooks: Consider Redis with sufficient memory allocation
Configure Cache Before Loading
Configure Cache Before Loading
Always set up caching before creating or loading spreadsheets. Configuration cannot be changed after worksheet instantiation.
Disable TTL
Disable TTL
Ensure your cache backend has TTL disabled or set long enough to prevent data loss during processing.
Monitor Memory Usage
Monitor Memory Usage
Use PHP’s
memory_get_peak_usage() to monitor memory consumption and verify cache effectiveness:Memory Usage Comparison
| Configuration | Memory per Cell | 10,000 Cells | 100,000 Cells |
|---|---|---|---|
| No caching (32-bit) | ~1 KB | ~10 MB | ~100 MB |
| No caching (64-bit) | ~1.6 KB | ~16 MB | ~160 MB |
| With PSR-16 cache | Varies | Significantly reduced | Significantly reduced |
Actual memory savings depend on your cache implementation and configuration. APCu and Redis typically provide the best memory reduction.
Troubleshooting
”Cell entry no longer exists in cache”
This error occurs when cached data is prematurely deleted. Common causes:- TTL expiration (disable or increase TTL)
- Cache storage limit reached (increase cache size)
- External process cleared the cache
Performance Issues with Caching
If caching makes your application slower:- Verify network latency to Redis/Memcache servers
- Consider APCu for local caching
- Check if your workbook is small enough to process without caching
See Also
- Read Filters - Load only specific data
- Caching Strategies - Advanced cache configuration
- Reading Large Files - Additional optimization techniques

