Issues

ZF2-133: CLONE - Zend_Cache_Core::save() requires that $data be a string or that it be auto-serialized

Description

Trying to store and integer or an object throws a fatal exception even when using a cache backend that supports a large range of values. Memcache and APC can store complete objects and they do not need to be pre-serialized for those backends. It is generally wise to wrap data in an object prior to storing it because otherwise arrays are mangled in certain circumstances.

Checking the validity of the passed dat should be the domain of the backend, not the front-end. The front-end has no way of knowing which data is valid for a particular backend. As mentioned, Memcache and APC have no trouble dealing with complex objects or arrays. In any case, storing an integer should be reasonable.

Lines 276-283 from Zend_Cache_Core:

 
        if ($this->_options['automatic_serialization']) {
            // we need to serialize datas before storing them
            $data = serialize($data);
        } else {
            if (!is_string($data)) {
                Zend_Cache::throwException("Datas must be string or set automatic_serialization = true");
            }
        }

Test code to demonstrate the problem:


$frontendOptions = array(
    'caching' => true,
    'logging' => false,
    'lifetime' => 3600,
    'automatic_cleaning_factor' => 0,
);
$backendOptions = array(
    'servers' => array(array('host' => 'localhost', 'port' => 11211, 'persistent' => false)),
    'compression' => true,
);
$cache = Zend_Cache::factory('Core', 'Memcached', $frontendOptions, $backendOptions);
$cache->save(1, 'test_cache');

Comments

fixed by desing

Now data no longer gets serialized by default. To enable serializing please use the plug-in "serializer"