Yii2 Memcache Cross-platform interaction values cannot be unified

Source: Internet
Author: User
Tags yii

1 First load in configuration file

web\basic\config\web.php ...    ' components ' = = [        ' request ' = = [            //!!! Insert a secret key in the following (if it's empty)-This is Requi Red by Cookie validation '            cookievalidationkey ' = ' Zhaoyang ',        ],        ' mecache ' + = [            ' class ' = ' Yii\caching\memcache ',            ' usememcached ' =>0,            ' servers ' = =                [                    ' host ' = ' localhost ',                    ' Port ' = = 11211,                ]            ],        ],        ' user ' = [            ' identityclass ' = ' app\models\user ',            ' Enableautologin ' = True,        ],............

2 Calling Memcache

$mecache = Yii:: $app->mecache;   $mecache->set ("Key", "value", "$");   $mecache->get ("key");

It's possible to use it in general, no problem at all.

But I memcache the problem with the Java side.

Java writes memcache, I can't get it.

PHP writes the memcache, Java can't get

3 workaround

Web\basic\vendor\yiisoft\yii2\caching\cache.php//overrides 2 methods, both read and write call this method public function MySet ($key, $value, $duration = 0, $ Dependency = null) {    return $this->setvalue ($key, $value, $duration);} Public Function Myget ($key) {    return $this->getvalue ($key);}

The following explains why, in short, the program is encrypted, remove the encryption layer, PHP Java read and write unified on the line

4 analysis

① $mecache = Yii:: $app->mecache;

②\basic\vendor\yiisoft\yii2\caching\memcache.php

③class MemCache extends Cache is a self-cache \basic\vendor\yiisoft\yii2\caching\cache.php

④ View the original Yii2 set and get are encrypted

    Public function Get ($key) {$key = $this->buildkey ($key);        $value = $this->getvalue ($key);        if ($value = = = False | | $this->serializer = = = False) {return $value;        } elseif ($this->serializer = = = = NULL) {$value = Unserialize ($value);        } else {$value = Call_user_func ($this->serializer[1], $value); } if (Is_array ($value) &&! (        $value [1] instanceof Dependency && $value [1]->gethaschanged ($this)) {return $value [0];        } else {return false; }} Public function set ($key, $value, $duration = 0, $dependency = null) {if ($dependency!== null && Amp        $this->serializer!== false) {$dependency->evaluatedependency ($this);        } if ($this->serializer = = = null) {$value = serialize ([$value, $dependency]); } elseif ($this->serializer!== false) {$value = Call_user_func($this->serializer[0], [$value, $dependency]);        } $key = $this->buildkey ($key);    return $this->setvalue ($key, $value, $duration); }

⑤ the above method to add two non-encrypted direct read and write memcache problem solved

Yii2 Memcache Cross-platform interaction values cannot be unified

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.