| 1 |
# Memory saving |
| 2 |
|
| 3 |
PhpSpreadsheet uses an average of about 1k per cell in your worksheets, so |
| 4 |
large workbooks can quickly use up available memory. Cell caching |
| 5 |
provides a mechanism that allows PhpSpreadsheet to maintain the cell |
| 6 |
objects in a smaller size of memory, or off-memory (eg: on disk, in APCu, |
| 7 |
memcache or redis). This allows you to reduce the memory usage for large |
| 8 |
workbooks, although at a cost of speed to access cell data. |
| 9 |
|
| 10 |
By default, PhpSpreadsheet holds all cell objects in memory, but |
| 11 |
you can specify alternatives by providing your own |
| 12 |
[](https://www.php-fig.org/psr/psr-16/PSR-16](https://www.php-fig.org/psr/psr-16/](https://www.php-fig.org/psr/psr-16/) implementation. PhpSpreadsheet keys |
| 13 |
are automatically namespaced, and cleaned up after use, so a single cache |
| 14 |
instance may be shared across several usage of PhpSpreadsheet or even with other |
| 15 |
cache usages. |
| 16 |
|
| 17 |
To enable cell caching, you must provide your own implementation of cache like so: |
| 18 |
|
| 19 |
``` php |
| 20 |
$cache = new MyCustomPsr16Implementation(); |
| 21 |
|
| 22 |
\PhpOffice\PhpSpreadsheet\Settings::setCache($cache); |
| 23 |
``` |
| 24 |
|
| 25 |
A separate cache is maintained for each individual worksheet, and is |
| 26 |
automatically created when the worksheet is instantiated based on the |
| 27 |
settings that you have configured. You cannot change |
| 28 |
the configuration settings once you have started to read a workbook, or |
| 29 |
have created your first worksheet. |
| 30 |
|
| 31 |
## Beware of TTL |
| 32 |
|
| 33 |
As opposed to common cache concept, PhpSpreadsheet data cannot be re-generated |
| 34 |
from scratch. If some data is stored and later is not retrievable, |
| 35 |
PhpSpreadsheet will throw an exception. |
| 36 |
|
| 37 |
That means that the data stored in cache **must not be deleted** by a |
| 38 |
third-party or via TTL mechanism. |
| 39 |
|
| 40 |
So be sure that TTL is either de-activated or long enough to cover the entire |
| 41 |
usage of PhpSpreadsheet. |
| 42 |
|
| 43 |
## Common use cases |
| 44 |
|
| 45 |
PhpSpreadsheet does not ship with alternative cache implementation. It is up to |
| 46 |
you to select the most appropriate implementation for your environment. You |
| 47 |
can either implement [](https://www.php-fig.org/psr/psr-16/PSR-16](https://www.php-fig.org/psr/psr-16/](https://www.php-fig.org/psr/psr-16/) from scratch, |
| 48 |
or use [](https://packagist.org/search/?q=psr-16pre-existing libraries](https://packagist.org/search/?q=psr-16](https://packagist.org/search/?q=psr-16). |
| 49 |
|
| 50 |
One such library is [](https://www.php-cache.com/PHP Cache](https://www.php-cache.com/](https://www.php-cache.com/) which |
| 51 |
provides a wide range of alternatives. Refers to their documentation for |
| 52 |
details, but here are a few suggestions that should get you started. |
| 53 |
|
| 54 |
### APCu |
| 55 |
|
| 56 |
Require the packages into your project: |
| 57 |
|
| 58 |
```sh |
| 59 |
composer require cache/simple-cache-bridge cache/apcu-adapter |
| 60 |
``` |
| 61 |
|
| 62 |
Configure PhpSpreadsheet with something like: |
| 63 |
|
| 64 |
```php |
| 65 |
$pool = new \Cache\Adapter\Apcu\ApcuCachePool(); |
| 66 |
$simpleCache = new \Cache\Bridge\SimpleCache\SimpleCacheBridge($pool); |
| 67 |
|
| 68 |
\PhpOffice\PhpSpreadsheet\Settings::setCache($simpleCache); |
| 69 |
``` |
| 70 |
|
| 71 |
### Redis |
| 72 |
|
| 73 |
Require the packages into your project: |
| 74 |
|
| 75 |
```sh |
| 76 |
composer require cache/simple-cache-bridge cache/redis-adapter |
| 77 |
``` |
| 78 |
|
| 79 |
Configure PhpSpreadsheet with something like: |
| 80 |
|
| 81 |
```php |
| 82 |
$client = new \Redis(); |
| 83 |
$client->connect('127.0.0.1', 6379); |
| 84 |
$pool = new \Cache\Adapter\Redis\RedisCachePool($client); |
| 85 |
$simpleCache = new \Cache\Bridge\SimpleCache\SimpleCacheBridge($pool); |
| 86 |
|
| 87 |
\PhpOffice\PhpSpreadsheet\Settings::setCache($simpleCache); |
| 88 |
``` |
| 89 |
|
| 90 |
### Memcache |
| 91 |
|
| 92 |
Require the packages into your project: |
| 93 |
|
| 94 |
```sh |
| 95 |
composer require cache/simple-cache-bridge cache/memcache-adapter |
| 96 |
``` |
| 97 |
|
| 98 |
Configure PhpSpreadsheet with something like: |
| 99 |
|
| 100 |
```php |
| 101 |
$client = new \Memcache(); |
| 102 |
$client->connect('localhost', 11211); |
| 103 |
$pool = new \Cache\Adapter\Memcache\MemcacheCachePool($client); |
| 104 |
$simpleCache = new \Cache\Bridge\SimpleCache\SimpleCacheBridge($pool); |
| 105 |
|
| 106 |
\PhpOffice\PhpSpreadsheet\Settings::setCache($simpleCache); |
| 107 |
``` |
| 108 |
|