View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template}

{zone-data:component-name}
Zend_Memory
{zone-data}

{zone-data:proposer-list}
[Alexander Veremyev|mailto:alexander.v@zend.com]
{zone-data}

{zone-data:revision}
1.0 - 21 July 2006: Created.
{zone-data}

{zone-data:overview}
Zend_Memory is a component that offers API for managing data in limited memory mode.
{zone-data}

{zone-data:references}
{zone-data}

{zone-data:requirements}
* Module must effectively manage large amount of data.
* Memory management must be based on access statistics.
* Data load/unload process must be transparent for an application.
{zone-data}

{zone-data:dependencies}
* Zend_Exception
{zone-data}

{zone-data:operation}
User creates Zend_Memory objects to store binary (string) value by using appropriate backend (Zend_Cache backends).
Stored value can be accessed by 'value' property of Zend_Memory class.
{zone-data}

{zone-data:class-list}
* Zend_MemoryManager
* Zend_Memory
* Zend_Memory_Exception
{zone-data}

{zone-data:use-cases}
||UC-01||

{code}
$memoryManager = new Zend_MemoryManager();
$memoryManager->setMemoryLimit(1024*1024*32);
...
{code}


||UC-02||

{code}
$backendOptions = array(
'cacheDir' => './tmp/' // Directory where to put the cache files
);
$memoryManager = new Zend_MemoryManager('File', $backendOptions);
...

{code}


||UC-03||

{code}
$loadedFiles = array();

for ($count = 0; $count < 10000; $count++) {
$f = fopen($fileNames[$count], 'rb');
$data = fread($f, filesize($fileNames[$count]));
$fclose($f);

$loadedFiles[] = $memoryManager->create($data)
}

...

echo $loadedFiles[$index1]->value;
...
$loadedFiles[$index2]->value = $newValue;
...
$loadedFiles[$index3]->lock();
$loadedFiles[$index3]->value[$charIndex] = ' ';
$loadedFiles[$index3]->unlock();
{code}

{zone-data}

{zone-data:skeletons}
{code}
class Zend_Memory_Exception extends Zend_Exception {}

/**
* Memory manager
*
* This class encapsulates memory menagement operations, when PHP works
* in limited memory mode.
*
*
* @category Zend
* @package Zend_Memory
* @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_MemoryManager
{
/**
* Object storage backend
*
* @var Zend_Cache_Backend_Interface
*/
private $_backend;

/**
* Memory grow limit.
* Default value is 2/3 of memory_limit php.ini variable
* Negative value means no limit
*
* @var integer
*/
private $_memoryLimit = -1;

/**
* Minimum value size to be swapped.
* Default value is 16K
* Negative value means that all values may be swapped
*
* @var integer
*/
private $_minSize = 16384;

/**
* Overall size of memory, used by values
*
* @var integer
*/
private $_memorySize = 0;

/**
* Counter of Zend_Memory objects
*
* @var integer
*/
private $_objects = 0;

/**
* Id for next Zend_Memory object
*
* @var integer
*/
private $_nextId = 0;

/**
* Dinamic list of loaded objects
*
* @var array
*/
private $_loadedObjects;


/**
* Memory manager constructor
*
* If backend is not specified, then 'File' backend with default options is used
*
* @param string $backend backend name
* @param array $backendOptions associative array of options for the corresponding backend constructor
*/
public function __construct($backend = null, $backendOptions);

/**
* Set memory grow limit
*
* @param string $newLimit
* @throws Zend_Exception
*/
public function setMemoryLimit($newLimit);

/**
* Set minimum size of values, which may be swapped
*
* @param integer $newSize
* @throws Zend_Exception
*/
public function setMinSize($newSize);

/**
* Create new Zend_Memory object
*
* @param string $value
* @param boolean $share Share memory object between scripts if true
* @return Zend_Memory
*/
public function create($value, $share = false);

/**
* Get Zend_Memory object
*
* @param integer $id
* @return Zend_Memory
*/
public function get($id);

/**
* Swap object data to disk
* Actualy swaps data or only unloads it from memory,
* if object is not changed since last swap
*/
private function _swap($id);

/**
* Load value from swap file.
*/
private function _load($id);
}

/**
* Memory object
*
* @category Zend
* @package Zend_Memory
* @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Memory
{
/**
* Internal object Id
*
* @var integer
*/
private $_id;

/**
* Value container
*
* @var string
*/
private $_value;

/**
* Value size
*
* @var integer
*/
private $_size;

const LOADED = 1;
const SWAPPED = 2;
const LOCKED = 4;
/**
* Object state (LOADED/SWAPPED)
*
* @var integer
*/
private $_state;

public function __construct($memoryManagerReference, $id, $value);
public function __destruct();

/**
* Lock object in memory.
* If writeLock is true, than object is locked for writing
* Otherwise only read lock is obtained.
* (Write lock also discards swapped data)
*
* @param boolean $writeLock
*/
public function lock($writeLock = true);

/**
* Unlock object
*/
public function unlock();

/**
* Get memory object id
*
* @return integer
*/
public function getId();

/**
* Get handler
*
* Loads object if necessary and moves it to the top of loaded objects list.
* Swaps objects from the bottom of loaded objects list, if necessary.
*
* @param string $property
* @return string
* @throws Zend_Exception
*/
public function &__get($property);

/**
* Set handler
*
* @param string $property
* @param string $value
* @throws Zend_Exception
*/
public function __set($property, $value);
}

{code}
{zone-data}

{zone-data:milestones}
* Milestone 1: Proposal is reviewed and approved by community
* Milestone 2: [design notes will be published here|http://framework.zend.com/wiki/display/ZFDEV/Zend+Framework+Components+-+Developer+Notes]
* Milestone 3: Working prototype checked into the incubator
* Milestone 4: Unit tests exist, work, and are checked into SVN.
* Milestone 5: Initial documentation exists.
* Milestone 6: Zend_Pdf is switched to use Zend_Memory for resources (take a look at [ZF-12|http://framework.zend.com/issues/browse/ZF-12]).
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>