Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Error when attempting to save data to memcached

Error is: Failed storing data in memcached, error code: 20

    use Phalcon\Mvc\User\Component;
    use Phalcon\DI;
    use Phalcon\Cache\Frontend\Data as FrontData;
    use Phalcon\Cache\Backend\Libmemcached as BackMemCached;
    use Phalcon\Mvc\Model\Message;

    class Universe extends Component {

        protected function buildCache() {
            $sdk = $this->getDi()->getShared('sdkaccount');
            $region = $this->getDi()->getShared('session')->get('current_region');

            $params = [
                'brands' => null,
                'packages' => null,
                'groups' => null
            ];

            $getParams = [
                'region' => $region->code
            ];

            $sdk_response = $sdk->post("AudienceProfile/Region", $params, $getParams);

            if ($sdk_response->result != "success") {
                $this->appendMessage(new Message($sdk_response->message));
                return false;
            }

            $this->buildCacheData($sdk_response,"public");

            return true;
        }

        protected function buildCacheData($sdk_response,$type) {
            $cache = $this->getCache();

            $brands = $sdk_response->metadata->brands;
            $packages = $sdk_response->metadata->packages;
            $groups = $sdk_response->metadata->groups;
            $audiences = $sdk_response->data;

            if ($type == "private") {
                $user = DI::getDefault()->getSession()->get('auth_user');
                $cache->save("privatebrands".$user->account_id,$brands);
                $cache->save("privatepackages".$user->account_id,$packages);
                $cache->save("privategroups".$user->account_id,$groups);
                $cache->save("privateaudiences".$user->account_id,$audiences);
            } else {
                //// It's dying here
                $cache->save("publicbrands",$brands);
                $cache->save("publicpackages",$packages);
                $cache->save("publicgroups",$groups);
                $cache->save("publicaudiences",$audiences);
            }

            return true;
        }

        protected function getCache() {
            $frontCache = new FrontData([
                "lifetime" => 900 //15 minutes
            ]);

            $cache = new BackMemCached(
                $frontCache,
                [
                    "servers" => [
                        "host" => "127.0.0.1",
                        "port" => "11211",
                        "weight" => "1"
                    ]
                ]
            );

            return $cache;
        }


        public function getGroups($arrGroupIDs) {
            $user = DI::getDefault()->getSession()->get('auth_user');
            $cache = $this->getCache();
            $arr = [];
            $groups = $cache->get("publicgroups");

            if (empty($groups)) {
                if (!$this->buildCache()) {
                    return false;
                }
                $groups = $cache->get("publicgroups");
            }

            foreach ($arrGroupIDs as $groupID) {
                foreach($groups as $group) {
                    var_dump($group,$groupID);
                    die();
                }
            }
            return $arr;
        }
    }

It's dying calling $cache->save("publicbrands",$brands); What am I doing wrong?



9.8k

Thanks! I thought this was a Phalcon specific error and found no docs. Good to know it's a general PHP error code.



9.8k

I've made sure that memcached is installed on the server, but I'm still getting the same error. When I print_r the cache instance, I get this:

    object(Phalcon\Cache\Backend\Libmemcached)#104 (8) {
    ["_frontend":protected]=>
    object(Phalcon\Cache\Frontend\Data)#118 (1) {
        ["_frontendOptions":protected]=>
        array(1) {
        ["lifetime"]=>
          int(900)
        }
    }
    ["_options":protected]=>
    array(2) {
        ["servers"]=>
        array(3) {
        ["host"]=>
        string(9) "127.0.0.1"
        ["port"]=>
        string(5) "11211"
        ["weight"]=>
        string(3) "100"
        }
        ["statsKey"]=>
        string(5) "_PHCM"
    }
    ["_prefix":protected]=>
    string(0) ""
    ["_lastKey":protected]=>
    string(0) ""
    ["_lastLifetime":protected]=>
    NULL
    ["_fresh":protected]=>
    bool(false)
    ["_started":protected]=>
    bool(false)
    ["_memcache":protected]=>
    NULL
    }

It seems like this is a valid instance of the memcached backend. The code from above is still valid, but I continue to get this error



9.8k

Never mind... Created it via DI and now it's working.

DI (SharedService) is always a way to go.