2
0

Zend_Memory-MemoryObjects.xml 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.memory.memory-objects">
  4. <title>Memory Objects</title>
  5. <sect2 id="zend.memory.memory-objects.movable">
  6. <title>Movable</title>
  7. <para>
  8. Create movable memory objects using the <methodname>create([$data])</methodname>
  9. method of the memory manager:
  10. </para>
  11. <programlisting language="php"><![CDATA[
  12. $memObject = $memoryManager->create($data);
  13. ]]></programlisting>
  14. <para>
  15. "Movable" means that such objects may be swapped and unloaded from
  16. memory and then loaded when application code accesses the object.
  17. </para>
  18. </sect2>
  19. <sect2 id="zend.memory.memory-objects.locked">
  20. <title>Locked</title>
  21. <para>
  22. Create locked memory objects using the <methodname>createLocked([$data])</methodname>
  23. method of the memory manager:
  24. </para>
  25. <programlisting language="php"><![CDATA[
  26. $memObject = $memoryManager->createLocked($data);
  27. ]]></programlisting>
  28. <para>
  29. "Locked" means that such objects are never swapped and unloaded
  30. from memory.
  31. </para>
  32. <para>
  33. Locked objects provides the same interface as movable objects
  34. (<classname>Zend_Memory_Container_Interface</classname>).
  35. So locked object can be used in any place instead of movable objects.
  36. </para>
  37. <para>
  38. It's useful if an application or developer can decide, that some
  39. objects should never be swapped, based on performance considerations.
  40. </para>
  41. <para>
  42. Access to locked objects is faster, because the memory manager doesn't
  43. need to track changes for these objects.
  44. </para>
  45. <para>
  46. The locked objects class (<classname>Zend_Memory_Container_Locked</classname>)
  47. guarantees virtually the same performance as working with a string
  48. variable. The overhead is a single dereference to get the class property.
  49. </para>
  50. </sect2>
  51. <sect2 id="zend.memory.memory-objects.value">
  52. <title>Memory container 'value' property</title>
  53. <para>
  54. Use the memory container (movable or locked) '<property>value</property>'
  55. property to operate with memory object data:
  56. </para>
  57. <programlisting language="php"><![CDATA[
  58. $memObject = $memoryManager->create($data);
  59. echo $memObject->value;
  60. $memObject->value = $newValue;
  61. $memObject->value[$index] = '_';
  62. echo ord($memObject->value[$index1]);
  63. $memObject->value = substr($memObject->value, $start, $length);
  64. ]]></programlisting>
  65. <para>
  66. An alternative way to access memory object data is to use the <link
  67. linkend="zend.memory.memory-objects.api.getRef"><methodname>getRef()</methodname></link>
  68. method. This method <emphasis>must</emphasis> be used for <acronym>PHP</acronym>
  69. versions before 5.2. It also may have to be used in some other
  70. cases for performance reasons.
  71. </para>
  72. </sect2>
  73. <sect2 id="zend.memory.memory-objects.api">
  74. <title>Memory container interface</title>
  75. <para>
  76. Memory container provides the following methods:
  77. </para>
  78. <sect3 id="zend.memory.memory-objects.api.getRef">
  79. <title>getRef() method</title>
  80. <programlisting language="php"><![CDATA[
  81. public function &getRef();
  82. ]]></programlisting>
  83. <para>
  84. The <methodname>getRef()</methodname> method returns reference to the object value.
  85. </para>
  86. <para>
  87. Movable objects are loaded from the cache at this moment if the
  88. object is not already in memory. If the object is loaded from
  89. the cache, this might cause swapping of other objects if the
  90. memory limit would be exceeded by having all the managed
  91. objects in memory.
  92. </para>
  93. <para>
  94. The <methodname>getRef()</methodname> method <emphasis>must</emphasis> be
  95. used to access memory object data for <acronym>PHP</acronym> versions before 5.2.
  96. </para>
  97. <para>
  98. Tracking changes to data needs additional resources.
  99. The <methodname>getRef()</methodname> method returns reference to string,
  100. which is changed directly by user application.
  101. So, it's a good idea to use the <methodname>getRef()</methodname> method
  102. for value data processing:
  103. </para>
  104. <programlisting language="php"><![CDATA[
  105. $memObject = $memoryManager->create($data);
  106. $value = &$memObject->getRef();
  107. for ($count = 0; $count < strlen($value); $count++) {
  108. $char = $value[$count];
  109. ...
  110. }
  111. ]]></programlisting>
  112. </sect3>
  113. <sect3 id="zend.memory.memory-objects.api.touch">
  114. <title>touch() method</title>
  115. <programlisting language="php"><![CDATA[
  116. public function touch();
  117. ]]></programlisting>
  118. <para>
  119. The <methodname>touch()</methodname> method should be used in common with
  120. <methodname>getRef()</methodname>. It signals that object value has been changed:
  121. </para>
  122. <programlisting language="php"><![CDATA[
  123. $memObject = $memoryManager->create($data);
  124. ...
  125. $value = &$memObject->getRef();
  126. for ($count = 0; $count < strlen($value); $count++) {
  127. ...
  128. if ($condition) {
  129. $value[$count] = $char;
  130. }
  131. ...
  132. }
  133. $memObject->touch();
  134. ]]></programlisting>
  135. </sect3>
  136. <sect3 id="zend.memory.memory-objects.api.lock">
  137. <title>lock() method</title>
  138. <programlisting language="php"><![CDATA[
  139. public function lock();
  140. ]]></programlisting>
  141. <para>
  142. The <methodname>lock()</methodname> methods locks object in memory.
  143. It should be used to prevent swapping of some objects you choose.
  144. Normally, this is not necessary, because the memory manager uses
  145. an intelligent algorithm to choose candidates for swapping.
  146. But if you exactly know, that at this part of code some
  147. objects should not be swapped, you may lock them.
  148. </para>
  149. <para>
  150. Locking objects in memory also guarantees that reference
  151. returned by the <methodname>getRef()</methodname> method is valid until you
  152. unlock the object:
  153. </para>
  154. <programlisting language="php"><![CDATA[
  155. $memObject1 = $memoryManager->create($data1);
  156. $memObject2 = $memoryManager->create($data2);
  157. ...
  158. $memObject1->lock();
  159. $memObject2->lock();
  160. $value1 = &$memObject1->getRef();
  161. $value2 = &$memObject2->getRef();
  162. for ($count = 0; $count < strlen($value2); $count++) {
  163. $value1 .= $value2[$count];
  164. }
  165. $memObject1->touch();
  166. $memObject1->unlock();
  167. $memObject2->unlock();
  168. ]]></programlisting>
  169. </sect3>
  170. <sect3 id="zend.memory.memory-objects.api.unlock">
  171. <title>unlock() method</title>
  172. <programlisting language="php"><![CDATA[
  173. public function unlock();
  174. ]]></programlisting>
  175. <para>
  176. <methodname>unlock()</methodname> method unlocks object when it's no longer
  177. necessary to be locked. See the example above.
  178. </para>
  179. </sect3>
  180. <sect3 id="zend.memory.memory-objects.api.isLocked">
  181. <title>isLocked() method</title>
  182. <programlisting language="php"><![CDATA[
  183. public function isLocked();
  184. ]]></programlisting>
  185. <para>
  186. The <methodname>isLocked()</methodname> method can be used to check if
  187. object is locked. It returns <constant>TRUE</constant> if the object
  188. is locked, or <constant>FALSE</constant> if it is not locked.
  189. This is always <constant>TRUE</constant> for "locked" objects,
  190. and may be either <constant>TRUE</constant> or <constant>FALSE</constant>
  191. for "movable" objects.
  192. </para>
  193. </sect3>
  194. </sect2>
  195. </sect1>