Reference
Memory Objects
Movable
"Movable" memory objects are ones that may be swapped into the cache backend andunloaded from memory when not in active use.
Create movable memory objects using thecreate([$data]) method of the memorymanager:
$memObject = $memoryManager->create($data);Such objects will be retrieved from the cache and/or memor when accessed again.
Locked
"Locked" memory objects will never be swapped to cache or unloaded from memory.
Create locked memory objects using thecreateLocked([$data]) method of thememory manager:
$memObject = $memoryManager->createLocked($data);Locked objects implement the same interface as movable objects(Zend\Memory\Container\Interface), and can be used interchangably with movableobjects. Use them when you have performance considerations that dictate keepingthe information in memory. Access to locked objects is faster, because thememory manager doesn't need to track changes for these objects.
The locked objects class (Zend\Memory\Container\Locked) guarantees virtuallythe same performance as working with a string variable. The overhead is a singledereference to get the class property.
Memory container 'value' property
Use the memory container (movable or locked)value property to operate withmemory object data:
$memObject = $memoryManager->create($data);echo $memObject->value;$memObject->value = $newValue;$memObject->value[$index] = '_';echo ord($memObject->value[$index1]);$memObject->value = substr($memObject->value, $start, $length);An alternative way to access memory object data is to use thegetRef(); method.
Memory container interface
Each memory container type provides the following methods:
getRef() method
&getRef() : mixedThegetRef() method returns a reference to the object value.
Movable objects are loaded from the cache at this moment if the object is notalready in memory. If the object is loaded from the cache, this might causeswapping of other objects if the memory limit would be exceeded by having allthe managed objects in memory.
Tracking changes to data needs additional resources. ThegetRef() methodreturns a reference to the string value used to store the data, which is changeddirectly by user application. Use thegetRef() method for value dataprocessing where you want to ensure the data changes without necessarilyinteracting directly with the memory container:
$memObject = $memoryManager->create($data);$value = &$memObject->getRef();for ($count = 0; $count < strlen($value); $count++) { $char = $value[$count]; // ...}touch() method
touch() : voidThetouch() method should be used in conjunction withgetRef(). It signalsthat object value has been changed:
$memObject = $memoryManager->create($data);...$value = &$memObject->getRef();for ($count = 0; $count < strlen($value); $count++) { // ... if ($condition) { $value[$count] = $char; } // ...}$memObject->touch();lock() method
lock() : voidThelock() methods locks the object in memory. It should be used to preventswapping of the object. Normally, this is not necessary, because the memorymanager uses an intelligent algorithm to choose candidates for swapping. But ifyou know that at a specific point in the code an object should not be swapped,you may lock it.
Locking objects in memory also guarantees that the reference returned by thegetRef() method is valid until you unlock the object:
$memObject1 = $memoryManager->create($data1);$memObject2 = $memoryManager->create($data2);...$memObject1->lock();$memObject2->lock();$value1 = &$memObject1->getRef();$value2 = &$memObject2->getRef();for ($count = 0; $count < strlen($value2); $count++) { $value1 .= $value2[$count];}$memObject1->touch();$memObject1->unlock();$memObject2->unlock();unlock() method
unlock() : voidTheunlock() method unlocks object when it's no longer necessary to be locked.See the example above.
isLocked() method
isLocked() : boolTheisLocked() method can be used to check if object is locked. It returnstrue if the object is locked, orfalse if it is not locked. This is alwaystrue for "locked" objects, and may be eithertrue orfalse for "movable"objects.
Found a mistake or want to contribute to the documentation? Edit this page on GitHub!