Zend_Memory-Overview.xml 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.memory.overview">
  4. <title>Overview</title>
  5. <sect2 id="zend.memory.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. The <classname>Zend_Memory</classname> component is intended to manage data in an
  9. environment with limited memory.
  10. </para>
  11. <para>
  12. Memory objects (memory containers) are generated by memory manager
  13. by request and transparently swapped/loaded when it's necessary.
  14. </para>
  15. <para>
  16. For example, if creating or loading a managed object would cause
  17. the total memory usage to exceed the limit you specify, some
  18. managed objects are copied to cache storage outside of memory.
  19. In this way, the total memory used by managed objects does not
  20. exceed the limit you need to enforce.
  21. </para>
  22. <para>
  23. The memory manager uses
  24. <link linkend="zend.cache.backends">Zend_Cache backends</link>
  25. as storage providers.
  26. </para>
  27. <example id="zend.memory.introduction.example-1">
  28. <title>Using Zend_Memory component</title>
  29. <para>
  30. <methodname>Zend_Memory::factory()</methodname> instantiates the memory
  31. manager object with specified backend options.
  32. </para>
  33. <programlisting language="php"><![CDATA[
  34. $backendOptions = array(
  35. 'cache_dir' => './tmp/' // Directory where to put the swapped memory blocks
  36. );
  37. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  38. $loadedFiles = array();
  39. for ($count = 0; $count < 10000; $count++) {
  40. $f = fopen($fileNames[$count], 'rb');
  41. $data = fread($f, filesize($fileNames[$count]));
  42. $fclose($f);
  43. $loadedFiles[] = $memoryManager->create($data);
  44. }
  45. echo $loadedFiles[$index1]->value;
  46. $loadedFiles[$index2]->value = $newValue;
  47. $loadedFiles[$index3]->value[$charIndex] = '_';
  48. ]]></programlisting>
  49. </example>
  50. </sect2>
  51. <sect2 id="zend.memory.theory-of-operation">
  52. <title>Theory of Operation</title>
  53. <para>
  54. <classname>Zend_Memory</classname> component operates with the following concepts:
  55. <itemizedlist>
  56. <listitem>
  57. <para>Memory manager</para>
  58. </listitem>
  59. <listitem>
  60. <para>Memory container</para>
  61. </listitem>
  62. <listitem>
  63. <para>Locked memory object</para>
  64. </listitem>
  65. <listitem>
  66. <para>Movable memory object</para>
  67. </listitem>
  68. </itemizedlist>
  69. </para>
  70. <sect3 id="zend.memory.theory-of-operation.manager">
  71. <title>Memory manager</title>
  72. <para>
  73. The memory manager generates memory objects (locked or movable)
  74. by request of user application and returns them wrapped into
  75. a memory container object.
  76. </para>
  77. </sect3>
  78. <sect3 id="zend.memory.theory-of-operation.container">
  79. <title>Memory container</title>
  80. <para>
  81. The memory container has a virtual or actual <property>value</property>
  82. attribute of string type. This attribute contains the data value
  83. specified at memory object creation time.
  84. </para>
  85. <para>
  86. You can operate with this <property>value</property> attribute as
  87. an object property:
  88. </para>
  89. <programlisting language="php"><![CDATA[
  90. $memObject = $memoryManager->create($data);
  91. echo $memObject->value;
  92. $memObject->value = $newValue;
  93. $memObject->value[$index] = '_';
  94. echo ord($memObject->value[$index1]);
  95. $memObject->value = substr($memObject->value, $start, $length);
  96. ]]></programlisting>
  97. <note>
  98. <para>
  99. If you are using a <acronym>PHP</acronym> version earlier than 5.2, use the
  100. <link linkend="zend.memory.memory-objects.api.getRef">getRef()</link>
  101. method instead of accessing the value property directly.
  102. </para>
  103. </note>
  104. </sect3>
  105. <sect3 id="zend.memory.theory-of-operation.locked">
  106. <title>Locked memory</title>
  107. <para>
  108. Locked memory objects are always stored in memory.
  109. Data stored in locked memory are never swapped to the cache
  110. backend.
  111. </para>
  112. </sect3>
  113. <sect3 id="zend.memory.theory-of-operation.movable">
  114. <title>Movable memory</title>
  115. <para>
  116. Movable memory objects are transparently swapped and loaded
  117. to/from the cache backend by <classname>Zend_Memory</classname> when it's necessary.
  118. </para>
  119. <para>
  120. The memory manager doesn't swap objects with size less than
  121. the specified minimum, due to performance considerations.
  122. See <link linkend="zend.memory.memory-manager.settings.min-size">this section</link>
  123. for more details.
  124. </para>
  125. </sect3>
  126. </sect2>
  127. </sect1>