user3287550 user3287550 - 3 months ago 18
PHP Question

Memcached return not stored

I'm a newbie with

Memcached
and I'm trying to use this feature for have an editable configuration over the air in my application.

So I need to store some data, for doing this I did:

class Settings
{
private $_config = array();
private static $_memcached = null;

function __construct()
{
self::$_memcached = new Memcached();
self::$_memcached->addServer('127.0.0.1', 11211);

$file = 'config.php'; //return $config content
require_once $file;

$this->_config = $config;

foreach($config as $item => $value)
{
self::$_memcached->add($item, $value);
echo self::$_memcached->getResultMessage();
}
}
}


I followed the instruction of the documentation available here.
Unlucky I get this error:


NOT STORED


What am I doing wrong?

Answer

As I wrote in comments. Some key exist in memcached. And you try to add key one more time. It is the reason. Try to add inspection before add. It should be something like this:

foreach ($config as $item => $value) {
    if (!(bool)self::$_memcached->get($item)) {
        self::$_memcached->add($item, $value);
        echo self::$_memcached->getResultMessage();
    }
}
Comments