eZ Community » Forums » Suggestions » Caching of function attributes in...
expandshrink

Caching of function attributes in eZPersistentObject, why not?

Caching of function attributes in eZPersistentObject, why not?

Thursday 08 December 2011 3:15:48 pm - 1 reply

I have discovered that the values of attributes in eZPersistentObjects that are generated by functions are not cached at all. This means that the function is called over and over again even if the attribute is accessed repeatedly on the same object instance. Often this leads to identical queries being sent to the database several times.

A suggestion is that the return value from the accessor function for each attribute is saved after the first call, and then just returned on subsequent accesses. This can be easily implemented with just five rows of code. This could give a performance gain throughout the system.

 

Is there any problems or other implications with this approach?

 

Example, ezpersistentobject.php in function attribute and in function setAttribute:

if (!isset($this->functionAttributesCache)) $this->functionAttributesCache = array();
if (array_key_exists($attr, $this->functionAttributesCache)) {
    $retVal = $this->functionAttributesCache[$attr];
}
elseif ( method_exists( $this, $functionName ) )
{
    $this->functionAttributesCache[$attr] = $retVal = $this->$functionName();
}
$functionName = $functions[$attr];
$this->functionAttributesCache[$attr] = $val;
$oldValue = $this->$functionName( $val );

Modified on Thursday 08 December 2011 3:20:06 pm by Johan Beronius

Thursday 08 December 2011 4:29:53 pm

By doing it this way you will very quickly get memory problems.
We are planning on fixing this in our up coming API with Domain object cache, kind of a Unit of Work if you will. But this will then make sure to discard objects when you reach a certain amount of objects to not use to much memory.

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from