酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memory_saving.md 3.4 KiB

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