3 zend-memory assists with managing data in an environment with limited memory.
5 Memory objects (memory containers) are generated by the memory manager at your
6 request and transparently swapped/loaded when necessary.
8 For example, if creating or loading a managed object would cause the total
9 memory usage to exceed the limit you specify, some managed objects are copied to
10 cache storage outside of memory. In this way, the total memory used by managed
11 objects does not exceed the limit you need to enforce. To provide this
12 functionality, the memory manager can compose [zend-cache storage adapters](http://zendframework.github.io/zend-cache/storage/adapter/)
17 Instantiate the memory manager class:
20 use Zend\Memory\MemoryManager;
22 // No caching backend:
23 $memoryManager = new MemoryManager();
26 Optionally, you can create a cache storage adapter, and pass it to the
27 `MemoryManager` constructor:
30 use Zend\Cache\StorageFactory;
31 use Zend\Memory\MemoryManager;
33 // Use a filesystem adapter, placing memory blocks in the tmp directory
34 // under which the application is running.
35 $cache = StorageFactory::factory([
37 'name' => 'filesystem',
38 'options' => ['cache_dir' => './tmp/'],
42 $memoryManager = new MemoryManager($cache);
45 Once you have a `MemoryManager` instance, you can start pushing values to it and
46 pulling values from it.
49 $loadedFiles = array();
51 for ($count = 0; $count < 10000; $count++) {
52 $f = fopen($fileNames[$count], 'rb');
53 $data = fread($f, filesize($fileNames[$count]));
56 $loadedFiles[] = $memoryManager->create($data);
59 echo $loadedFiles[$index1]->value;
61 $loadedFiles[$index2]->value = $newValue;
63 $loadedFiles[$index3]->value[$charIndex] = '_';
66 ## Theory of Operation
68 zend-memory operates with the following concepts:
72 - Locked memory object
73 - Movable memory object
77 The memory manager generates memory objects (locked or movable) by request of
78 the application, and returns them wrapped into a memory container object.
82 The memory container has a virtual or actual `value` attribute of type `string`.
83 This attribute contains the data value specified at memory object creation time.
85 You can operate with this `value` attribute as an object property:
88 $memObject = $memoryManager->create($data);
90 echo $memObject->value;
92 $memObject->value = $newValue;
94 $memObject->value[$index] = '_';
96 echo ord($memObject->value[$index1]);
98 $memObject->value = substr($memObject->value, $start, $length);
103 Locked memory objects are always stored in memory. Data stored in locked memory
104 are never swapped to the cache backend.
108 Movable memory objects are transparently swapped and loaded to/from the cache
109 backend by the `MemoryManager` when necessary.
111 The memory manager does not swap objects with size less than the specified
112 minimum, due to performance considerations. See [the settings section](memory-manager.md#settings)